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

Use node: prefix for node imports. #2358

Open
lgarron opened this issue Sep 12, 2023 · 2 comments
Open

Use node: prefix for node imports. #2358

lgarron opened this issue Sep 12, 2023 · 2 comments

Comments

@lgarron
Copy link

lgarron commented Sep 12, 2023

When bundling a program that uses yargs, the bundler needs to know whether to include certain dependencies. In particular, node dependencies should be excluded.

Unfortunately, yargs does not currently use the node: prefix, which means:

  • One has to enumerate these by hand (e.g. by running the build a few times and seeing which imports break).
  • These imports are indistinguishable from npm packages with the same name (often polyfills that a coder may or may not want to include for separate reasons).

Many bundlers also support wildcards for ignores: for example, you can set node:* as external packages in esbuild.

So using a node: prefix for all imports of node modules would make it simpler and safer to produce correct bundled code when using yargs.

This is available for ESM back to node 12, matching the compatibility defined here: https://github.com/yargs/yargs/blob/main/package.json#L125

@lgarron
Copy link
Author

lgarron commented Sep 12, 2023

Looks like updating https://github.com/yargs/yargs/blob/e0823dd7e6ced7eaf1d7d1e67f77374f4ef5cbce/lib/platform-shims/esm.mjs is pretty trivial (and similar for CJS, although I don't need that myself), but the yargs-parser build breaks with:

npm ERR! [!] (plugin Typescript) Error: Debug Failure. Unhandled SyntaxKind: Unknown.
npm ERR! Error: Debug Failure. Unhandled SyntaxKind: Unknown.
npm ERR!     at pipelineEmitWithHintWorker (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:114185:22)
npm ERR!     at pipelineEmitWithHint (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113716:17)
npm ERR!     at pipelineEmitWithComments (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:117404:13)
npm ERR!     at pipelineEmit (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113656:13)
npm ERR!     at emitExpression (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113640:13)
npm ERR!     at emitExportAssignment (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:115652:13)
npm ERR!     at pipelineEmitWithHintWorker (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113929:32)
npm ERR!     at pipelineEmitWithHint (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113716:17)
npm ERR!     at pipelineEmitWithComments (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:117404:13)
npm ERR!     at pipelineEmit (/Users/lgarron/Code/git/github.com/yargs/yargs-parser/node_modules/typescript/lib/typescript.js:113656:13)

https://rollupjs.org/configuration-options/ includes node: prefixes in its very own examples, so clearly the Rollup project is aware of them and had better have a way to handle them, but I haven't figured out how. Setting the "external" field in the rollup.config.js doesn't seem to be sufficient.

@shadowspawn
Copy link
Member

The error might be a dependency problem rather than node:. I saw similar error in yargs-parser (unrelated to node:):

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

No branches or pull requests

2 participants