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

Add CLI flag to explicitly disable config file transpilation #4055

Closed
pastelmind opened this issue Apr 22, 2021 · 8 comments · Fixed by #4549
Closed

Add CLI flag to explicitly disable config file transpilation #4055

pastelmind opened this issue Apr 22, 2021 · 8 comments · Fixed by #4549

Comments

@pastelmind
Copy link

pastelmind commented Apr 22, 2021

Feature Use Case

Rollup gained support for native ESM configs in #3445. This feature enables Rollup to "give up" transpiling and let Node.js execute the raw ESM. However, support for ESM configs are still flaky at the moment:

  • Rollup requires Node.js >= 13 to support native ESM config. However, native ESM is well-supported in Node.js v12.x as well. In 12.17.0, the --experimental-modules flag was made redundant; in 12.22.0 native ESM was declared "stable".
    Node.js v12 is scheduled to be maintained until April 2022. In the meantime, many projects staying on v12 will be unable to use native ESM for their Rollup config file.
    See:

    function supportsNativeESM() {
    return Number(/^v(\d+)/.exec(process.version)![1]) >= 13;
    }

    • This can be fixed by changing the constant, or by using a proper semver comparison.
  • When package.json has "type": "module", Node.js treats all scripts with the .js extension as native ESM. This behavior was introduced in Node.js 12+. However, Rollup does not respect this field when consuming rollup.config.js: it still transpiles the config to CJS before passing it to Node.js.
    This becomes a problem if the config file imports a pure ES module: Because Rollup transpiles the imports to require()s, the build fails with "Node tried to require an ES module from a CommonJS file, which is not supported.".
    See:

    if (err.code === 'ERR_REQUIRE_ESM') {
    return error({
    code: 'TRANSPILED_ESM_CONFIG',
    message: `While loading the Rollup configuration from "${relativeId(
    fileName
    )}", Node tried to require an ES module from a CommonJS file, which is not supported. A common cause is if there is a package.json file with "type": "module" in the same folder. You can try to fix this by changing the extension of your configuration file to ".cjs" or ".mjs" depending on the content, which will prevent Rollup from trying to preprocess the file but rather hand it to Node directly.`,
    url: 'https://rollupjs.org/guide/en/#using-untranspiled-config-files'
    });
    }

    • This cannot be "fixed" without breaking existing code that may be relying on Rollup transpiling the config.

While these two issues can be resolved separately, we may expect further edge cases until the ecosystem adapts to native ESM. Rollup's "should I transpile the config?" detection method may not reflect the interests of its users. Furthermore, changes in the detection method would break existing code that relies on the current behavior. Therefore, it would be nice to give users an escape hatch that gives full control over whether Rollup should transpile its config file.

Feature Proposal

Add a --[no--]skip-config-transpile CLI flag. --skip-config-transpile will force Rollup not to transpile the config, and --no-skip-config-transpile will force Rollup to transpile the config. In both cases, Rollup leaves the decision up to the user; it forgoes checking the version of Node.js or the config file extension.

Some notes:

  • If either flag is not provided, Rollup retains the current behavior. This maintains backwards compatibility.
  • Since the decision to transpile a config file must be made before loading the config, we cannot control this behavior through the config file itself. This has to be a CLI flag.
  • When the ecosystem around native ESM solidifies, Rollup could change the default behavior without worrying about large-scale breakage.
@pastelmind pastelmind changed the title Add CLI flag to explicitly disable Rollup's own ESM-to-CJS transpilation Add CLI flag to explicitly disable config file transpilation Apr 22, 2021
@lukastaegert
Copy link
Member

Maybe a positive flag might be less confusing as it avoids the double negation of --no-skip-config-transpile, i.e --transpile-config or no-transpile-config.
Otherwise am I right to assume that providing this flag will override Rollup's auto-detection, meaning that .mjs could be force-transpired for Node 14?
I guess a PR for this would be welcome.

@pastelmind
Copy link
Author

I'm not familiar with Rollup's codebase. Could you provide suggestions on what files should I look at to add a CLI option? Also, do I need to write tests for CLI options?

@lukastaegert
Copy link
Member

Also, do I need to write tests for CLI options?

Yes please. CLI tests are found in /test/cli/samples and work by directly invoking a command and looking at the generated output. They are configured by a _config.js file in the respective folder which also supports a minNodeVersion flag if you want to test something only on e.g. ESM capable Node versions. One difference one could test is that transpiled config files do not need extensions for their imports while non-transpiled config files can import ES modules from node_modules (there is a special node_modules folder in /test/cli for this).

Could you provide suggestions on what files should I look at to add a CLI option?

The entire CLI is in the /cli folder. The most important part for you is in loadConfigFile.ts where it is decided how the file is loaded. Here you have access to the command options.

Also, you should extend documentation in /cli/help.md and /docs/01-command-line-reference.md.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4574 as part of rollup@3.0.0-5. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-5 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4574 as part of rollup@3.0.0-6. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-6 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4574 as part of rollup@3.0.0-7. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-7 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4574 as part of rollup@3.0.0-8. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-8 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4574 as part of rollup@3.0.0. You can test it via npm install rollup.

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

Successfully merging a pull request may close this issue.

3 participants