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

"Annotate" exported object to fix named / namespace imports of our API in Node ESM #57133

Merged
merged 20 commits into from
Mar 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename for clarity
  • Loading branch information
jakebailey committed Jan 24, 2024
commit d8108fc04024a3731305ef88849fc9e5abb64fc0
12 changes: 6 additions & 6 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async function runDtsBundler(entrypoint, output) {
]);
}

const requireFn = createRequire(import.meta.url);
const require = createRequire(import.meta.url);

/**
* @param {string} entrypoint
Expand Down Expand Up @@ -226,17 +226,17 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
// ensuring that source maps still work.
//
// See: https://github.com/evanw/esbuild/issues/1905
const require = "require";
const fakeName = "Q".repeat(require.length);
const requireStr = "require";
const fakeName = "Q".repeat(requireStr.length);
const fakeNameRegExp = new RegExp(fakeName, "g");
options.define = { [require]: fakeName };
options.define = { [requireStr]: fakeName };
options.plugins = [
{
name: "post-process",
setup: build => {
build.onEnd(async () => {
let contents = await fs.promises.readFile(outfile, "utf-8");
contents = contents.replace(fakeNameRegExp, require);
contents = contents.replace(fakeNameRegExp, requireStr);
await fs.promises.writeFile(outfile, contents);

// This is a trick esbuild uses when emitting CJS to ensure that
Expand All @@ -253,7 +253,7 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
// https://github.com/evanw/esbuild/issues/3281

// Using createRequire here is emperically faster than await import.
const obj = requireFn(outfile);
const obj = require(outfile);
const names = Object.keys(obj);
const fakeExport = ` 0 && (module.exports = {\n${names.map(name => ` ${name},\n`).join("")} });`;
contents = contents.replace("module.exports = ts;", `module.exports = ts;\n${fakeExport}`);
Expand Down
Loading