UNPKG

@serpent/rollup-kits

Version:
71 lines (70 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.make = void 0; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const base_1 = require("./base"); /* format: amd, cjs, es, iife, umd, system amd – Asynchronous Module Definition, used with module loaders like RequireJS cjs – CommonJS, suitable for Node and other bundlers (alias: commonjs) iife – A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this.) umd – Universal Module Definition, works as amd, cjs and iife all in one es – Keep the bundle as an ES module file, suitable for other bundlers and inclusion as a <script type=module> tag in modern browsers (alias: esm, module) system – Native format of the SystemJS loader (alias: systemjs) */ function make(options) { const plugins = base_1.getPlugins(options.pluginOptions); const { pkg, entry, name, external, outputDir = 'dist', multiple, minify, formats = [], normalPlugins = plugins.normalPlugins, minifyPlugins = plugins.minifyPlugins, } = options; const baseName = getBaseName(entry); if (!formats.length) { if (pkg.main) formats.push({ format: 'cjs', file: pkg.main }); if (pkg.module) formats.push({ format: 'es', file: pkg.module }); if (pkg.browser) formats.push({ format: 'umd', file: pkg.browser, name }); } const result = []; const requireNameFormats = ['umd', 'iife']; const configs = [{ ext: '.js', plugins: normalPlugins }]; if (minify) configs.push({ ext: '.min.js', plugins: minifyPlugins }); configs.forEach(({ ext, plugins }) => { const formatsWithoutExternal = formats.filter(f => requireNameFormats.includes(f.format)); const formatsWithExternal = formats.filter(f => !requireNameFormats.includes(f.format)); const getOutput = (formats) => { return formats.map(ft => { let output = Object.assign(Object.assign({ name }, ft), { dir: multiple ? outputDir : undefined, file: multiple ? undefined : ft.file ? ft.file.replace(/\.\w+$/, ext) : path_1.default.join(outputDir, `${baseName}.${ft.format}${ext}`) }); if (options.handleOutput) output = options.handleOutput(output); return output; }); }; if (formatsWithoutExternal.length) { result.push({ input: entry, output: getOutput(formatsWithoutExternal), plugins, external: [...base_1.builtins], }); } if (formatsWithExternal.length) { result.push({ input: entry, output: getOutput(formatsWithExternal), plugins, external: external || base_1.getExternal(pkg), }); } }); return result; } exports.make = make; function getBaseName(name) { return path_1.default.basename(name, path_1.default.extname(name)); }