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

feat(node-resolve): add native node es modules support #413

Merged
merged 1 commit into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/node-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Rich Harris <richard.a.harris@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/node-resolve/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"main": "./dist/cjs/index.js",
shellscape marked this conversation as resolved.
Show resolved Hide resolved
"engines": {
"node": ">= 8.0.0"
},
Expand Down Expand Up @@ -75,6 +75,11 @@
"!**/types.ts"
]
},
"module": "dist/index.es.js",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/es/index.js"
},
"module": "./dist/es/index.js",
"type": "commonjs",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this say commonjs instead of module? When I was reading https://nodejs.org/api/esm.html#esm_conditional_exports I saw that they use type module together with exports require and import.

Copy link
Member Author

Choose a reason for hiding this comment

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

For modules there is package.json with {type:module} in dist/es

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. Is there a reason a nested package.json approach is preferred to declaring everything in the top-level package.json?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not all tools are ready to use native es modules. Some may just not work. So we go with commonjs default and enable module mode where necessary.

Copy link
Member

Choose a reason for hiding this comment

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

I think type: commonjs in a package.json is not really necessary as this is the default anyway: https://nodejs.org/api/esm.html#esm_package_json_type_field

But we can still keep it for clarity's sake.

"types": "types/index.d.ts"
}
6 changes: 4 additions & 2 deletions packages/node-resolve/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import babel from 'rollup-plugin-babel';
import json from '@rollup/plugin-json';

import { emitModulePackageFile } from '../../shared/rollup.config';

import pkg from './package.json';

export default {
Expand All @@ -22,7 +24,7 @@ export default {
],
external: Object.keys(pkg.dependencies).concat(['fs', 'path', 'os', 'util']),
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' }
{ file: pkg.main, format: 'cjs', exports: 'named' },
{ file: pkg.module, format: 'es', plugins: [emitModulePackageFile()] }
]
};