From c5991820e01a773ceefb6c47b5a1e312c1708892 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 1 Jun 2020 17:03:12 +0300 Subject: [PATCH] feat(pluginutils): add native node es modules support (#419) * feat(pluginutils): add native node es modules support Ref https://github.com/rollup/plugins/issues/412 https://github.com/rollup/plugins/pull/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 --- packages/pluginutils/package.json | 9 +++++++-- packages/pluginutils/rollup.config.js | 17 +++++++---------- shared/rollup.config.js | 13 +++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/pluginutils/package.json b/packages/pluginutils/package.json index d7bc11794..2c0db1df9 100644 --- a/packages/pluginutils/package.json +++ b/packages/pluginutils/package.json @@ -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" }, @@ -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" } diff --git a/packages/pluginutils/rollup.config.js b/packages/pluginutils/rollup.config.js index a2ddd2e94..34ef1a3a4 100755 --- a/packages/pluginutils/rollup.config.js +++ b/packages/pluginutils/rollup.config.js @@ -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 { @@ -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()] } ] }; diff --git a/shared/rollup.config.js b/shared/rollup.config.js index 292a84960..4ba7f3f7a 100644 --- a/shared/rollup.config.js +++ b/shared/rollup.config.js @@ -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"}` + }); + } + }; +}