diff --git a/README.md b/README.md index bbd3eff..826ce28 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ serves different, more-specific use cases. * `--output, -o` Output filename or directory path (defaults to ./dist) * `--cjs, -c` Path for / whether to create a CommonJS dist file (defaults to false or main in package.json) -* `--iife, -f` Path for / whether to create a IIFE dist file (defaults to - false or iife in package.json) * `--esm, -e` Path for / whether to create a ESM dist file (defaults to false or module in package.json) * `--browser, -b` Path for / whether to create a browser-specific (ESM) dist file (defaults to false or browser in package.json) +* `--iife, -f` Path for / whether to create a IIFE dist file (defaults to + false or iife in package.json) * `--inline, -l` Inline/bundle imported modules (defaults to false) * `--babel` Transpile output with Babel (defaults to false) * `--plugins, -p` Specify a path for a Rollup plugins file to include diff --git a/cli.js b/cli.js index e7f0584..295a234 100755 --- a/cli.js +++ b/cli.js @@ -9,7 +9,7 @@ const dist = require('.') const { cyan, gray, yellow, red } = require('chalk') const writeFile = pify(fs.writeFile) -const logError = err => console.error(`💥 ${red('Boom!')}`, err) +const logError = err => console.error('💥', red('Boom!'), err) async function run () { const cli = meow( @@ -25,12 +25,12 @@ async function run () { --output, -o Output filename or directory path (defaults to ./dist) --cjs, -c Path for / whether to create a CommonJS dist file (defaults to false or main in package.json) - --iife, -f Path for / whether to create a IIFE dist file (defaults - to false or iife in package.json) --esm, -e Path for / whether to create a ESM dist file (defaults to false or module in package.json) --browser , -b Path for / whether to create a browser-specific (ESM) dist file (defaults to false or browser in package.json) + --iife, -f Path for / whether to create a IIFE dist file (defaults + to false or iife in package.json) --inline, -l Inline/bundle imported modules (defaults to false) --babel Transpile output with Babel (defaults to false) --plugins, -p Specify a path for a Rollup plugins file to include @@ -69,15 +69,15 @@ async function run () { fs.mkdirSync(dirname(path), { recursive: true }) // Inform the user about what files are being written. - const relative = path.replace(`${process.cwd()}/`, '') + const rel = path.replace(`${process.cwd()}/`, '') if (moduleType === 'cjs') { - console.info(cyan('💿 Writing CommonJS dist file:'), gray(relative)) + console.info('💿', cyan('Writing CommonJS dist file:'), gray(rel)) } else if (moduleType === 'esm') { - console.info(cyan('📦 Writing ES Module dist file:'), gray(relative)) + console.info('📦', cyan('Writing ES Module dist file:'), gray(rel)) } else if (moduleType === 'browser') { - console.info(cyan('🌎 Writing Browser dist file:'), gray(relative)) + console.info('🌎', cyan('Writing Browser dist file:'), gray(rel)) } else if (moduleType === 'iife') { - console.info(cyan('🎁 Writing IIFE dist file:'), gray(relative)) + console.info('🎁', cyan('Writing IIFE dist file:'), gray(rel)) } // Add the file write operation to the list of writes to be completed @@ -91,7 +91,7 @@ async function run () { // Filter the results for errors and log them. results.filter(r => r instanceof Error).forEach(err => logError(err)) } else { - console.warn(yellow('🤷 No distribution files were specified')) + console.warn(yellow('🤷', 'No distribution files were specified')) } } catch (err) { logError(err) diff --git a/dist/dist.js b/dist/dist.js index ac86bfb..faad05d 100644 --- a/dist/dist.js +++ b/dist/dist.js @@ -22,15 +22,16 @@ async function dist (options) { input = options.input || path.resolve(path.join(path.dirname(path$$1), 'index.js')), output = options.output || path.join(path.dirname(path$$1), 'dist'), cjs = options.cjs !== undefined ? options.cjs : pkg.main, - iife = options.iife !== undefined ? options.iife : pkg.iife, esm = options.esm !== undefined ? options.esm : pkg.module, - browser = options.browser !== undefined ? options.browser : pkg.browser + browser = options.browser !== undefined ? options.browser : pkg.browser, + iife = options.iife !== undefined ? options.iife : pkg.iife } = options; let inline = options.inline || options.inline === ''; cjs = cjs || cjs === ''; - iife = iife || iife === ''; esm = esm || esm === ''; + browser = browser || browser === ''; + iife = iife || iife === ''; // Import plugins file if specified. let plugins = []; @@ -97,44 +98,44 @@ async function dist (options) { cjsBundle = await bundler.generate({ format: 'cjs' }); } - // Generate the Immediately Invoked Function Expression (IIFE) bundle. - let iifeBundle; - if (iife) { - iifeBundle = await iifeBundler.generate({ format: 'iife', name }); - } - // Generate the EcmaScript Module bundle. let esmBundle; if (esm || browser) { esmBundle = await bundler.generate({ format: 'esm' }); } + // Generate the Immediately Invoked Function Expression (IIFE) bundle. + let iifeBundle; + if (iife) { + iifeBundle = await iifeBundler.generate({ format: 'iife', name }); + } + let cjsCode = cjs ? cjsBundle.output[0].code : undefined; - let iifeCode = iife ? iifeBundle.output[0].code : undefined; let esmCode = (esm || browser) ? esmBundle.output[0].code : undefined; + let iifeCode = iife ? iifeBundle.output[0].code : undefined; // Determine the output file paths. const dir = path.extname(output) ? path.dirname(output) : output; const cjsPath = typeof cjs === 'string' && path.extname(cjs) ? path.resolve(cjs) : path.join(dir, `${name}.js`); - const iifePath = typeof iife === 'string' && path.extname(iife) - ? path.resolve(iife) - : path.join(dir, `${name}.iife.js`); const esmPath = typeof esm === 'string' && path.extname(esm) ? path.resolve(esm) : path.join(dir, `${name}.m.js`); const browserPath = typeof browser === 'string' && path.extname(browser) ? path.resolve(browser) : path.join(dir, `${name}.browser.js`); + const iifePath = typeof iife === 'string' && path.extname(iife) + ? path.resolve(iife) + : path.join(dir, `${name}.iife.js`); // Return an object with the properties that use the file path as the key and // the source code as the value. return { ...(cjs ? { cjs: [cjsPath, cjsCode] } : {}), - ...(iife ? { iife: [iifePath, iifeCode] } : {}), ...(esm ? { esm: [esmPath, esmCode] } : {}), - ...(browser ? { browser: [browserPath, esmCode] } : {}) + ...(browser ? { browser: [browserPath, esmCode] } : {}), + ...(iife ? { iife: [iifePath, iifeCode] } : {}) } } diff --git a/index.js b/index.js index 96d5b8f..4877091 100644 --- a/index.js +++ b/index.js @@ -18,15 +18,16 @@ export default async function dist (options) { input = options.input || resolve(join(dirname(path), 'index.js')), output = options.output || join(dirname(path), 'dist'), cjs = options.cjs !== undefined ? options.cjs : pkg.main, - iife = options.iife !== undefined ? options.iife : pkg.iife, esm = options.esm !== undefined ? options.esm : pkg.module, - browser = options.browser !== undefined ? options.browser : pkg.browser + browser = options.browser !== undefined ? options.browser : pkg.browser, + iife = options.iife !== undefined ? options.iife : pkg.iife } = options let inline = options.inline || options.inline === '' cjs = cjs || cjs === '' - iife = iife || iife === '' esm = esm || esm === '' + browser = browser || browser === '' + iife = iife || iife === '' // Import plugins file if specified. let plugins = [] @@ -93,43 +94,43 @@ export default async function dist (options) { cjsBundle = await bundler.generate({ format: 'cjs' }) } - // Generate the Immediately Invoked Function Expression (IIFE) bundle. - let iifeBundle - if (iife) { - iifeBundle = await iifeBundler.generate({ format: 'iife', name }) - } - // Generate the EcmaScript Module bundle. let esmBundle if (esm || browser) { esmBundle = await bundler.generate({ format: 'esm' }) } + // Generate the Immediately Invoked Function Expression (IIFE) bundle. + let iifeBundle + if (iife) { + iifeBundle = await iifeBundler.generate({ format: 'iife', name }) + } + let cjsCode = cjs ? cjsBundle.output[0].code : undefined - let iifeCode = iife ? iifeBundle.output[0].code : undefined let esmCode = (esm || browser) ? esmBundle.output[0].code : undefined + let iifeCode = iife ? iifeBundle.output[0].code : undefined // Determine the output file paths. const dir = extname(output) ? dirname(output) : output const cjsPath = typeof cjs === 'string' && extname(cjs) ? resolve(cjs) : join(dir, `${name}.js`) - const iifePath = typeof iife === 'string' && extname(iife) - ? resolve(iife) - : join(dir, `${name}.iife.js`) const esmPath = typeof esm === 'string' && extname(esm) ? resolve(esm) : join(dir, `${name}.m.js`) const browserPath = typeof browser === 'string' && extname(browser) ? resolve(browser) : join(dir, `${name}.browser.js`) + const iifePath = typeof iife === 'string' && extname(iife) + ? resolve(iife) + : join(dir, `${name}.iife.js`) // Return an object with the properties that use the file path as the key and // the source code as the value. return { ...(cjs ? { cjs: [cjsPath, cjsCode] } : {}), - ...(iife ? { iife: [iifePath, iifeCode] } : {}), ...(esm ? { esm: [esmPath, esmCode] } : {}), - ...(browser ? { browser: [browserPath, esmCode] } : {}) + ...(browser ? { browser: [browserPath, esmCode] } : {}), + ...(iife ? { iife: [iifePath, iifeCode] } : {}) } }