Skip to content

Commit 25b73d2

Browse files
cometkimdevelopit
andauthored
feat: enabled config overriding by publishConfig (#834)
* feat: enabled config overriding by publishConfig * add changesets * update README * Update odd-pets-type.md Co-authored-by: Jason Miller <developit@users.noreply.github.com>
1 parent 4f7fbc4 commit 25b73d2

File tree

6 files changed

+121
-36
lines changed

6 files changed

+121
-36
lines changed

.changeset/odd-pets-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'microbundle': patch
3+
---
4+
5+
Add support for configuration overrides using the `publishConfig` package.json field.

README.md

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@
3636

3737
2️⃣ **Set up** your `package.json`:
3838

39-
```js
39+
```jsonc
4040
{
41-
"name": "foo", // your package name
42-
"source": "src/foo.js", // your source code
43-
"main": "dist/foo.js", // where to generate the CommonJS/Node bundle
44-
"exports": "./dist/foo.modern.js", // path to the modern output (see below)
45-
"module": "dist/foo.module.js", // where to generate the ESM bundle
46-
"unpkg": "dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
41+
"name": "foo", // your package name
42+
"type": "module",
43+
"source": "src/foo.js", // your source code
44+
"exports": "./dist/foo.modern.js", // where to generate the modern bundle (see below)
45+
"main": "./dist/foo.cjs", // where to generate the CommonJS bundle
46+
"module": "./dist/foo.module.js", // where to generate the ESM bundle
47+
"unpkg": "./dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
4748
"scripts": {
48-
"build": "microbundle", // compiles "source" to "main"/"module"/"unpkg"
49-
"dev": "microbundle watch" // re-build when source files change
49+
"build": "microbundle", // compiles "source" to "main"/"module"/"unpkg"
50+
"dev": "microbundle watch" // re-build when source files change
5051
}
5152
}
5253
```
@@ -55,11 +56,18 @@
5556

5657
## 💽 Output Formats <a name="formats"></a>
5758

58-
Microbundle produces <code title="ECMAScript Modules (import / export)">esm</code>, <code title="CommonJS (Node-style module.exports)">cjs</code>, <code title="Universal Module Definition (works everywhere)">umd</code> bundles with your code compiled to syntax that works pretty much everywhere. While it's possible to customize the browser or Node versions you wish to support using a [browserslist configuration](https://github.com/browserslist/browserslist#browserslist-), the default setting is optimal and strongly recommended.
59+
Microbundle produces <code title="ECMAScript Modules (import / export)">esm</code>, <code title="CommonJS (Node-style module.exports)">cjs</code>, <code title="Universal Module Definition (works everywhere)">umd</code> bundles with your code compiled to syntax that works pretty much everywhere.
60+
While it's possible to customize the browser or Node versions you wish to support using a [browserslist configuration](https://github.com/browserslist/browserslist#browserslist-), the default setting is optimal and strongly recommended.
5961

6062
## 🤖 Modern Mode <a name="modern"></a>
6163

62-
In addition to the above formats, Microbundle also outputs a `modern` bundle specially designed to work in _all modern browsers_. This bundle preserves most modern JS features when compiling your code, but ensures the result runs in 95% of web browsers without needing to be transpiled. Specifically, it uses [preset-modules](https://github.com/babel/preset-modules) to target the set of browsers that support `<script type="module">` - that allows syntax like async/await, tagged templates, arrow functions, destructured and rest parameters, etc. The result is generally smaller and faster to execute than the `esm` bundle:
64+
In addition to the above formats, Microbundle also outputs a `modern` bundle specially designed to work in _all modern browsers_.
65+
This bundle preserves most modern JS features when compiling your code, but ensures the result runs in 95% of web browsers without needing to be transpiled.
66+
Specifically, it uses Babel's ["bugfixes" mode](https://babeljs.io/blog/2020/03/16/7.9.0#babelpreset-envs-bugfixes-option-11083httpsgithubcombabelbabelpull11083)
67+
(previously known as [preset-modules](https://github.com/babel/preset-modules)) to target the set of browsers that support `<script type="module">` - that allows syntax like async/await, tagged templates, arrow functions, destructured and rest parameters, etc.
68+
The result is generally smaller and faster to execute than the plain `esm` bundle.
69+
70+
Take the following source code for example:
6371

6472
```js
6573
// Our source, "src/make-dom.js":
@@ -135,38 +143,65 @@ The `"exports"` field can also be an object for packages with multiple entry mod
135143

136144
## 📦 Usage & Configuration <a name="usage"></a>
137145

138-
Microbundle includes two commands - `build` (the default) and `watch`. Neither require any options, but you can tailor things to suit your needs a bit if you like.
146+
Microbundle includes two commands - `build` (the default) and `watch`.
147+
Neither require any options, but you can tailor things to suit your needs a bit if you like.
148+
149+
- **`microbundle`** – bundles your code once and exits. (alias: `microbundle build`)
150+
- **`microbundle watch`** – bundles your code, then re-bundles when files change.
139151

140152
> ℹ️ Microbundle automatically determines which dependencies to inline into bundles based on your `package.json`.
141153
>
142154
> 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.
143155
144-
### `microbundle` / `microbundle build`
156+
### Configuration
145157

146-
Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to locate the input file, and the `main`, `umd:main`, `module` and `exports` properties to figure out where it should place each generated bundle:
158+
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").
159+
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`.
147160

148-
149-
```
161+
```jsonc
150162
{
151-
"source": "src/index.js", // input
152-
"main": "dist/foo.js", // CommonJS output bundle
153-
"umd:main": "dist/foo.umd.js", // UMD output bundle
154-
"module": "dist/foo.m.js", // ES Modules output bundle
163+
"source": "src/index.js", // input
164+
"main": "dist/foo.js", // CommonJS output bundle
165+
"umd:main": "dist/foo.umd.js", // UMD output bundle
166+
"module": "dist/foo.m.js", // ES Modules output bundle
155167
"exports": {
156-
"require": "./dist/foo.js", // CommonJS output bundle
168+
"require": "./dist/foo.js", // CommonJS output bundle
157169
"default": "./dist/foo.modern.js", // Modern ES Modules output bundle
158170
}
159-
"types": "dist/foo.d.ts" // TypeScript typings directory
171+
"types": "dist/foo.d.ts" // TypeScript typings directory
160172
}
161173
```
162174

163-
When deciding which bundle to use, Node.js 12+ and webpack 5+ will prefer the `exports` property, while older Node.js releases use the `main` property, and other bundlers prefer the `module` field. For more information about the meaning of the different properties, refer to the [Node.js documentation](https://nodejs.org/api/packages.html#packages_package_entry_points).
175+
When deciding which bundle to use, Node.js 12+ and webpack 5+ will prefer the `exports` property, while older Node.js releases use the `main` property, and other bundlers prefer the `module` field.
176+
For more information about the meaning of the different properties, refer to the [Node.js documentation](https://nodejs.org/api/packages.html#packages_package_entry_points).
177+
178+
For UMD builds, microbundle will use a camelCase version of the `name` field in your `package.json` as export name.
179+
Alternatively, this can be explicitly by adding an `"amdName"` key in your `package.json`, or passing the `--name` command line argument.
164180

165-
For UMD builds, microbundle will use a camelCase version of the `name` field in your `package.json` as export name. This can be customized using an `"amdName"` key in your `package.json` or the `--name` command line argument.
181+
### Additional Configuration Options
166182

167-
### `microbundle watch`
183+
Config also can be overridded by the [`publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig) property in your `package.json`.
168184

169-
Acts just like `microbundle build`, but watches your source files and rebuilds on any change.
185+
```jsonc
186+
{
187+
"main": "src/index.ts", // this would be used in the dev environment (e.g. Jest)
188+
"publishConfig": {
189+
"source": "src/index.js", // input
190+
"main": "dist/my-library.js", // output
191+
},
192+
"scripts": {
193+
"build": "microbundle"
194+
}
195+
}
196+
```
197+
198+
### Building a single bundle with fixed output name
199+
200+
By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:
201+
202+
```bash
203+
microbundle -i lib/main.js -o dist/bundle.js --no-pkg-main -f umd
204+
```
170205

171206
### Using with TypeScript
172207

@@ -204,19 +239,11 @@ This can be customized by passing the command line argument `--css-modules "[nam
204239
| true | import './my-file.css'; | :white_check_mark: |
205240
| true | import './my-file.module.css'; | :white_check_mark: |
206241

207-
### Building a single bundle with a fixed output name
208-
209-
By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:
210-
211-
```bash
212-
microbundle -i lib/main.js -o dist/bundle.js --no-pkg-main -f umd
213-
```
214-
215242
### Mangling Properties
216243

217244
To achieve the smallest possible bundle size, libraries often wish to rename internal object properties or class members to smaller names - transforming `this._internalIdValue` to `this._i`. Microbundle doesn't do this by default, however it can be enabled by creating a `mangle.json` file (or a `"mangle"` property in your package.json). Within that file, you can specify a regular expression pattern to control which properties should be mangled. For example: to mangle all property names beginning an underscore:
218245

219-
```json
246+
```jsonc
220247
{
221248
"mangle": {
222249
"regex": "^_"
@@ -236,6 +263,8 @@ The `--define` option can be used to inject or replace build-time constants when
236263
| `microbundle --define API_KEY='abc123'` | `console.log(API_KEY)` | `console.log("abc123")` |
237264
| `microbundle --define @assign=Object.assign` | `assign(a, b)` | `Object.assign(a, b)` |
238265

266+
267+
239268
### All CLI Options <a name="options"></a>
240269

241270
```

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export default async function microbundle(inputOptions) {
4444
const cwd = options.cwd;
4545

4646
const { hasPackageJson, pkg } = await getConfigFromPkgJson(cwd);
47-
options.pkg = pkg;
47+
options.pkg = {
48+
...pkg,
49+
...pkg.publishConfig,
50+
};
4851

4952
const { finalName, pkgName } = getName({
5053
name: options.name,

test/__snapshots__/index.test.js.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,40 @@ exports[`fixtures build pretty with microbundle 5`] = `
24412441
"
24422442
`;
24432443
2444+
exports[`fixtures build publish-config with microbundle 1`] = `
2445+
"Used script: microbundle -f cjs
2446+
2447+
Directory tree:
2448+
2449+
publish-config
2450+
dist
2451+
bar.js
2452+
bar.js.map
2453+
foo.d.ts
2454+
node_modules
2455+
package.json
2456+
src
2457+
foo.ts
2458+
2459+
2460+
Build \\"publishConfig\\" to dist:
2461+
55 B: bar.js.gz
2462+
33 B: bar.js.br"
2463+
`;
2464+
2465+
exports[`fixtures build publish-config with microbundle 2`] = `3`;
2466+
2467+
exports[`fixtures build publish-config with microbundle 3`] = `
2468+
"exports.foo=function(){return 42};
2469+
//# sourceMappingURL=bar.js.map
2470+
"
2471+
`;
2472+
2473+
exports[`fixtures build publish-config with microbundle 4`] = `
2474+
"export declare function foo(): number;
2475+
"
2476+
`;
2477+
24442478
exports[`fixtures build pure with microbundle 1`] = `
24452479
"Used script: microbundle
24462480
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "publish-config",
3+
"main": "src/foo.ts",
4+
"publishConfig": {
5+
"source": "src/foo.ts",
6+
"main": "dist/bar.js"
7+
},
8+
"scripts": {
9+
"build": "microbundle -f cjs"
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo() {
2+
return 42;
3+
}

0 commit comments

Comments
 (0)