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

Support/ignore export as namespace TypeScript syntax #2070

Closed
shroudedcode opened this issue Mar 2, 2022 · 5 comments
Closed

Support/ignore export as namespace TypeScript syntax #2070

shroudedcode opened this issue Mar 2, 2022 · 5 comments

Comments

@shroudedcode
Copy link

Recently I've come across multiple packages which use the (admittedly poorly documented) export as namespace syntax in their declaration files to declare that they provide both CommonJS exports and a global namespace object. Searching DefinitelyTyped alone brings up 706 occurrences of this pattern.

When bundling a script importing one of these packages ESBuild reports the following error:

 [ERROR] Unexpected "as"

    node_modules/@types/mapbox__polyline/index.d.ts:15:7:
      15  export as namespace polyline;
                 ~~

It seems like ESBuild simply doesn't know the export as namespace syntax yet, so the as keyword is flagged as unexpected in that context.

Proposal

Since (as far as I know) ESBuild only supports actual module formats (and not global namespace objects), I think it would be a good enough solution to just completely ignore that line. Modules containing it would then be treated solely according to their remaining export declarations.

@magic-akari
Copy link
Contributor

It is weird.
IIRC, esbuild doesn't read .d.ts.

@shroudedcode
Copy link
Author

@magic-akari

IIRC, esbuild doesn't read .d.ts.

I thought so too, but this comment from the changelog seems to suggest the opposite.

@evanw
Copy link
Owner

evanw commented Mar 2, 2022

You aren't supposed to feed esbuild .d.ts files, so esbuild shouldn't ever even see them in the first place. However, esbuild sometimes ends up seeing them, so it does still try to parse their different syntax to avoid build errors. But the syntax is parsed and discarded because .d.ts files only contain type definitions (at least they are supposed to be used that way) and esbuild ignores all type definitions. I can change esbuild to parse and ignore this code.

magic-akari added a commit to magic-akari/esbuild that referenced this issue Mar 2, 2022
@shroudedcode
Copy link
Author

Thanks for the help @magic-akari and @evanw!

I've spent some more time looking into this issue and realized that it was partly caused by the following setting in my project's tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@mapbox/polyline": ["node_modules/@types/mapbox__polyline/index.d.ts"]
    }
  }
}

I initially set this up to work around an issue with TypeScript's experimental NodeNext module resolution mode which seems to have been fixed in the meantime. What I didn't know at that time was that ESBuild respects the paths option as well but for resolving actual source files.

Maybe the real fix here isn't to add support for export as namespace, but instead to ignore any paths entries ending in .d.ts because those are only added to fix typing issues. 🤔

I've created a little reproduction repo (with regular TypeScript module resolution because that doesn't seem to be part of the issue) here which has two scripts (bundle and bundle:paths) which bundle the index.ts file once with and once without the paths option.

@evanw evanw closed this as completed in 50dc6ae Mar 2, 2022
@magic-akari
Copy link
Contributor

Maybe the real fix here isn't to add support for export as namespace, but instead to ignore any paths entries ending in .d.ts because those are only added to fix typing issues.

agree with you.

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

Successfully merging a pull request may close this issue.

3 participants