Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Rearranging dist file order
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Feb 6, 2019
1 parent 745b418 commit dbb59e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
31 changes: 16 additions & 15 deletions dist/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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] } : {})
}
}

Expand Down
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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] } : {})
}
}

0 comments on commit dbb59e4

Please sign in to comment.