-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): support building for the browser with rollup
- Loading branch information
Kent C. Dodds
committed
Sep 7, 2017
1 parent
6697b79
commit 25ca58f
Showing
12 changed files
with
251 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const path = require('path') | ||
const camelcase = require('lodash.camelcase') | ||
const rollupBabel = require('rollup-plugin-babel') | ||
const commonjs = require('rollup-plugin-commonjs') | ||
const nodeResolve = require('rollup-plugin-node-resolve') | ||
const json = require('rollup-plugin-json') | ||
const uglify = require('rollup-plugin-uglify') | ||
const rollupAlias = require('rollup-plugin-alias') | ||
const {getPkg, hasFile, hasPkgProp, parseEnv} = require('../utils') | ||
|
||
const pkg = getPkg() | ||
const here = p => path.join(__dirname, p) | ||
|
||
const minify = parseEnv('BUILD_MINIFY', false) | ||
const format = process.env.BUILD_FORMAT | ||
const isPreact = parseEnv('BUILD_PREACT', false) | ||
|
||
const capitalize = s => s[0].toUpperCase() + s.slice(1) | ||
|
||
const defaultGlobals = Object.keys(pkg.peerDependencies).reduce((deps, dep) => { | ||
deps[dep] = capitalize(camelcase(dep)) | ||
return deps | ||
}, {}) | ||
|
||
const defaultExternal = Object.keys(pkg.peerDependencies) | ||
|
||
const filenameSuffix = parseEnv( | ||
'BUILD_FILENAME_SUFFIX', | ||
isPreact ? '.preact' : '', | ||
) | ||
const globals = parseEnv( | ||
'BUILD_GLOBALS', | ||
isPreact ? Object.assign(defaultGlobals, {preact: 'preact'}) : defaultGlobals, | ||
) | ||
const external = parseEnv( | ||
'BUILD_EXTERNAL', | ||
isPreact ? defaultExternal.concat(['preact', 'prop-types']) : defaultExternal, | ||
).filter((e, i, arry) => arry.indexOf(e) === i) | ||
|
||
if (isPreact) { | ||
delete globals.react | ||
delete globals['prop-types'] // TODO: is this necessary? | ||
external.splice(external.indexOf('react'), 1) | ||
} | ||
|
||
const alias = parseEnv('BUILD_ALIAS', isPreact ? {react: 'preact'} : null) | ||
const esm = format === 'esm' | ||
const umd = format === 'umd' | ||
const cjs = format === 'cjs' | ||
|
||
let output | ||
|
||
const filename = path.join('dist', pkg.name) | ||
|
||
if (esm) { | ||
output = [{file: `${filename}${filenameSuffix}.es.js`, format: 'es'}] | ||
} else if (umd) { | ||
if (minify) { | ||
output = [ | ||
{ | ||
file: `${filename}${filenameSuffix}.umd.min.js`, | ||
format: 'umd', | ||
sourcemap: true, | ||
}, | ||
] | ||
} else { | ||
output = [ | ||
{ | ||
file: `${filename}${filenameSuffix}.umd.js`, | ||
format: 'umd', | ||
sourcemap: true, | ||
}, | ||
] | ||
} | ||
} else if (cjs) { | ||
output = [{file: `${filename}${filenameSuffix}.cjs.js`, format: 'cjs'}] | ||
} else if (format) { | ||
throw new Error(`invalid format specified: "${format}".`) | ||
} else { | ||
throw new Error('no format specified. --environment FORMAT:xxx') | ||
} | ||
|
||
const useBuiltinConfig = !hasFile('.babelrc') && !hasPkgProp('babel') | ||
const babelPresets = useBuiltinConfig ? [here('../config/babelrc.js')] : [] | ||
|
||
module.exports = { | ||
input: 'src/index.js', | ||
output, | ||
exports: esm ? 'named' : 'default', | ||
name: capitalize(camelcase(pkg.name)), | ||
external, | ||
globals, | ||
plugins: [ | ||
alias ? rollupAlias(alias) : null, | ||
nodeResolve({jsnext: true, main: true}), | ||
commonjs({include: 'node_modules/**'}), | ||
json(), | ||
rollupBabel({ | ||
exclude: 'node_modules/**', | ||
presets: babelPresets, | ||
babelrc: true, | ||
}), | ||
minify ? uglify() : null, | ||
].filter(Boolean), | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if (process.argv.includes('--browser')) { | ||
require('./rollup') | ||
} else { | ||
require('./babel') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const path = require('path') | ||
const spawn = require('cross-spawn') | ||
const rimraf = require('rimraf') | ||
const { | ||
hasFile, | ||
resolveBin, | ||
fromRoot, | ||
getConcurrentlyArgs, | ||
} = require('../../utils') | ||
|
||
const crossEnv = resolveBin('cross-env') | ||
const rollup = resolveBin('rollup') | ||
const args = process.argv.slice(2) | ||
const here = p => path.join(__dirname, p) | ||
|
||
const useBuiltinConfig = | ||
!args.includes('--config') && !hasFile('rollup.config.js') | ||
const config = useBuiltinConfig | ||
? `--config ${here('../../config/rollup.config.js')}` | ||
: args.includes('--config') ? '' : '--config' // --config will pick up the rollup.config.js file | ||
|
||
const getCommand = env => | ||
`${crossEnv} BUILD_ROLLUP=true ${env} ${rollup} ${config}` | ||
|
||
const scripts = args.includes('--p-react') | ||
? getPReactScripts() | ||
: getConcurrentlyArgs({ | ||
esm: getCommand('BUILD_FORMAT=esm'), | ||
cjs: getCommand('BUILD_FORMAT=cjs'), | ||
umd: getCommand('BUILD_FORMAT=umd'), | ||
'umd.min': getCommand('BUILD_FORMAT=umd BUILD_MINIFY=true'), | ||
}) | ||
|
||
rimraf.sync(fromRoot('dist')) | ||
|
||
const result = spawn.sync(resolveBin('concurrently'), scripts, { | ||
stdio: 'inherit', | ||
}) | ||
|
||
function getPReactScripts() { | ||
return getConcurrentlyArgs({ | ||
'react.esm': getCommand('BUILD_FORMAT=esm'), | ||
'react.cjs': getCommand('BUILD_FORMAT=cjs'), | ||
'react.umd': getCommand('BUILD_FORMAT=umd'), | ||
'react.umd.min': getCommand('BUILD_FORMAT=umd BUILD_MINIFY=true'), | ||
'preact.esm': getCommand('BUILD_PREACT=true BUILD_FORMAT=esm'), | ||
'preact.cjs': getCommand('BUILD_PREACT=true BUILD_FORMAT=cjs'), | ||
'preact.umd': getCommand('BUILD_PREACT=true BUILD_FORMAT=umd'), | ||
'preact.umd.min': getCommand( | ||
'BUILD_PREACT=true BUILD_FORMAT=umd BUILD_MINIFY=true', | ||
), | ||
}) | ||
} | ||
|
||
process.exit(result.status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.