Skip to content

Commit

Permalink
refactor: refactor the rollup config for easier build additions (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Jul 16, 2018
1 parent c22389b commit 3538f2d
Showing 1 changed file with 84 additions and 85 deletions.
169 changes: 84 additions & 85 deletions generators/app/templates/scripts/_rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ const {uglify} = require('rollup-plugin-uglify');
const {minify} = require('uglify-es');
const pkg = require('../package.json');

/* General Globals */
const moduleName = '<%= moduleName %>';
const pluginName = '<%= pluginName %>';
const mainFile = 'src/plugin.js';
const banner = `/*! @name ${pkg.name} @version ${pkg.version} @license ${pkg.license} */`;
/* to prevent going into a screen during rollup */
process.stderr.isTTY = false;

let isWatch = false;

process.argv.forEach((a) => {
if ((/-w|--watch/).test(a)) {
isWatch = true;
}
});

/* configuration for plugins */
const primedPlugins = {
Expand All @@ -36,19 +41,11 @@ const primedPlugins = {
uglify: uglify({output: {comments: 'some'}}, minify)
};

// to prevent a screen during rollup watch/build
process.stderr.isTTY = false;

let isWatch = false;

process.argv.forEach((a) => {
if ((/-w|--watch/).test(a)) {
isWatch = true;
}
});
/* General Globals */
const moduleName = '<%= moduleName %>';
const pluginName = '<%= pluginName %>';

// globals, aka replace require calls
// with this
// globals, aka replace require calls with this
const globals = {
umd: {
'video.js': 'videojs',
Expand All @@ -67,9 +64,8 @@ const globals = {
}
};

// externals, aka don't bundle there
// and if not listed as a global don't require
// them either
// externals, aka don't bundle these and if not
// listed as a global don't require them either
const externals = {
umd: Object.keys(globals.umd).concat([

Expand All @@ -85,7 +81,6 @@ const externals = {
/* plugins that should be used in each bundle with caveats as comments */
const plugins = {
// note uglify will be added before babel for minified bundle
// see minPlugins below
umd: [
primedPlugins.resolve,
primedPlugins.json,
Expand All @@ -109,74 +104,78 @@ const plugins = {
]
};

// clone umd plugins, remove babel, add uglify then babel
const minPlugins = plugins.umd.slice();

minPlugins.splice(plugins.umd.indexOf(primedPlugins.babel), 1);
minPlugins.push(primedPlugins.uglify);
minPlugins.push(primedPlugins.babel);

const builds = [{
// umd
input: mainFile,
output: {
name: moduleName,
file: `dist/${pluginName}.js`,
format: 'umd',
globals: globals.umd,
banner
},
external: externals.umd,
plugins: plugins.umd
}, {
// cjs
input: mainFile,
output: [{
file: `dist/${pluginName}.cjs.js`,
format: 'cjs',
globals: globals.module,
banner
}],
external: externals.module,
plugins: plugins.module
}, {
// es
input: mainFile,
output: [{
file: `dist/${pluginName}.es.js`,
format: 'es',
globals: globals.module,
banner
}],
external: externals.module,
plugins: plugins.module
}, {
// test bundle
input: 'test/**/*.test.js',
output: {
name: `${moduleName}Tests`,
file: 'test/dist/bundle.js',
format: 'iife',
globals: globals.test
},
external: externals.test,
plugins: plugins.test
}];
/* make a build with the specifed settings */
const makeBuild = (name, settings) => {
const b = Object.assign({}, {
plugins: plugins[name],
external: externals[name],
input: 'src/plugin.js'
}, settings);

const fixOutput = (o) => {
if (!o.banner) {
o.banner = `/*! @name ${pkg.name} @version ${pkg.version} @license ${pkg.license} */`;
}
if (!o.globals) {
o.globals = globals[name];
}

return o;
};

if (!Array.isArray(b.output)) {
b.output = fixOutput(b.output);
} else {
b.output = b.output.map(fixOutput);
}

return b;
};

/* all rollup builds by name. note only object values will be used */
const builds = {
umd: makeBuild('umd', {
output: [{
name: moduleName,
file: `dist/${pluginName}.js`,
format: 'umd'
}]
}),
cjs: makeBuild('module', {
output: [{
file: `dist/${pluginName}.cjs.js`,
format: 'cjs'
}]
}),
es: makeBuild('module', {
output: [{
file: `dist/${pluginName}.es.js`,
format: 'es'
}]
}),
test: makeBuild('test', {
input: 'test/**/*.test.js',
output: [{
name: `${moduleName}Tests`,
file: 'test/dist/bundle.js',
format: 'iife'
}]
})
};

if (!isWatch) {
builds.push({
// minified umd
input: mainFile,
output: {
builds.minUmd = makeBuild('umd', {
output: [{
name: moduleName,
file: `dist/${pluginName}.min.js`,
format: 'umd',
globals: globals.umd,
banner
},
external: externals.umd,
plugins: minPlugins
format: 'umd'
}],
// we need to minify before babel
plugins: plugins.umd
.filter((p) => p !== primedPlugins.babel)
.concat([primedPlugins.uglify, primedPlugins.babel])
});

}

export default builds;
export default Object.values(builds);

0 comments on commit 3538f2d

Please sign in to comment.