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

support loading ESM plugins from the CLI via --plugin #4265

Merged
merged 2 commits into from Nov 12, 2021
Merged

support loading ESM plugins from the CLI via --plugin #4265

merged 2 commits into from Nov 12, 2021

Conversation

kzc
Copy link
Contributor

@kzc kzc commented Nov 5, 2021

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:

n/a

Description

Rollup presently only supports CJS plugins from the CLI via --plugin. This PR adds seamless ESM plugin support to the CLI. To maintain backwards compatibility rollup will still attempt to resolve the plugin via require first, and only upon error will it attempt to load the plugin via a dynamic import(). This is due to the different module resolution semantics of CJS and ESM modules in NodeJS.

The use of dynamic import to load ESM plugins works with Node v12+. No rollup CLI functionality is lost on NodeJS versions that do not support loading ESM via dynamic import.

@codecov
Copy link

codecov bot commented Nov 5, 2021

Codecov Report

Merging #4265 (9c5f2eb) into master (2810269) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4265   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         204      204           
  Lines        7289     7294    +5     
  Branches     2081     2081           
=======================================
+ Hits         7172     7177    +5     
  Misses         58       58           
  Partials       59       59           
Impacted Files Coverage Δ
cli/run/commandPlugins.ts 97.61% <100.00%> (+0.25%) ⬆️
cli/run/index.ts 100.00% <100.00%> (ø)
cli/run/loadConfigFile.ts 96.15% <100.00%> (+0.07%) ⬆️
cli/run/loadConfigFromCommand.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2810269...9c5f2eb. Read the comment docs.

@kzc
Copy link
Contributor Author

kzc commented Nov 5, 2021

Interesting... the new ESM CLI plugin tests succeed on Node v12 here, but not on my local machine. The Node v12 version used by github CI must be more recent than my local version.

The Node v10 failures for the new ESM CLI plugin tests were expected - both here and on my local machine. I will add a condition to skip the ESM CLI plugin tests on Node v10, as no old functionality is lost on that Node version.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks a lot!

@dnalborczyk
Copy link
Contributor

Interesting... the new ESM CLI plugin tests succeed on Node v12 here, but not on my local machine. The Node v12 version used by github CI must be more recent than my local version.

The Node v10 failures for the new ESM CLI plugin tests were expected - both here and on my local machine. I will add a condition to skip the ESM CLI plugin tests on Node v10, as no old functionality is lost on that Node version.

the experimental-modules flag was removed in v12.17, and esm was considered stable in v12.22.

@dnalborczyk
Copy link
Contributor

I think rollup should probably make the move to full esm for v3 as well and move the ecosystem forward! it should be perfectly fine when used from the cli as esm is supported from node.js 12.22+. some friction could potentially arise when used in code, but that can be mitigated by using dynamic imports in cjs if needed.

@kzc
Copy link
Contributor Author

kzc commented Nov 11, 2021

Even if rollup v3 moves to an all ESM toolchain for the CLI, rollup v2 should support both ESM and CJS during the transition period. Although it's technically possible to mimic the semantics of require with dynamic import(), it's non-trivial. require module resolution has a lot of peculiar historical behaviors.

@dnalborczyk
Copy link
Contributor

Even if rollup v3 moves to an all ESM toolchain for the CLI, rollup v2 should support both ESM and CJS during the transition period.

yes, I agree. that was just a thought I had in mind since it touched the esm topic.

@lukastaegert lukastaegert merged commit 2d85884 into rollup:master Nov 12, 2021
@mreinstein
Copy link

@kzc this is freakin awesome, thank you!! ❤️

@kzc
Copy link
Contributor Author

kzc commented Nov 12, 2021

Glad it's useful. It simplified my workflow.

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.

None yet

4 participants