Skip to content

Commit e72377a

Browse files
authored
fix: mains by format fallbacks (#964)
1 parent fd4ea1a commit e72377a

File tree

9 files changed

+159
-6
lines changed

9 files changed

+159
-6
lines changed

.changeset/old-dodos-laugh.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+
Fixes filename generation for es & modern outputs. Both 'jsnext:main' and 'esmodule' were incorrectly ignored.

src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,15 @@ function getMain({ options, entry, format }) {
297297
mainsByFormat.es = replaceName(
298298
pkg.module && !pkg.module.match(/src\//)
299299
? pkg.module
300-
: pkg['jsnext:main'] || pkgTypeModule
301-
? 'x.esm.js'
302-
: 'x.esm.mjs',
300+
: pkg['jsnext:main'] || (pkgTypeModule ? 'x.esm.js' : 'x.esm.mjs'),
303301
mainNoExtension,
304302
);
305303

306304
mainsByFormat.modern = replaceName(
307305
(pkg.exports && walk(pkg.exports, pkgTypeModule)) ||
308306
(pkg.syntax && pkg.syntax.esmodules) ||
309307
pkg.esmodule ||
310-
pkgTypeModule
311-
? 'x.modern.js'
312-
: 'x.modern.mjs',
308+
(pkgTypeModule ? 'x.modern.js' : 'x.modern.mjs'),
313309
mainNoExtension,
314310
);
315311
mainsByFormat.cjs = replaceName(

test/__snapshots__/index.test.js.snap

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,122 @@ exports[`fixtures build custom-babelrc with microbundle 5`] = `
15111511
"
15121512
`;
15131513
1514+
exports[`fixtures build custom-outputs with microbundle 1`] = `
1515+
"Used script: microbundle -f modern,es,umd,cjs
1516+
1517+
Directory tree:
1518+
1519+
custom-outputs
1520+
dist
1521+
custom-outputs.exportsx.mjs
1522+
custom-outputs.exportsx.mjs.map
1523+
custom-outputs.js
1524+
custom-outputs.js.map
1525+
custom-outputs.modulex.mjs
1526+
custom-outputs.modulex.mjs.map
1527+
custom-outputs.umdx.js
1528+
custom-outputs.umdx.js.map
1529+
package.json
1530+
src
1531+
index.js
1532+
two.js
1533+
1534+
1535+
Build \\"custom-outputs\\" to dist:
1536+
187 B: custom-outputs.js.gz
1537+
138 B: custom-outputs.js.br
1538+
113 B: custom-outputs.exportsx.mjs.gz
1539+
92 B: custom-outputs.exportsx.mjs.br
1540+
188 B: custom-outputs.modulex.mjs.gz
1541+
139 B: custom-outputs.modulex.mjs.br
1542+
273 B: custom-outputs.umdx.js.gz
1543+
203 B: custom-outputs.umdx.js.br"
1544+
`;
1545+
1546+
exports[`fixtures build custom-outputs with microbundle 2`] = `8`;
1547+
1548+
exports[`fixtures build custom-outputs with microbundle 3`] = `
1549+
"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;
1550+
//# sourceMappingURL=custom-outputs.exportsx.mjs.map
1551+
"
1552+
`;
1553+
1554+
exports[`fixtures build custom-outputs with microbundle 4`] = `
1555+
"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)}};
1556+
//# sourceMappingURL=custom-outputs.js.map
1557+
"
1558+
`;
1559+
1560+
exports[`fixtures build custom-outputs with microbundle 5`] = `
1561+
"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)}}
1562+
//# sourceMappingURL=custom-outputs.modulex.mjs.map
1563+
"
1564+
`;
1565+
1566+
exports[`fixtures build custom-outputs with microbundle 6`] = `
1567+
"!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)}}});
1568+
//# sourceMappingURL=custom-outputs.umdx.js.map
1569+
"
1570+
`;
1571+
1572+
exports[`fixtures build custom-outputs-alt with microbundle 1`] = `
1573+
"Used script: microbundle -f modern,es,umd,cjs
1574+
1575+
Directory tree:
1576+
1577+
custom-outputs-alt
1578+
dist
1579+
custom-outputs-alt.cjsmain.js
1580+
custom-outputs-alt.cjsmain.js.map
1581+
custom-outputs-alt.esmodule.mjs
1582+
custom-outputs-alt.esmodule.mjs.map
1583+
custom-outputs-alt.jsnextmain.mjs
1584+
custom-outputs-alt.jsnextmain.mjs.map
1585+
custom-outputs-alt.unpkg.js
1586+
custom-outputs-alt.unpkg.js.map
1587+
package.json
1588+
src
1589+
index.js
1590+
two.js
1591+
1592+
1593+
Build \\"custom-outputs-alt\\" to dist:
1594+
187 B: custom-outputs-alt.cjsmain.js.gz
1595+
138 B: custom-outputs-alt.cjsmain.js.br
1596+
113 B: custom-outputs-alt.esmodule.mjs.gz
1597+
92 B: custom-outputs-alt.esmodule.mjs.br
1598+
188 B: custom-outputs-alt.jsnextmain.mjs.gz
1599+
139 B: custom-outputs-alt.jsnextmain.mjs.br
1600+
277 B: custom-outputs-alt.unpkg.js.gz
1601+
226 B: custom-outputs-alt.unpkg.js.br"
1602+
`;
1603+
1604+
exports[`fixtures build custom-outputs-alt with microbundle 2`] = `8`;
1605+
1606+
exports[`fixtures build custom-outputs-alt with microbundle 3`] = `
1607+
"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)}};
1608+
//# sourceMappingURL=custom-outputs-alt.cjsmain.js.map
1609+
"
1610+
`;
1611+
1612+
exports[`fixtures build custom-outputs-alt with microbundle 4`] = `
1613+
"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;
1614+
//# sourceMappingURL=custom-outputs-alt.esmodule.mjs.map
1615+
"
1616+
`;
1617+
1618+
exports[`fixtures build custom-outputs-alt with microbundle 5`] = `
1619+
"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)}}
1620+
//# sourceMappingURL=custom-outputs-alt.jsnextmain.mjs.map
1621+
"
1622+
`;
1623+
1624+
exports[`fixtures build custom-outputs-alt with microbundle 6`] = `
1625+
"!function(e,t){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=t():\\"function\\"==typeof define&&define.amd?define(t):(e||self).customOutputsAlt=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)}}});
1626+
//# sourceMappingURL=custom-outputs-alt.unpkg.js.map
1627+
"
1628+
`;
1629+
15141630
exports[`fixtures build custom-source with microbundle 1`] = `
15151631
"Used script: microbundle
15161632
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "custom-outputs-alt",
3+
"cjs:main": "dist/custom-outputs-alt.cjsmain.js",
4+
"unpkg": "dist/custom-outputs-alt.unpkg.js",
5+
"jsnext:main": "dist/custom-outputs-alt.jsnextmain.mjs",
6+
"esmodule": "dist/custom-outputs-alt.esmodule.mjs",
7+
"scripts": {
8+
"build": "microbundle -f modern,es,umd,cjs"
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+
}
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-outputs.js",
4+
"umd:main": "dist/custom-outputs.umdx.js",
5+
"module": "dist/custom-outputs.modulex.mjs",
6+
"exports": "./dist/custom-outputs.exportsx.mjs",
7+
"scripts": {
8+
"build": "microbundle -f modern,es,umd,cjs"
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)