wessberg / rollup-plugin-ts

A TypeScript Rollup plugin that bundles declarations, respects Browserslists, and enables seamless integration with transpilers such as babel and swc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CJS bundle contains import statement

georgesmith46 opened this issue · comments

  • 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 👍