From a7f7265727222da4f7af103454fc800416f0fd9a Mon Sep 17 00:00:00 2001 From: Andreas Heissenberger Date: Wed, 5 May 2021 21:47:31 +0200 Subject: [PATCH] wenn package.json "type":"module" switch default type to ".cjs" for CommonJS build (#802) Co-authored-by: Jason Miller --- README.md | 18 +++++++++++++++--- src/index.js | 11 ++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f09c0491..83c4343f 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ Neither require any options, but you can tailor things to suit your needs a bit > > Read more about [How Microbundle decides which dependencies to bundle](https://github.com/developit/microbundle/wiki/How-Microbundle-decides-which-dependencies-to-bundle), including some example configurations. -### Configuration +### Specifying filenames in package.json Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to determine which of your JavaScript files to start bundling from (your "entry module"). The filenames and paths for generated bundles in each format are defined by the `main`, `umd:main`, `module` and `exports` properties in your `package.json`. @@ -178,6 +178,20 @@ For more information about the meaning of the different properties, refer to the For UMD builds, microbundle will use a camelCase version of the `name` field in your `package.json` as export name. Alternatively, this can be explicitly by adding an `"amdName"` key in your `package.json`, or passing the `--name` command line argument. +### Usage with `{"type":"module"}` in `package.json` + +Node.js 12.16+ adds a new "ES Module package", which can be enabled by adding `{"type":"module"}` to your package.json. +This property [changes the default source type](https://nodejs.org/api/packages.html#packages_determining_module_system) of `.js` files to be ES Modules instead of CommonJS. +When using `{"type":"module"}`, the file extension for CommonJS bundles generated by Microbundle must be changed to `.cjs`: + +``` +{ + "type": "module", + "module": "dist/foo.js", // ES Module bundle + "main": "dist/foo.cjs", // CommonJS bundle +} +``` + ### Additional Configuration Options Config also can be overridded by the [`publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig) property in your `package.json`. @@ -263,8 +277,6 @@ The `--define` option can be used to inject or replace build-time constants when | `microbundle --define API_KEY='abc123'` | `console.log(API_KEY)` | `console.log("abc123")` | | `microbundle --define @assign=Object.assign` | `assign(a, b)` | `Object.assign(a, b)` | - - ### All CLI Options ``` diff --git a/src/index.js b/src/index.js index dd067a45..3a5ea8d9 100644 --- a/src/index.js +++ b/src/index.js @@ -266,13 +266,15 @@ function getMain({ options, entry, format }) { let mainNoExtension = options.output; if (options.multipleEntries) { - let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/) + let name = entry.match( + /([\\/])index(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/, + ) ? mainNoExtension : entry; mainNoExtension = resolve(dirname(mainNoExtension), basename(name)); } mainNoExtension = mainNoExtension.replace( - /(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/, + /(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/, '', ); @@ -288,7 +290,10 @@ function getMain({ options, entry, format }) { (pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js', mainNoExtension, ); - mainsByFormat.cjs = replaceName(pkg['cjs:main'] || 'x.js', mainNoExtension); + mainsByFormat.cjs = replaceName( + pkg['cjs:main'] || (pkg.type && pkg.type === 'module' ? 'x.cjs' : 'x.js'), + mainNoExtension, + ); mainsByFormat.umd = replaceName( pkg['umd:main'] || pkg.unpkg || 'x.umd.js', mainNoExtension,