esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

import issues

Open Murtatrxx opened this issue 1 year ago • 4 comments

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?

Murtatrxx avatar Jan 12 '24 20:01 Murtatrxx

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:

hyrious avatar Jan 13 '24 18:01 hyrious

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

Murtatrxx avatar Jan 13 '24 20:01 Murtatrxx

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

Murtatrxx avatar Jan 13 '24 20:01 Murtatrxx

Because it messes up the import orders

Murtatrxx avatar Jan 13 '24 20:01 Murtatrxx