Skip to content

Commit

Permalink
fix: Allows naming of outputs individually
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Sep 15, 2021
1 parent b1a6374 commit 8f7811a
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 19 deletions.
28 changes: 9 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
116 changes: 116 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/custom-output/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "custom-output",
"main": "./dist/index.js",
"exports": "./dist/custom-name.js",
"scripts": {
"build": "microbundle -f cjs,es,umd,modern"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/custom-output/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/custom-output/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);
}
10 changes: 10 additions & 0 deletions test/fixtures/custom-outputs/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/custom-outputs/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/custom-outputs/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 8f7811a

Please sign in to comment.