Skip to content

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 shilman and tmeasday 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

@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
@ndelangen ndelangen removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Oct 28, 2025
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted.

4 participants