-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ESM named imports work in Node (#531)
Importing slang from a native ESM module is pretty cumbersome in Node right now. Here's an example of an error you may get if you try to use a normal named import: ``` % node index.mjs file:///private/tmp/npmproject-4/index.mjs:6 import { ProductionKind } from "@NomicFoundation/slang/syntax/parser/index.js"; ^^^^^^^^^^^^^^ SyntaxError: Named export 'ProductionKind' not found. The requested module '@NomicFoundation/slang/syntax/parser/index.js' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: import pkg from '@NomicFoundation/slang/syntax/parser/index.js'; const { ProductionKind } = pkg; at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21) at async ModuleJob.run (node:internal/modules/esm/module_job:190:5) Node.js v18.16.1 ``` This PR changes how we export things in JavaScript so that named imports from ESM modules work in Node.js. The difference is really subtle and may be hard to understand at first glance, so here's an explanation: * CJS and ESM aren't really compatible, but Node implements a compatibility mode which is not fully transparent. * One of the things that is not transparent is the `default` export, as that doesn't exist in CJS. * The compatibility layer hence treats reassigning `module.exports` as a `default` exports, hoping that it was the original intention of the author. That leads to named imports from ESM not working if you reassign it. * If you keep the original `module.exports` and just add properties to it, those are treated as named exports.
- Loading branch information
1 parent
f77330a
commit e3450be
Showing
6 changed files
with
18 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nomicfoundation/slang": patch | ||
--- | ||
|
||
Make ESM named imports work in Node.js. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters