metadevpro / openapi3-ts

TS Model & utils for creating and exposing OpenAPI 3.x contracts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v3.0: __exportStar is not defined

RobinTail opened this issue · comments

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?

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).

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?

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"
  }
}

Look at this @jonluca
I guess the hack we did at #80 (comment) adding the *.js extension could be producing this issue here.

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

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

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.

I made a PR that I believe should fix the issue:
#82
@pjmolina && @jonluca

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