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

CJS bundle contains import statement #212

Closed
georgesmith46 opened this issue May 10, 2023 · 1 comment
Closed

CJS bundle contains import statement #212

georgesmith46 opened this issue May 10, 2023 · 1 comment

Comments

@georgesmith46
Copy link

georgesmith46 commented May 10, 2023

  • Version: >=3.1.0
  • Rollup Version: N/A
  • Operating System and version (if applicable):
  • Node Version (if applicable):
  • Does it work with tsc (if applicable):

Reproduction

I've been working on a library which wraps rollup + rollup-plugin-ts and exports a build function. I want to run the rollup build within Jest so that I can test it.

Example project: https://github.com/georgesmith46/rollup-plugin-ts-test
Run npm i && npx jest

Expected Behavior

The test should pass.

Actual Behavior

The following error is thrown:

ReferenceError: The following babel dependencies could not be found within your node_modules folder: "@babel/core", "@babel/runtime", "@babel/plugin-transform-runtime", and "@babel/preset-env". Make sure to install them if you want to use babel for transpilation

Issue detail

It looks like there are two issues at play here. Firstly, the error message thrown here is overriding the actual import error message and therefore making it difficult to debug the issue. I would suggest also logging the error contained in core.

The real error is Error: You need to run with a version of node that supports ES Modules in the VM API. which is Jest telling us that if we want to use the import statement, we need to enable the experimental ES module support.

So this gets us to the crux of the issue, the CJS bundle of this package contains the import statement which Jest doesn't like.

// dist/cjs/index.cjs >=3.1.0
const results = await Promise.all(
  moduleNames
    .map(async (moduleName) => import(moduleName))
    .map(async (promise) =>
      promise
        .then((value) => ({
          status: "fulfilled",
          value,
        }))
        .catch((reason) => ({
          status: "rejected",
          reason,
        }))
    )
);
// dist/cjs/index.cjs <3.1.0
const results = await Promise.all(
  moduleNames
    .map(async (moduleName) =>
      Promise.resolve().then(() =>
        /*#__PURE__*/ _interopNamespace(require(moduleName))
      )
    )
    .map(async (promise) =>
      promise
        .then((value) => ({
          status: "fulfilled",
          value,
        }))
        .catch((reason) => ({
          status: "rejected",
          reason,
        }))
    )
);

Let me know if this is actually an issue with Jest/Babel and I can raise it with them instead 👍

@wessberg
Copy link
Owner

wessberg commented Aug 3, 2023

Hey there. Thanks for the bug report! You're right that the dynamic import expression left in the CommonJS bundle is definitely not intentional on my part. After investigating a little, it seems to be caused by a changed default setting in Rollup v3. I've made sure to negate this flag such that the CommonJS bundle is, well, just that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants