Skip to content

Commit

Permalink
feat(pluginutils): add native node es modules support
Browse files Browse the repository at this point in the history
Ref #412 #413

Enable native node es modules via "exports" field in package.json.
Added custom plugin to generate nested package.json with `{"type": "module"}`
to prevent mjs extension usage.
  • Loading branch information
TrySound committed May 26, 2020
1 parent 867c699 commit 8103ad4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 7 additions & 2 deletions packages/pluginutils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bugs": {
"url": "https://github.com/rollup/plugins/issues"
},
"main": "dist/index.js",
"main": "./dist/cjs/index.js",
"engines": {
"node": ">= 8.0.0"
},
Expand Down Expand Up @@ -75,12 +75,17 @@
"!**/types.ts"
]
},
"module": "dist/index.es.js",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/es/index.js"
},
"module": "./dist/es/index.js",
"nyc": {
"extension": [
".js",
".ts"
]
},
"type": "commonjs",
"types": "types/index.d.ts"
}
24 changes: 15 additions & 9 deletions packages/pluginutils/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import typescript from '@rollup/plugin-typescript';

import pkg from './package.json';

function emitModulePackageFile() {
return {
name: 'emit-module-package-file',
generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'package.json',
source: `{"type":"module"}`
});
}
};
}

export default {
input: 'src/index.ts',
plugins: [
Expand All @@ -13,14 +26,7 @@ export default {
],
external: Object.keys(pkg.dependencies).concat('path', 'util'),
output: [
{
format: 'cjs',
file: pkg.main,
exports: 'named'
},
{
format: 'es',
file: pkg.module
}
{ format: 'cjs', file: pkg.main, exports: 'named' },
{ file: pkg.module, format: 'es', plugins: [emitModulePackageFile()] }
]
};

0 comments on commit 8103ad4

Please sign in to comment.