Skip to content

Commit e50e864

Browse files
committed
fix: Allows naming of outputs individually
1 parent b1a6374 commit e50e864

File tree

8 files changed

+166
-23
lines changed

8 files changed

+166
-23
lines changed

src/index.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,6 @@ async function getEntries({ input, cwd }) {
252252
return entries;
253253
}
254254

255-
function replaceName(filename, name) {
256-
return resolve(
257-
dirname(filename),
258-
name + basename(filename).replace(/^[^.]+/, ''),
259-
);
260-
}
261-
262255
function walk(exports) {
263256
if (typeof exports === 'string') return exports;
264257
return walk(exports['.'] || exports.import || exports.module);
@@ -272,42 +265,42 @@ function getMain({ options, entry, format }) {
272265
return options.output;
273266
}
274267

275-
let mainNoExtension = options.output;
268+
let defaultOutputNoExtension = options.output;
276269
if (options.multipleEntries) {
277270
let name = entry.match(
278271
/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/,
279272
)
280-
? mainNoExtension
273+
? defaultOutputNoExtension
281274
: entry;
282-
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
275+
defaultOutputNoExtension = resolve(
276+
dirname(defaultOutputNoExtension),
277+
basename(name),
278+
);
283279
}
284-
mainNoExtension = mainNoExtension.replace(
280+
defaultOutputNoExtension = defaultOutputNoExtension.replace(
285281
/(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/,
286282
'',
287283
);
288284

289285
const mainsByFormat = {};
290286

291-
mainsByFormat.es = replaceName(
287+
mainsByFormat.es = resolve(
292288
pkg.module && !pkg.module.match(/src\//)
293289
? pkg.module
294-
: pkg['jsnext:main'] || 'x.esm.js',
295-
mainNoExtension,
290+
: pkg['jsnext:main'] || `${defaultOutputNoExtension}.esm.js`,
296291
);
297-
mainsByFormat.modern = replaceName(
292+
mainsByFormat.modern = resolve(
298293
(pkg.exports && walk(pkg.exports)) ||
299294
(pkg.syntax && pkg.syntax.esmodules) ||
300295
pkg.esmodule ||
301-
'x.modern.js',
302-
mainNoExtension,
296+
`${defaultOutputNoExtension}.modern.js`,
303297
);
304-
mainsByFormat.cjs = replaceName(
305-
pkg['cjs:main'] || (pkg.type && pkg.type === 'module' ? 'x.cjs' : 'x.js'),
306-
mainNoExtension,
298+
mainsByFormat.cjs = resolve(
299+
pkg['cjs:main'] ||
300+
`${defaultOutputNoExtension}.${pkg.type === 'module' ? 'c' : ''}js`,
307301
);
308-
mainsByFormat.umd = replaceName(
309-
pkg['umd:main'] || pkg.unpkg || 'x.umd.js',
310-
mainNoExtension,
302+
mainsByFormat.umd = resolve(
303+
pkg['umd:main'] || pkg.unpkg || `${defaultOutputNoExtension}.umd.js`,
311304
);
312305

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

test/__snapshots__/index.test.js.snap

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,122 @@ exports[`fixtures build custom-babelrc with microbundle 5`] = `
14171417
"
14181418
`;
14191419
1420+
exports[`fixtures build custom-output with microbundle 1`] = `
1421+
"Used script: microbundle -f cjs,es,umd,modern
1422+
1423+
Directory tree:
1424+
1425+
custom-output
1426+
dist
1427+
custom-name.js
1428+
custom-name.js.map
1429+
index.esm.js
1430+
index.esm.js.map
1431+
index.js
1432+
index.js.map
1433+
index.umd.js
1434+
index.umd.js.map
1435+
package.json
1436+
src
1437+
index.js
1438+
two.js
1439+
1440+
1441+
Build \\"customOutput\\" to dist:
1442+
187 B: index.js.gz
1443+
138 B: index.js.br
1444+
188 B: index.esm.js.gz
1445+
139 B: index.esm.js.br
1446+
274 B: index.umd.js.gz
1447+
203 B: index.umd.js.br
1448+
113 B: custom-name.js.gz
1449+
92 B: custom-name.js.br"
1450+
`;
1451+
1452+
exports[`fixtures build custom-output with microbundle 2`] = `8`;
1453+
1454+
exports[`fixtures build custom-output with microbundle 3`] = `
1455+
"async function n(...n){return n.reduce((n,t)=>n+t,0)}async function t(...t){return[await n(...t),await n(...t)]}export default t;
1456+
//# sourceMappingURL=custom-name.js.map
1457+
"
1458+
`;
1459+
1460+
exports[`fixtures build custom-output with microbundle 4`] = `
1461+
"var r=function(){try{var r=arguments;return Promise.resolve([].slice.call(r).reduce(function(r,e){return r+e},0))}catch(r){return Promise.reject(r)}};export default function(){try{var e=arguments,t=[].slice.call(e);return Promise.resolve(r.apply(void 0,t)).then(function(e){return Promise.resolve(r.apply(void 0,t)).then(function(r){return[e,r]})})}catch(r){return Promise.reject(r)}}
1462+
//# sourceMappingURL=index.esm.js.map
1463+
"
1464+
`;
1465+
1466+
exports[`fixtures build custom-output with microbundle 5`] = `
1467+
"var r=function(){try{var r=arguments;return Promise.resolve([].slice.call(r).reduce(function(r,e){return r+e},0))}catch(r){return Promise.reject(r)}};module.exports=function(){try{var e=arguments,t=[].slice.call(e);return Promise.resolve(r.apply(void 0,t)).then(function(e){return Promise.resolve(r.apply(void 0,t)).then(function(r){return[e,r]})})}catch(r){return Promise.reject(r)}};
1468+
//# sourceMappingURL=index.js.map
1469+
"
1470+
`;
1471+
1472+
exports[`fixtures build custom-output with microbundle 6`] = `
1473+
"!function(e,t){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=t():\\"function\\"==typeof define&&define.amd?define(t):(e||self).customOutput=t()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,t){return e+t},0))}catch(e){return Promise.reject(e)}};return function(){try{var t=arguments,r=[].slice.call(t);return Promise.resolve(e.apply(void 0,r)).then(function(t){return Promise.resolve(e.apply(void 0,r)).then(function(e){return[t,e]})})}catch(e){return Promise.reject(e)}}});
1474+
//# sourceMappingURL=index.umd.js.map
1475+
"
1476+
`;
1477+
1478+
exports[`fixtures build custom-outputs with microbundle 1`] = `
1479+
"Used script: microbundle -f cjs,es,umd,modern
1480+
1481+
Directory tree:
1482+
1483+
custom-outputs
1484+
dist
1485+
custom-exports.js
1486+
custom-exports.js.map
1487+
custom-main.js
1488+
custom-main.js.map
1489+
custom-module.js
1490+
custom-module.js.map
1491+
custom-unpkg.js
1492+
custom-unpkg.js.map
1493+
package.json
1494+
src
1495+
index.js
1496+
two.js
1497+
1498+
1499+
Build \\"customOutputs\\" to dist:
1500+
187 B: custom-main.js.gz
1501+
138 B: custom-main.js.br
1502+
188 B: custom-module.js.gz
1503+
139 B: custom-module.js.br
1504+
273 B: custom-unpkg.js.gz
1505+
203 B: custom-unpkg.js.br
1506+
113 B: custom-exports.js.gz
1507+
92 B: custom-exports.js.br"
1508+
`;
1509+
1510+
exports[`fixtures build custom-outputs with microbundle 2`] = `8`;
1511+
1512+
exports[`fixtures build custom-outputs with microbundle 3`] = `
1513+
"async function n(...n){return n.reduce((n,t)=>n+t,0)}async function t(...t){return[await n(...t),await n(...t)]}export default t;
1514+
//# sourceMappingURL=custom-exports.js.map
1515+
"
1516+
`;
1517+
1518+
exports[`fixtures build custom-outputs with microbundle 4`] = `
1519+
"var r=function(){try{var r=arguments;return Promise.resolve([].slice.call(r).reduce(function(r,e){return r+e},0))}catch(r){return Promise.reject(r)}};module.exports=function(){try{var e=arguments,t=[].slice.call(e);return Promise.resolve(r.apply(void 0,t)).then(function(e){return Promise.resolve(r.apply(void 0,t)).then(function(r){return[e,r]})})}catch(r){return Promise.reject(r)}};
1520+
//# sourceMappingURL=custom-main.js.map
1521+
"
1522+
`;
1523+
1524+
exports[`fixtures build custom-outputs with microbundle 5`] = `
1525+
"var r=function(){try{var r=arguments;return Promise.resolve([].slice.call(r).reduce(function(r,e){return r+e},0))}catch(r){return Promise.reject(r)}};export default function(){try{var e=arguments,t=[].slice.call(e);return Promise.resolve(r.apply(void 0,t)).then(function(e){return Promise.resolve(r.apply(void 0,t)).then(function(r){return[e,r]})})}catch(r){return Promise.reject(r)}}
1526+
//# sourceMappingURL=custom-module.js.map
1527+
"
1528+
`;
1529+
1530+
exports[`fixtures build custom-outputs with microbundle 6`] = `
1531+
"!function(e,t){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=t():\\"function\\"==typeof define&&define.amd?define(t):(e||self).customOutputs=t()}(this,function(){var e=function(){try{var e=arguments;return Promise.resolve([].slice.call(e).reduce(function(e,t){return e+t},0))}catch(e){return Promise.reject(e)}};return function(){try{var t=arguments,r=[].slice.call(t);return Promise.resolve(e.apply(void 0,r)).then(function(t){return Promise.resolve(e.apply(void 0,r)).then(function(e){return[t,e]})})}catch(e){return Promise.reject(e)}}});
1532+
//# sourceMappingURL=custom-unpkg.js.map
1533+
"
1534+
`;
1535+
14201536
exports[`fixtures build custom-source with microbundle 1`] = `
14211537
"Used script: microbundle
14221538
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "custom-output",
3+
"main": "./dist/index.js",
4+
"exports": "./dist/custom-name.js",
5+
"scripts": {
6+
"build": "microbundle -f cjs,es,umd,modern"
7+
}
8+
}
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "custom-outputs",
3+
"main": "./dist/custom-main.js",
4+
"module": "./dist/custom-module.js",
5+
"unpkg": "./dist/custom-unpkg.js",
6+
"exports": "./dist/custom-exports.js",
7+
"scripts": {
8+
"build": "microbundle -f cjs,es,umd,modern"
9+
}
10+
}
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)