diff --git a/src/index.js b/src/index.js index 47448a08..383e63fd 100644 --- a/src/index.js +++ b/src/index.js @@ -252,13 +252,6 @@ async function getEntries({ input, cwd }) { return entries; } -function replaceName(filename, name) { - return resolve( - dirname(filename), - name + basename(filename).replace(/^[^.]+/, ''), - ); -} - function walk(exports) { if (typeof exports === 'string') return exports; return walk(exports['.'] || exports.import || exports.module); @@ -288,26 +281,23 @@ function getMain({ options, entry, format }) { const mainsByFormat = {}; - mainsByFormat.es = replaceName( + mainsByFormat.es = resolve( pkg.module && !pkg.module.match(/src\//) ? pkg.module - : pkg['jsnext:main'] || 'x.esm.js', - mainNoExtension, + : pkg['jsnext:main'] || `${mainNoExtension}.esm.js`, ); - mainsByFormat.modern = replaceName( + mainsByFormat.modern = resolve( (pkg.exports && walk(pkg.exports)) || (pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || - 'x.modern.js', - mainNoExtension, + `${mainNoExtension}.modern.js`, ); - mainsByFormat.cjs = replaceName( - pkg['cjs:main'] || (pkg.type && pkg.type === 'module' ? 'x.cjs' : 'x.js'), - mainNoExtension, + mainsByFormat.cjs = resolve( + pkg['cjs:main'] || + `${mainNoExtension}.${pkg.type === 'module' ? 'c' : ''}js`, ); - mainsByFormat.umd = replaceName( - pkg['umd:main'] || pkg.unpkg || 'x.umd.js', - mainNoExtension, + mainsByFormat.umd = resolve( + pkg['umd:main'] || pkg.unpkg || `${mainNoExtension}.umd.js`, ); return mainsByFormat[format] || mainsByFormat.cjs; diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index e70c9176..88981a40 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -1417,6 +1417,122 @@ exports[`fixtures build custom-babelrc with microbundle 5`] = ` " `; +exports[`fixtures build custom-output with microbundle 1`] = ` +"Used script: microbundle -f cjs,es,umd,modern + +Directory tree: + +custom-output + dist + custom-name.js + custom-name.js.map + index.esm.js + index.esm.js.map + index.js + index.js.map + index.umd.js + index.umd.js.map + package.json + src + index.js + two.js + + +Build \\"customOutput\\" to dist: +187 B: index.js.gz +138 B: index.js.br +188 B: index.esm.js.gz +139 B: index.esm.js.br +274 B: index.umd.js.gz +203 B: index.umd.js.br +113 B: custom-name.js.gz +92 B: custom-name.js.br" +`; + +exports[`fixtures build custom-output with microbundle 2`] = `8`; + +exports[`fixtures build custom-output with microbundle 3`] = ` +"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; +//# sourceMappingURL=custom-name.js.map +" +`; + +exports[`fixtures build custom-output with microbundle 4`] = ` +"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)}} +//# sourceMappingURL=index.esm.js.map +" +`; + +exports[`fixtures build custom-output with microbundle 5`] = ` +"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)}}; +//# sourceMappingURL=index.js.map +" +`; + +exports[`fixtures build custom-output with microbundle 6`] = ` +"!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)}}}); +//# sourceMappingURL=index.umd.js.map +" +`; + +exports[`fixtures build custom-outputs with microbundle 1`] = ` +"Used script: microbundle -f cjs,es,umd,modern + +Directory tree: + +custom-outputs + dist + custom-exports.js + custom-exports.js.map + custom-main.js + custom-main.js.map + custom-module.js + custom-module.js.map + custom-unpkg.js + custom-unpkg.js.map + package.json + src + index.js + two.js + + +Build \\"customOutputs\\" to dist: +187 B: custom-main.js.gz +138 B: custom-main.js.br +188 B: custom-module.js.gz +139 B: custom-module.js.br +273 B: custom-unpkg.js.gz +203 B: custom-unpkg.js.br +113 B: custom-exports.js.gz +92 B: custom-exports.js.br" +`; + +exports[`fixtures build custom-outputs with microbundle 2`] = `8`; + +exports[`fixtures build custom-outputs with microbundle 3`] = ` +"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; +//# sourceMappingURL=custom-exports.js.map +" +`; + +exports[`fixtures build custom-outputs with microbundle 4`] = ` +"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)}}; +//# sourceMappingURL=custom-main.js.map +" +`; + +exports[`fixtures build custom-outputs with microbundle 5`] = ` +"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)}} +//# sourceMappingURL=custom-module.js.map +" +`; + +exports[`fixtures build custom-outputs with microbundle 6`] = ` +"!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)}}}); +//# sourceMappingURL=custom-unpkg.js.map +" +`; + exports[`fixtures build custom-source with microbundle 1`] = ` "Used script: microbundle diff --git a/test/fixtures/custom-output/package.json b/test/fixtures/custom-output/package.json new file mode 100644 index 00000000..a80ba852 --- /dev/null +++ b/test/fixtures/custom-output/package.json @@ -0,0 +1,8 @@ +{ + "name": "custom-output", + "main": "./dist/index.js", + "exports": "./dist/custom-name.js", + "scripts": { + "build": "microbundle -f cjs,es,umd,modern" + } +} diff --git a/test/fixtures/custom-output/src/index.js b/test/fixtures/custom-output/src/index.js new file mode 100644 index 00000000..c0a2efb0 --- /dev/null +++ b/test/fixtures/custom-output/src/index.js @@ -0,0 +1,5 @@ +import { two } from './two'; + +export default async function (...args) { + return [await two(...args), await two(...args)]; +} diff --git a/test/fixtures/custom-output/src/two.js b/test/fixtures/custom-output/src/two.js new file mode 100644 index 00000000..9f1f741b --- /dev/null +++ b/test/fixtures/custom-output/src/two.js @@ -0,0 +1,3 @@ +export async function two(...args) { + return args.reduce((total, value) => total + value, 0); +} diff --git a/test/fixtures/custom-outputs/package.json b/test/fixtures/custom-outputs/package.json new file mode 100644 index 00000000..b1102082 --- /dev/null +++ b/test/fixtures/custom-outputs/package.json @@ -0,0 +1,10 @@ +{ + "name": "custom-outputs", + "main": "./dist/custom-main.js", + "module": "./dist/custom-module.js", + "unpkg": "./dist/custom-unpkg.js", + "exports": "./dist/custom-exports.js", + "scripts": { + "build": "microbundle -f cjs,es,umd,modern" + } +} diff --git a/test/fixtures/custom-outputs/src/index.js b/test/fixtures/custom-outputs/src/index.js new file mode 100644 index 00000000..c0a2efb0 --- /dev/null +++ b/test/fixtures/custom-outputs/src/index.js @@ -0,0 +1,5 @@ +import { two } from './two'; + +export default async function (...args) { + return [await two(...args), await two(...args)]; +} diff --git a/test/fixtures/custom-outputs/src/two.js b/test/fixtures/custom-outputs/src/two.js new file mode 100644 index 00000000..9f1f741b --- /dev/null +++ b/test/fixtures/custom-outputs/src/two.js @@ -0,0 +1,3 @@ +export async function two(...args) { + return args.reduce((total, value) => total + value, 0); +}