lbundle
Version:
Small zero-configuration bundler build on top of Rollup.js and SWC for NPM libraries
59 lines (56 loc) • 2.1 kB
JavaScript
import 'rollup';
import { isString, isDefined } from '../utils/checks.mjs';
import { getExportsFilenames } from '../utils/get-exports-filenames.mjs';
import { getFilenameOutputFormat } from '../utils/get-filename-output-format.mjs';
import path from 'path';
const getLibOutputs = (param)=>{
let { options, globalName, pkg, isModule, cssFilename } = param;
const map = new Map();
[
pkg.main,
pkg.module,
pkg.unpkg,
...getExportsFilenames(pkg.exports)
].filter((filename)=>isString(filename) && jsExtensions[path.extname(filename)]).map((filename)=>{
const format = getFilenameOutputFormat(filename, isModule);
const ext = path.extname(filename);
const preserveModules = preserveModulesFormats[format];
const entryFileNames = preserveModules ? `[name]${ext}` : undefined;
const dir = preserveModules ? path.resolve(options.cwd, path.dirname(filename)) : undefined;
const file = preserveModules ? undefined : path.resolve(options.cwd, filename);
return {
name: globalName,
format,
file,
dir,
entryFileNames,
preserveModules,
assetFileNames: (param)=>{
let { names } = param;
const normalizedCssFilename = path.posix.normalize(cssFilename);
if (names.includes(normalizedCssFilename)) {
if (isDefined(dir)) return path.relative(dir, path.resolve(options.cwd, normalizedCssFilename));
return normalizedCssFilename;
}
return 'assets/[name]-[hash][extname]';
},
esModule: format === 'cjs',
strict: true,
sourcemap: true
};
}).forEach((output)=>map.set(output.format, output));
return [
...map.values()
];
};
const preserveModulesFormats = {
'esm': true,
'cjs': true
};
const jsExtensions = {
'.js': true,
'.cjs': true,
'.mjs': true
};
export { getLibOutputs };
//# sourceMappingURL=get-lib-outputs.mjs.map