Git Product home page Git Product logo

Comments (4)

vladimir-gal avatar vladimir-gal commented on June 16, 2024

Any updates on this ? Trying to get thinks working with this plugin + TypeORM

from vite-plugin-node.

richard-viney avatar richard-viney commented on June 16, 2024

No updates from my side, I haven’t looked into this further since my last post. TypeORM 0.3 was released recently which might affect things here in some way.

from vite-plugin-node.

JamesB797 avatar JamesB797 commented on June 16, 2024

I have Vite + SWC working on a rather large project using Typeorm ^0.3.4

That being said there are many potential issues that could cause that message, so I'll try to list some of them out.

The main obvious one:
You should not provide your entities as files anymore. Use DataSource and pass an array of entity classes, same goes for migrations. Imagine you have an index.ts that exports const allEntities = [EntityA, EntityB, ...] etc and then make a DataSource like

import { allEntities } from '../entities';
import { allMigrations } from '../migrations';

export const dataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'testing',
  password: 'testing',
  database: 'testing',
  synchronize: false,
  logging: false,
  entities: allEntities,
  migrations: allMigrations,
});

Circular references:
They're a huge problem when switching to a bundler from TSC. TSC compiles each file to JS separately so there's no problem, but SWC / ESBuild bundle everything together, so if a symbol appears before it's used, it'll cause an issue. You can get around this by using import type instead of import as much as possible, especially in your entity files for relationship typing, and just not having any circular dependencies.

Decorators:
Reflection metadata is outright not supported in ESBuild, but in SWC it's supported with some caveats. Circular dependencies in decorators are not handled very well and are almost guaranteed to break things. The best way to debug this is by building your bundle with vite build and then inspecting the offending symbols in the bundle to see why the include order got messed up

My Vite config is like this:

import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';

const port = Number(process.env.VITE_PORT);

export default defineConfig({
  build: {
    target: 'node14',
    outDir: `dist`,
    sourcemap: 'inline',
  },
  esbuild: false,
  server: port ? { port } : {},
  optimizeDeps: {
    exclude: ['pg-native'],
  },
  plugins: [
    ...VitePluginNode({
      exportName: 'main',
      adapter: 'express',
      appPath: `function/index.ts`,
      tsCompiler: 'swc',
      swcOptions: {
        module: {
          type: 'es6',
        },
        jsc: { keepClassNames: true },
      },
    }),
  ],
});

Hope that helps, let me know if you have more details or get it working

from vite-plugin-node.

axe-me avatar axe-me commented on June 16, 2024

@JamesB797 Thank you for your help!
@richard-viney and @RDIL Can you try out James' solution to see if that works for you.

from vite-plugin-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.