Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle Generated using esbuild throwing the exception #1597

Closed
hassanazharkhan opened this issue Sep 12, 2021 · 3 comments
Closed

Bundle Generated using esbuild throwing the exception #1597

hassanazharkhan opened this issue Sep 12, 2021 · 3 comments

Comments

@hassanazharkhan
Copy link

hassanazharkhan commented Sep 12, 2021

I'm getting the following exception while running my bundled file on local, I'am using the library called dynamo-easy which is heavily relies on typescript decorators.

Exception:

throw new Error("make sure you have enabled the typescript compiler options which enable us to work with decorators (see doc)");

tsconfig.json:

{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "rootDir": "./",
    "outDir": "./dist"
  },
  "include": [
    "./src/**/*"
  ]
}

tsconfig.build.json

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es2019",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "importHelpers": true,
    "declaration": true,
    "module": "commonjs"
  },
  "exclude": [
    "node_modules",
    "dist",
    "build",
    "**/build",
    "**/dist",
    "**/node_modules",
    "cdk.out",
    "**/cdk.out",
    "__mocks__",
    "**/__mocks__/**",
    "**/__tests__/**",
    "**/*.test.ts"
  ]
}

esbuild version: ^0.12.25
Os: MacOS BigSur

@evanw
Copy link
Owner

evanw commented Sep 12, 2021

Looks like TypeScript decorators are working fine to me:

$ cat foo/bar/tsconfig.json 
{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "rootDir": "./",
    "outDir": "./dist"
  },
  "include": [
    "./src/**/*"
  ]
}

$ cat tsconfig.build.json 
{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es2019",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "importHelpers": true,
    "declaration": true,
    "module": "commonjs"
  },
  "exclude": [
    "node_modules",
    "dist",
    "build",
    "**/build",
    "**/dist",
    "**/node_modules",
    "cdk.out",
    "**/cdk.out",
    "__mocks__",
    "**/__mocks__/**",
    "**/__tests__/**",
    "**/*.test.ts"
  ]
}

$ cat foo/bar/check.ts 
let didDecoratorsWork = false
let decorator = () => didDecoratorsWork = true
@decorator class Foo {}
console.log({ didDecoratorsWork })

$ esbuild foo/bar/check.ts | node
{ didDecoratorsWork: true }

You'll have to be more specific about what isn't working. Right now I have no way of reproducing your issue.

@hassanazharkhan
Copy link
Author

hassanazharkhan commented Sep 12, 2021

@evanw Thank you for looking into this, I have created a repo with the bug stated above, would you able to check there?

https://github.com/hassanazharkhan/esbuild-decorator-bug

Reproduction steps:

npx projen && node esbuild.js && node -e "console.log(require('./build/index.js').handler({}));"

@evanw
Copy link
Owner

evanw commented Sep 13, 2021

It looks like this library you're trying to use relies on TypeScript's emitDecoratorMetadata setting. This does not work with esbuild because it requires a type system to function, and replicating TypeScript's type system within esbuild is out of scope due to complexity. One workaround is to run tsc first to convert .ts files into .js files and then run esbuild on the resulting .js files. But your issue is not a problem with esbuild because emitDecoratorMetadata is intentionally not supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants