-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
CJS exports inside if/else blocks are not exposed to ESM code #18512
Comments
Hi, thanks for the detailed issue. The problem is most likely that the exports are wrapped in a condition If the condition is necessary in some environments, a possible solution might be adding a separate file with really static exports and referencing it under a
We use the exports in the following order: 'browser', 'module', 'import', 'production', 'default'. |
Both Deno and Node.js disagree with you 🤣 // lib.cjs
if (false) exports.hi = 34 // app.mjs
// DOES NOT throw a syntax error
import { hi } from "./lib.cjs"
console.log(hi)
//=> undefined I think that there is definite value to aligning with the existing Deno + Node.js static export detection logic even if it's "wrong" idealistically. as for my solution, I think I'm going to end up doing something like this only for browser targets: exports.named = exports.otherNamed = exports.hi = void 0;
if (typeof primordials !== "undefined") {
module.exports = primordials;
} else {
module.exports = {
hi: require("./hi.js"),
};
} but that incurs the additional overhead of duplicating all 760 named exports twice: once redundantly to satisfy under-classifying CJS lexers and once to actually assign them 🤷♂️ |
Actually, that still doesn't work: Raw text from unpkg: https://unpkg.com/@nodefill/[email protected]/dist/index-browser.js exports.uncurryThis = exports.applyBind = exports.Proxy =
exports.globalThis = exports.decodeURI /* ... */ = void 0
if (typeof primordials !== 'undefined') {
module.exports = primordials;
} else {
module.exports = {
["uncurryThis"]: require("./uncurryThis.js");
// ...
}
Object.setPrototypeOf(module.exports, null);
Object.freeze(module.exports);
} esm.run + esm.sh https://esm.sh/@nodefill/[email protected] ✅ |
Definitely, I agree that it would be a nice improvement, which we should consider. That will take some time though, so my previous response was aiming for an immediate solution.
This problem seems like something we can fix quickly. However, looking at the currently supported patterns, I think there's an even better approach that doesn't require duplication and should work right now: (() => {
if (typeof primordials !== "undefined") {
module.exports = primordials;
return;
}
module.exports = {
hi: require("./hi.js"),
name: require("./name.js"),
};
})(); The point is having |
Great! Let's keep it open for anyone who comes across the same issue until we improve the lexer to handle the original case as well. |
Sidenote that might warrant another issue: esm.run has the same issue as esm.sh with non-JS identifier exports. These are valid in ES2022: // Yes, this is valid JavaScript!
import { "RegExpGet$&" as RegExpGet$amp } from "@nodefill/primordials";
console.log(RegExpGet$amp());
//=> '\x00\x00\x00' ☝ yes that works in Node.js and deno (after i prodded them about it denoland/deno#19665 denoland/deno#19679 ) and should be exposed on the module namespace. you at least do better than esm.sh which doesn't expose .eval! |
Ok, that's interesting. I didn't know |
TypeScript doesn't support it which is probably why it's not very common. microsoft/TypeScript#40594 |
Unfortunately, it seems rollup (at least in the version we use) can't handle that syntax either, so the fix isn't as easy as I thought. I'll open a separate issue. |
Description
Normally (in Node.js, Bun, or Deno) when you import something that is CJS using an ESM import() or import statement, you get a module object with the .default being the raw module.exports of the CJS module, plus any statically-analyzable exports.thing = value names as names available on the esm object. Here's a quick demo:
This even works on esm.run!
However, that doesn't appear to be working for this package: https://www.npmjs.com/package/@nodefill/primordials
example of deno vs esm.run:
I don't know how you're currently detecting CJS exports, but I do know that Node.js seems to be using https://github.com/nodejs/cjs-module-lexer (idk what Deno uses)
github repo of npm package https://github.com/nodefill/primordials
Affected jsDelivr links
https://esm.run/@nodefill/primordials
Response headers
https://cdn.jsdelivr.net/npm/@nodefill/primordials/+esm
Information
Requisites
Additional content
esm.sh also has this problem so you're not alone (this would be a great chance to 1-up them tho 🤣)
The text was updated successfully, but these errors were encountered: