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

[v3.0] Keep dynamic imports in CommonJS output #4647

Merged
merged 4 commits into from Oct 7, 2022

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Oct 4, 2022

BREAKING CHANGE: We keep external dynamic imports in CommonJS output by default as import()

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

In order to complete the Node ESM story in Rollup 3, I decided to add this last feature to Rollup 3. This will by default keep external (and only those) dynamic imports in CommonJS output as import(…) to support ESM interop. You can deactivate this behavior via a new option dynamicImportInCjs: false, which will activate the old rewriting behavior.

output.dynamicImportInCjs

Type: boolean
CLI: --dynamicImportInCjs/--no-dynamicImportInCjs
Default: true

While CommonJS output originally supported only require(…) to import dependencies, recent Node versions also started to support import(…), which is the only way to import ES modules from CommonJS files. If this option is true, which is the default, Rollup will keep external dynamic imports as import(…) expressions in CommonJS output. Set this to false to rewrite dynamic imports using require(…) syntax.

// input
import('external').then(console.log);

// cjs output with dynamicImportInCjs: true or not set
'use strict';

import('external').then(console.log);

// cjs output with dynamicImportInCjs: false
'use strict';

function _interopNamespaceDefault(e) {
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(
          n,
          k,
          d.get
            ? d
            : {
                enumerable: true,
                get: function () {
                  return e[k];
                }
              }
        );
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

Promise.resolve()
  .then(function () {
    return /*#__PURE__*/ _interopNamespaceDefault(require('external'));
  })
  .then(console.log);

@github-actions
Copy link

github-actions bot commented Oct 4, 2022

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#rollup-3-cjs-dynamic-import

or load it into the REPL:
https://rollupjs.org/repl/?pr=4647

@codecov
Copy link

codecov bot commented Oct 4, 2022

Codecov Report

❗ No coverage uploaded for pull request base (release-3.0.0@b33513c). Click here to learn what that means.
The diff coverage is 100.00%.

@@               Coverage Diff                @@
##             release-3.0.0    #4647   +/-   ##
================================================
  Coverage                 ?   98.97%           
================================================
  Files                    ?      213           
  Lines                    ?     7485           
  Branches                 ?     2115           
================================================
  Hits                     ?     7408           
  Misses                   ?       23           
  Partials                 ?       54           
Impacted Files Coverage Δ
src/utils/options/mergeOptions.ts 100.00% <ø> (ø)
src/ast/nodes/ImportExpression.ts 100.00% <100.00%> (ø)
src/utils/options/normalizeOutputOptions.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@lukastaegert lukastaegert force-pushed the rollup-3-cjs-dynamic-import branch 3 times, most recently from 24ededd to b46330e Compare October 4, 2022 07:19
@lukastaegert lukastaegert changed the base branch from master to release-3.0.0 October 5, 2022 04:25
@lukastaegert lukastaegert merged commit 58b226a into release-3.0.0 Oct 7, 2022
@lukastaegert lukastaegert deleted the rollup-3-cjs-dynamic-import branch October 7, 2022 18:26
lukastaegert added a commit that referenced this pull request Oct 10, 2022
* Support dynamic imports in cjs output and make default

* Add documentation

* Also use import for unresolvable dynamic imports

* Extend CLI docs
@lukastaegert lukastaegert mentioned this pull request Oct 11, 2022
9 tasks
@rollup-bot
Copy link
Collaborator

This PR has been released 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.

lukastaegert added a commit that referenced this pull request Oct 11, 2022
* Support dynamic imports in cjs output and make default

* Add documentation

* Also use import for unresolvable dynamic imports

* Extend CLI docs
@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.0.0. You can test it via npm install rollup.

divdavem added a commit to AmadeusITGroup/kassette that referenced this pull request Dec 1, 2022
divdavem added a commit to AmadeusITGroup/kassette that referenced this pull request Dec 1, 2022
vreynolds pushed a commit to honeycombio/libhoney-js that referenced this pull request Apr 21, 2023
Bumps [rollup](https://github.com/rollup/rollup) from 2.79.0 to 3.20.2.

The only difference in artifacts is in the CJS file. The dynamic import of superagent is no longer rewritten as require (rollup/rollup#4647). We can turn that off with rollup config, but it should work with this change since dynamic import in CJS has been available since node version 9.

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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 this pull request may close these issues.

Allow preservation of dynamic import with variable in CJS
2 participants