evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.18k stars 1.15k forks source link

import issues #3588

Closed Murtatrxx closed 9 months ago

Murtatrxx commented 10 months ago
throw new Error(`tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.`);
          ^

Error: tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.
    at Object.<anonymous> (C:\Users\murat\OneDrive\Belgeler\Gradient\node_modules\tsyringe\dist\cjs\index.js:5:11)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)

I get this error, but I'm sure i'm importing it on the very first line on my ts file, like this;

import 'reflect-metadata';
import 'dotenv/config';
import '@sentry/tracing';

import { Logger } from './new-services-with-DI/Logger';
import sentry from '@sentry/node';

import { Client } from 'discord.js';
import { container } from 'tsyringe';
// ... other things

here's my tsup config file (it's an esbuild wrapper),

import { defineConfig } from 'tsup';

export default defineConfig({
  entry: [
    'src/**/**.ts',
    '!src/@types/*',
  ],
  format: ['esm'],
  target: 'esnext',
  external: ['undici'],
  bundle: true,
  splitting: true,
  silent: true,
  clean: true,
  outDir: 'dist',
  minify: true,
 });

Can someone explain why, or maybe suggest a fix?

hyrious commented 10 months ago

tsyringe requires a feature (emitDecoratorMetadata) that involves type system which esbuild cannot provide.

If you still want to use esbuild to bundle your source codes, you can use plugins to transform your TypeScript sources to with the reflect metadata. There're 2 possible ways:

Murtatrxx commented 10 months ago

But when I run my code with tsx, is just runs fine, but when i bundle them manually, this happens.

Murtatrxx commented 10 months ago

Oh maybe it's the code splitting that is causing the issues?

Murtatrxx commented 10 months ago

Because it messes up the import orders