Skip to content

Commit f4ab33d

Browse files
authored
feat: add --pkg-main option (#658)
1 parent f3749f2 commit f4ab33d

File tree

7 files changed

+109
-40
lines changed

7 files changed

+109
-40
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ Microbundle uses the fields from your `package.json` to figure out where it shou
178178
}
179179
```
180180

181+
### Building a single bundle with a fixed output name
182+
183+
By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:
184+
185+
```bash
186+
microbundle -i lib/main.js -o dist/bundle.js --no-pkg-main -f umd
187+
```
188+
181189
### Mangling Properties
182190

183191
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:
@@ -212,6 +220,7 @@ Options
212220
-o, --output Directory to place build files into
213221
-f, --format Only build specified formats (any of modern,es,cjs,umd or iife) (default modern,es,cjs,umd)
214222
-w, --watch Rebuilds on any change (default false)
223+
--pkg-main Outputs files analog to package.json main entries (default true)
215224
--target Specify your target environment (node or web) (default web)
216225
--external Specify external dependencies, or 'none' (default peerDependencies and dependencies in package.json)
217226
--globals Specify globals dependencies, or 'none'

src/index.js

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,54 @@ async function getEntries({ input, cwd }) {
392392
return entries;
393393
}
394394

395+
function replaceName(filename, name) {
396+
return resolve(
397+
dirname(filename),
398+
name + basename(filename).replace(/^[^.]+/, ''),
399+
);
400+
}
401+
402+
function getMain({ options, entry, format }) {
403+
const { pkg } = options;
404+
const pkgMain = options['pkg-main'];
405+
406+
if (!pkgMain) {
407+
return options.output;
408+
}
409+
410+
let mainNoExtension = options.output;
411+
if (options.multipleEntries) {
412+
let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/)
413+
? mainNoExtension
414+
: entry;
415+
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
416+
}
417+
mainNoExtension = mainNoExtension.replace(
418+
/(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/,
419+
'',
420+
);
421+
422+
const mainsByFormat = {};
423+
424+
mainsByFormat.es = replaceName(
425+
pkg.module && !pkg.module.match(/src\//)
426+
? pkg.module
427+
: pkg['jsnext:main'] || 'x.esm.js',
428+
mainNoExtension,
429+
);
430+
mainsByFormat.modern = replaceName(
431+
(pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js',
432+
mainNoExtension,
433+
);
434+
mainsByFormat.cjs = replaceName(pkg['cjs:main'] || 'x.js', mainNoExtension);
435+
mainsByFormat.umd = replaceName(
436+
pkg['umd:main'] || 'x.umd.js',
437+
mainNoExtension,
438+
);
439+
440+
return mainsByFormat[format] || mainsByFormat.cjs;
441+
}
442+
395443
// shebang cache map because the transform only gets run once
396444
const shebang = {};
397445

@@ -442,38 +490,6 @@ function createConfig(options, entry, format, writeMeta) {
442490
);
443491
}
444492

445-
function replaceName(filename, name) {
446-
return resolve(
447-
dirname(filename),
448-
name + basename(filename).replace(/^[^.]+/, ''),
449-
);
450-
}
451-
452-
let mainNoExtension = options.output;
453-
if (options.multipleEntries) {
454-
let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/)
455-
? mainNoExtension
456-
: entry;
457-
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
458-
}
459-
mainNoExtension = mainNoExtension.replace(
460-
/(\.(umd|cjs|es|m))?\.(mjs|[tj]sx?)$/,
461-
'',
462-
);
463-
464-
let moduleMain = replaceName(
465-
pkg.module && !pkg.module.match(/src\//)
466-
? pkg.module
467-
: pkg['jsnext:main'] || 'x.esm.js',
468-
mainNoExtension,
469-
);
470-
let modernMain = replaceName(
471-
(pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js',
472-
mainNoExtension,
473-
);
474-
let cjsMain = replaceName(pkg['cjs:main'] || 'x.js', mainNoExtension);
475-
let umdMain = replaceName(pkg['umd:main'] || 'x.umd.js', mainNoExtension);
476-
477493
const modern = format === 'modern';
478494

479495
// let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));
@@ -698,14 +714,7 @@ function createConfig(options, entry, format, writeMeta) {
698714
},
699715
format: modern ? 'es' : format,
700716
name: options.name,
701-
file: resolve(
702-
options.cwd,
703-
{
704-
modern: modernMain,
705-
es: moduleMain,
706-
umd: umdMain,
707-
}[format] || cjsMain,
708-
),
717+
file: resolve(options.cwd, getMain({ options, entry, format })),
709718
},
710719
};
711720

src/prog.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default handler => {
2828
DEFAULT_FORMATS,
2929
)
3030
.option('--watch, -w', 'Rebuilds on any change', false)
31+
.option(
32+
'--pkg-main',
33+
'Outputs files analog to package.json main entries',
34+
true,
35+
)
3136
.option('--target', 'Specify your target environment (node or web)', 'web')
3237
.option('--external', `Specify external dependencies, or 'none'`)
3338
.option('--globals', `Specify globals dependencies, or 'none'`)

test/__snapshots__/index.test.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,38 @@ exports[`fixtures build basic-no-compress with microbundle 5`] = `
731731
"
732732
`;
733733
734+
exports[`fixtures build basic-no-pkg-main with microbundle 1`] = `
735+
"Used script: microbundle --pkg-main false
736+
737+
Directory tree:
738+
739+
basic-no-pkg-main
740+
dist
741+
basic-no-pkg-main.js
742+
basic-no-pkg-main.js.map
743+
package.json
744+
src
745+
index.js
746+
two.js
747+
748+
749+
Build \\"basicNoPkgMain\\" to dist:
750+
187 B: basic-no-pkg-main.js.gz
751+
138 B: basic-no-pkg-main.js.br
752+
188 B: basic-no-pkg-main.js.gz
753+
139 B: basic-no-pkg-main.js.br
754+
279 B: basic-no-pkg-main.js.gz
755+
229 B: basic-no-pkg-main.js.br"
756+
`;
757+
758+
exports[`fixtures build basic-no-pkg-main with microbundle 2`] = `2`;
759+
760+
exports[`fixtures build basic-no-pkg-main with microbundle 3`] = `
761+
"!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)}}});
762+
//# sourceMappingURL=basic-no-pkg-main.js.map
763+
"
764+
`;
765+
734766
exports[`fixtures build basic-ts with microbundle 1`] = `
735767
"Used script: microbundle
736768
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "basic-no-pkg-main",
3+
"scripts": {
4+
"build": "microbundle --pkg-main false"
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { two } from './two';
2+
3+
export default async function(...args) {
4+
return [await two(...args), await two(...args)];
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function two(...args) {
2+
return args.reduce((total, value) => total + value, 0);
3+
}

0 commit comments

Comments
 (0)