Skip to content

Commit

Permalink
fix: get full list of exports for esm compat in dist-cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jan 19, 2024
1 parent 7841411 commit 6f4713a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
28 changes: 27 additions & 1 deletion scripts/compilation/Inliner.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,33 @@ module.exports = class Inliner {
}

/**
* step 6: we validate that the index.js file has a require statement
* Step 6: "Annotate the CommonJS export names for ESM import in node",
* except, correctly.
*/
async annotateCjsExportNames() {
if (this.bailout) {
return this;
}
const exportNames = Object.keys(require(this.outfile));
/* (find and replace the following)
0 && (module.exports = {
...
});
*/
this.indexContents = this.indexContents.replace(
/0 && \(module\.exports = \{((.|\n)*?)\}\);/,
`
0 && (module.exports = {
${exportNames.join(",\n ")}
});
`
);
fs.writeFileSync(this.outfile, this.indexContents, "utf-8");
return this;
}

/**
* step 7: we validate that the index.js file has a require statement
* for any variant files, to ensure they are not in the inlined (bundled) index.
*/
async validate() {
Expand Down
1 change: 1 addition & 0 deletions scripts/compilation/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if (!package) {
await inliner.rewriteStubs();
await inliner.fixVariantImportPaths();
await inliner.dedupeExternals();
await inliner.annotateCjsExportNames();
await inliner.validate();
})();
}

0 comments on commit 6f4713a

Please sign in to comment.