Skip to content

Commit

Permalink
feat: add --pkg-main option (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
katywings committed Jun 14, 2020
1 parent f3749f2 commit f4ab33d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 40 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ Microbundle uses the fields from your `package.json` to figure out where it shou
}
```

### Building a single bundle with a fixed output name

By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:

```bash
microbundle -i lib/main.js -o dist/bundle.js --no-pkg-main -f umd
```

### Mangling Properties

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:
Expand Down Expand Up @@ -212,6 +220,7 @@ Options
-o, --output Directory to place build files into
-f, --format Only build specified formats (any of modern,es,cjs,umd or iife) (default modern,es,cjs,umd)
-w, --watch Rebuilds on any change (default false)
--pkg-main Outputs files analog to package.json main entries (default true)
--target Specify your target environment (node or web) (default web)
--external Specify external dependencies, or 'none' (default peerDependencies and dependencies in package.json)
--globals Specify globals dependencies, or 'none'
Expand Down
89 changes: 49 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,54 @@ async function getEntries({ input, cwd }) {
return entries;
}

function replaceName(filename, name) {
return resolve(
dirname(filename),
name + basename(filename).replace(/^[^.]+/, ''),
);
}

function getMain({ options, entry, format }) {
const { pkg } = options;
const pkgMain = options['pkg-main'];

if (!pkgMain) {
return options.output;
}

let mainNoExtension = options.output;
if (options.multipleEntries) {
let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/)
? mainNoExtension
: entry;
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
}
mainNoExtension = mainNoExtension.replace(
/(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/,
'',
);

const mainsByFormat = {};

mainsByFormat.es = replaceName(
pkg.module && !pkg.module.match(/src\//)
? pkg.module
: pkg['jsnext:main'] || 'x.esm.js',
mainNoExtension,
);
mainsByFormat.modern = replaceName(
(pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js',
mainNoExtension,
);
mainsByFormat.cjs = replaceName(pkg['cjs:main'] || 'x.js', mainNoExtension);
mainsByFormat.umd = replaceName(
pkg['umd:main'] || 'x.umd.js',
mainNoExtension,
);

return mainsByFormat[format] || mainsByFormat.cjs;
}

// shebang cache map because the transform only gets run once
const shebang = {};

Expand Down Expand Up @@ -442,38 +490,6 @@ function createConfig(options, entry, format, writeMeta) {
);
}

function replaceName(filename, name) {
return resolve(
dirname(filename),
name + basename(filename).replace(/^[^.]+/, ''),
);
}

let mainNoExtension = options.output;
if (options.multipleEntries) {
let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/)
? mainNoExtension
: entry;
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
}
mainNoExtension = mainNoExtension.replace(
/(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/,
'',
);

let moduleMain = replaceName(
pkg.module && !pkg.module.match(/src\//)
? pkg.module
: pkg['jsnext:main'] || 'x.esm.js',
mainNoExtension,
);
let modernMain = replaceName(
(pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js',
mainNoExtension,
);
let cjsMain = replaceName(pkg['cjs:main'] || 'x.js', mainNoExtension);
let umdMain = replaceName(pkg['umd:main'] || 'x.umd.js', mainNoExtension);

const modern = format === 'modern';

// let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));
Expand Down Expand Up @@ -698,14 +714,7 @@ function createConfig(options, entry, format, writeMeta) {
},
format: modern ? 'es' : format,
name: options.name,
file: resolve(
options.cwd,
{
modern: modernMain,
es: moduleMain,
umd: umdMain,
}[format] || cjsMain,
),
file: resolve(options.cwd, getMain({ options, entry, format })),
},
};

Expand Down
5 changes: 5 additions & 0 deletions src/prog.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default handler => {
DEFAULT_FORMATS,
)
.option('--watch, -w', 'Rebuilds on any change', false)
.option(
'--pkg-main',
'Outputs files analog to package.json main entries',
true,
)
.option('--target', 'Specify your target environment (node or web)', 'web')
.option('--external', `Specify external dependencies, or 'none'`)
.option('--globals', `Specify globals dependencies, or 'none'`)
Expand Down
32 changes: 32 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,38 @@ exports[`fixtures build basic-no-compress with microbundle 5`] = `
"
`;
exports[`fixtures build basic-no-pkg-main with microbundle 1`] = `
"Used script: microbundle --pkg-main false
Directory tree:
basic-no-pkg-main
dist
basic-no-pkg-main.js
basic-no-pkg-main.js.map
package.json
src
index.js
two.js
Build \\"basicNoPkgMain\\" to dist:
187 B: basic-no-pkg-main.js.gz
138 B: basic-no-pkg-main.js.br
188 B: basic-no-pkg-main.js.gz
139 B: basic-no-pkg-main.js.br
279 B: basic-no-pkg-main.js.gz
229 B: basic-no-pkg-main.js.br"
`;
exports[`fixtures build basic-no-pkg-main with microbundle 2`] = `2`;
exports[`fixtures build basic-no-pkg-main with microbundle 3`] = `
"!function(e,r){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=r():\\"function\\"==typeof define&&define.amd?define(r):(e=e||self).basicNoPkgMain=r()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,r){return e+r},0))}catch(e){return Promise.reject(e)}};return function(){try{var r=arguments,n=[].slice.call(r);return Promise.resolve(e.apply(void 0,n)).then(function(r){return Promise.resolve(e.apply(void 0,n)).then(function(e){return[r,e]})})}catch(e){return Promise.reject(e)}}});
//# sourceMappingURL=basic-no-pkg-main.js.map
"
`;
exports[`fixtures build basic-ts with microbundle 1`] = `
"Used script: microbundle
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/basic-no-pkg-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "basic-no-pkg-main",
"scripts": {
"build": "microbundle --pkg-main false"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/basic-no-pkg-main/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { two } from './two';

export default async function(...args) {
return [await two(...args), await two(...args)];
}
3 changes: 3 additions & 0 deletions test/fixtures/basic-no-pkg-main/src/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function two(...args) {
return args.reduce((total, value) => total + value, 0);
}

0 comments on commit f4ab33d

Please sign in to comment.