metadevpro / openapi3-ts

TS Model & utils for creating and exposing OpenAPI 3.x contracts.
MIT License
485 stars 64 forks source link

v3.0: __exportStar is not defined #81

Closed RobinTail closed 2 years ago

RobinTail commented 2 years ago

Hello. I wanted to try the latest major update, but I'm getting the following error:

node_modules/openapi3-ts/dist/cjs/index.js:3
__exportStar(require("./model/index.js"), exports);
^
ReferenceError: __exportStar is not defined

Node version is 18.6. Typescript 4.7.4

Do you know why this is happening and/or how it can be fixed?

pjmolina commented 2 years ago

If you are consuming it with Typescript, reference the sources (from the src folder). CJS (CommonJS version is packaged for JS consumers in NodeJS mainly).

RobinTail commented 2 years ago

In this case I'm getting another error:

node_modules/openapi3-ts/src/index.ts:1
export * from './model/index.js';
^^^^^^

SyntaxError: Unexpected token 'export'

Any recommendations?

RobinTail commented 2 years ago

My tsconfig

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "resolveJsonModule": true
  },
}

where the extension is

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 14",

  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node"
  }
}
pjmolina commented 2 years ago

Look at this @jonluca I guess the hack we did at https://github.com/metadevpro/openapi3-ts/pull/80#discussion_r940379397 adding the *.js extension could be producing this issue here.

RobinTail commented 2 years ago

The problem is the export itself. As per my understanding export works in ESM, while my project is not.

RobinTail commented 2 years ago

Or maybe the build itself should be adjusted to include the definition of __exportStar, so /dist/cjs can be used without any issues.

I found out that __exportStar is a part of tslib: https://www.npmjs.com/package/tslib

RobinTail commented 2 years ago

Ah! I think I found out what causes that. In your tsconfig.json there is a line

"noEmitHelpers": true,

it's also not overridden in tsconfig-cjs.json, so the produced CJS build does not have those tslib helpers for exports/imports.

RobinTail commented 2 years ago

I made a PR that I believe should fix the issue: https://github.com/metadevpro/openapi3-ts/pull/82 @pjmolina && @jonluca

jonluca commented 2 years ago

Yes those are helpers emitted by typescript that might be missing when you consume the bundle. That PR should fix it up. Thanks @RobinTail!

pjmolina commented 2 years ago

Thanks @RobinTail & @jonluca Merged.