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

Tech: do not generate ESM dist for preset files #22330

Merged
merged 5 commits into from
May 2, 2023
Merged

Conversation

ndelangen
Copy link
Member

Closes #21316

What I did

When TSup is bundling code, it will add a bit of require polyfill code to the output, when it encounters code referencing require in 1 of the entrypoints.

It will add the polyfill code to all entrypoints, and will refuse to treeshake it out.

So I made en excemption for preset entries, to never get a mjs file generated, because those often have references to require in them.

How to test

Run the build process and check for existence of this type of code in dist:

var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw new Error('Dynamic require of "' + x + '" is not supported');
});

export {
  __require
};

At least within the docs addon this no longer happens.

This code does impact all monorepo packages though, but I think that's fine. AFAIK we never use mjs exports of presets, always the cjs files.

Some day we'll convert everything over to ONLY ESM, and have all packages be type=module, but that's a project for future me.

@ndelangen ndelangen self-assigned this May 1, 2023
@ndelangen ndelangen added maintenance User-facing maintenance tasks patch:yes Bugfix & documentation PR that need to be picked to main branch labels May 1, 2023
@ndelangen ndelangen changed the title do not generate ESM for preset entries Tech: do not generate ESM dist for preset files May 1, 2023
@ndelangen
Copy link
Member Author

before:
Screenshot 2023-05-01 at 16 38 25
Screenshot 2023-05-01 at 16 38 39

after:
Screenshot 2023-05-01 at 15 55 42

@ndelangen ndelangen requested review from tmeasday and shilman May 1, 2023 14:39
@ndelangen ndelangen requested a review from shilman May 1, 2023 15:57
Copy link
Member

@tmeasday tmeasday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a neat solution

scripts/prepare/bundle.ts Show resolved Hide resolved
@ndelangen ndelangen merged commit 5c3ec1f into next May 2, 2023
@ndelangen ndelangen deleted the norbert/fix-21316 branch May 2, 2023 07:34
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label May 3, 2023
shilman pushed a commit that referenced this pull request May 3, 2023
Tech: do not generate ESM dist for preset files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance User-facing maintenance tasks patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
3 participants