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 native ESM configs in Node 13, support untranspiled configs #3445

Merged
merged 14 commits into from Mar 29, 2020

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Mar 16, 2020

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

resolves #3443
resolves #2953

This will add support for native ESM and untranspiled configs, which will solve two issues:

  • If a config is in a "type": "module" folder, it could not import real ESM plugins in Node 13
  • if a config imports a plugin from a nested folder, the __dirname and import.meta.url were wrong

Support will be based on file extension:

  • If the extension is .cjs, the config will assumed to be CommonJS and will be required untranspiled
  • If the extension is .mjs AND the Node version is at least 13, the config will be assumed to be native ESM and will be imported untranspiled
  • Otherwise the old logic (transpiling to CJS) will be used

TODO:

  • Add helpful error messages for common issues that point users to using the correct extensions and file format
  • Automatically check for configs in the following order: rollup.config.mjs, rollup.config.cjs, rollup.config.js.

@rollup-bot
Copy link
Collaborator

rollup-bot commented Mar 16, 2020

Thank you for your contribution! ❤️

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

npm install rollup/rollup#load-untranspiled-config-file

or load it into the REPL:
https://rollupjs.org/repl/?circleci=10188

@codecov
Copy link

codecov bot commented Mar 16, 2020

Codecov Report

Merging #3445 into master will increase coverage by 0.72%.
The diff coverage is 94.41%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3445      +/-   ##
==========================================
+ Coverage   95.08%   95.81%   +0.72%     
==========================================
  Files         171      174       +3     
  Lines        5861     5898      +37     
  Branches     1732     1737       +5     
==========================================
+ Hits         5573     5651      +78     
+ Misses        156      127      -29     
+ Partials      132      120      -12     
Impacted Files Coverage Δ
cli/run/build.ts 89.47% <88.88%> (+9.47%) ⬆️
cli/run/getConfigPath.ts 89.47% <89.47%> (ø)
cli/run/watch.ts 84.93% <93.02%> (+40.93%) ⬆️
cli/run/loadConfigFile.ts 95.55% <95.34%> (+11.55%) ⬆️
cli/run/commandPlugins.ts 96.87% <96.87%> (ø)
cli/run/index.ts 100.00% <100.00%> (+3.52%) ⬆️
cli/run/loadConfigFromCommand.ts 100.00% <100.00%> (ø)
cli/run/resetScreen.ts 100.00% <100.00%> (+37.50%) ⬆️
src/utils/mergeOptions.ts 100.00% <100.00%> (ø)
src/watch/watch.ts 99.00% <100.00%> (+0.96%) ⬆️
... and 6 more

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 618e7f6...b906590. Read the comment docs.

@lukastaegert lukastaegert force-pushed the load-untranspiled-config-file branch 5 times, most recently from 3a23845 to 9acf722 Compare March 16, 2020 13:36
@lukastaegert lukastaegert changed the base branch from master to render-dynamic-import-hook March 23, 2020 11:25
@lukastaegert lukastaegert force-pushed the render-dynamic-import-hook branch 3 times, most recently from 94c2955 to aad9f32 Compare March 23, 2020 15:58
@lukastaegert lukastaegert force-pushed the load-untranspiled-config-file branch 2 times, most recently from 03d9fac to a7fe879 Compare March 23, 2020 16:11
@lukastaegert lukastaegert marked this pull request as ready for review March 23, 2020 16:13
@lukastaegert lukastaegert changed the base branch from render-dynamic-import-hook to master March 24, 2020 15:37
@lukastaegert lukastaegert force-pushed the load-untranspiled-config-file branch 6 times, most recently from 47d86be to 944fb74 Compare March 29, 2020 16:13
@lukastaegert lukastaegert force-pushed the load-untranspiled-config-file branch 7 times, most recently from ccd3ceb to ff0e2e4 Compare March 29, 2020 19:38
@lukastaegert lukastaegert merged commit 7eea04a into master Mar 29, 2020
@lukastaegert lukastaegert deleted the load-untranspiled-config-file branch March 29, 2020 20:02
@brettz9
Copy link

brettz9 commented Apr 15, 2020

With the latest Rollup release, despite this merge, with /Users/brett/rollup-plugin-filesize/rollup.config.js:

import filesize from "./dist/index-esm.js";

...and /Users/brett/rollup-plugin-filesize/dist/index-esm.js:

console.log(__dirname);

Expected

/Users/brett/rollup-plugin-filesize/dist/

Actual

/Users/brett/rollup-plugin-filesize/

brettz9 added a commit to brettz9/rollup-plugin-filesize that referenced this pull request Apr 15, 2020
1. Fix for __dirname? rollup/rollup#3445
2. Resume using `filesize` again in Rollup config; tests/add coverage?

feat: replace `render` with `reporter` array

BREAKING CHANGE:

Removes `render`.

Also adds sourcemaps and bumps some devDeps and drops unneeded ones
@lukastaegert
Copy link
Member Author

Please read the docs, you will only get an untranspiled config if you rename your config with an .mjs in Node 13 or .a cjs extension. Also I am not sure you can use __dirname in a native Node ES module. https://rollupjs.org/guide/en/#using-untranspiled-config-files

@brettz9
Copy link

brettz9 commented Apr 15, 2020

Ah, my apologies, thank you!

@brettz9
Copy link

brettz9 commented Apr 15, 2020

... you will only get an untranspiled config if you rename your config with an .mjs in Node 13 or .a cjs extension

and from the docs:

By default, Rollup will expect config files to be ES modules and bundle and transpile them and their relative imports to CommonJS before requiring them.

But in my example, my relative imports are ESM, so shouldn't that work? (And I am on Node 10, FWIW.) I'm sorry to belabor this if I'm the one missing something, but I had taken a look at the docs, but was just unclear (and still am).

Re: __dirname, yes, you are right, it is not to be supported (per https://nodejs.org/dist/latest-v12.x/docs/api/esm.html#esm_no_require_exports_module_exports_filename_dirname ), but I get the same results with import.meta.url.

@lukastaegert
Copy link
Member Author

and bundle and transpile them

This is what breaks import.meta.url and __dirname. Maybe this side-effect should be stated explicitly. A bundle of course has the __dirname of the bundled file. If you are stuck on Node 10, your only option for correct __dirname in relative imports is to switch to .cjs.

@brettz9
Copy link

brettz9 commented Apr 15, 2020

I would understand the bundle having the __dirname or import.meta.url of the bundled file (being within dist where it is output). But these end up instead being the Rollup config file path--i.e., /Users/brett/rollup-plugin-filesize/rollup.config.js is what import.meta.url gives.

And even when testing on Node 12 with the rollup.config.mjs file, I am getting the same result--import.meta.url is showing the path of the rollup.config.mjs config file--not the path of the source file in which the logging statement is present (nor of the destination file).

@brettz9
Copy link

brettz9 commented Apr 15, 2020

FWIW, with rollup.config.cjs (and its change to point to the dist file, built with @babel/plugin-syntax-import-meta), things are working as expected for me, however.

@lukastaegert
Copy link
Member Author

And mjs is only working natively in Node 13, as stated in the docs. Otherwise the config file is the bundled file, hence everything points there.

@brettz9
Copy link

brettz9 commented Apr 15, 2020

Ah, ok, I see. Re: the config file itself being the bundled file, I see now how that can make sense.

Re: Node 13, my apologies, and thank you very much for your patience and help.

jbedard added a commit to jbedard/rules_nodejs that referenced this pull request May 13, 2020
alexeagle pushed a commit to bazelbuild/rules_nodejs that referenced this pull request May 13, 2020
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.

Cannot import pure ECMAScript modules in rollup.config.js __dirname and import.meta in plugins
3 participants