Skip to content

Commit

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

Ref rollup/plugins#412 rollup/plugins#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.

* Export emitModulePackageFile from shared config

* Bundle estree-walker

* Swap options
  • Loading branch information
TrySound committed Jul 13, 2021
1 parent 8444e1d commit 925b68b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 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"
}
17 changes: 7 additions & 10 deletions packages/pluginutils/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { builtinModules } from 'module';

import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';

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

import pkg from './package.json';

export default {
Expand All @@ -11,16 +15,9 @@ export default {
commonjs({ include: '../../node_modules/.pnpm/registry.npmjs.org/**' }),
typescript({ include: '**/*.{ts,js}', module: 'esnext' })
],
external: Object.keys(pkg.dependencies).concat('path', 'util'),
external: [...builtinModules, 'picomatch'],
output: [
{
format: 'cjs',
file: pkg.main,
exports: 'named'
},
{
format: 'es',
file: pkg.module
}
{ file: pkg.main, format: 'cjs', exports: 'named' },
{ file: pkg.module, format: 'es', plugins: [emitModulePackageFile()] }
]
};
13 changes: 13 additions & 0 deletions shared/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ export function createConfig(pkg) {
]
};
}

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

0 comments on commit 925b68b

Please sign in to comment.