Skip to content

Commit

Permalink
refactor: make the rollup config easier for users to edit (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Jun 26, 2018
1 parent 55c4f3f commit d2b2bd8
Showing 1 changed file with 103 additions and 75 deletions.
178 changes: 103 additions & 75 deletions generators/app/templates/scripts/_rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const resolve = require('rollup-plugin-node-resolve');
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} */`;

const plugins = {
/* configuration for plugins */
const primedPlugins = {
babel: babel({
babelrc: false,
exclude: 'node_modules/**',
Expand Down Expand Up @@ -41,120 +47,142 @@ process.argv.forEach((a) => {
}
});

const umdGlobals = {
'video.js': 'videojs',
'global': 'window',
'global/window': 'window',
'global/document': 'document'
// globals, aka replace require calls
// with this
const globals = {
umd: {
'video.js': 'videojs',
'global': 'window',
'global/window': 'window',
'global/document': 'document'
},
module: {
'video.js': 'videojs'
},
test: {
'qunit': 'QUnit',
'qunitjs': 'QUnit',
'sinon': 'sinon',
'video.js': 'videojs'
}
};
const moduleGlobals = {
'video.js': 'videojs'

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

]),
module: Object.keys(globals.module).concat([

]),
test: Object.keys(globals.test).concat([

])
};

const testGlobals = {
'qunit': 'QUnit',
'qunitjs': 'QUnit',
'sinon': 'sinon',
'video.js': 'videojs'
/* 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,
primedPlugins.commonjs,
primedPlugins.babel
],

// note babel will be removed for es module bundle
// see esPlugins below
module: [
primedPlugins.resolve,
primedPlugins.json,
primedPlugins.commonjs,
primedPlugins.babel
],

test: [
primedPlugins.multiEntry,
primedPlugins.resolve,
primedPlugins.json,
primedPlugins.commonjs,
primedPlugins.babel
]
};

const testExternals = Object.keys(testGlobals).concat([
]);
// clone module plugins, remove babel
const esPlugins = plugins.module.slice();

esPlugins.splice(plugins.module.indexOf(primedPlugins.babel), 1);

// 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: 'src/plugin.js',
input: mainFile,
output: {
name: '<%= moduleName %>',
file: 'dist/<%= pluginName %>.js',
name: moduleName,
file: `dist/${pluginName}.js`,
format: 'umd',
globals: umdGlobals,
globals: globals.umd,
banner
},
external: Object.keys(umdGlobals),
plugins: [
plugins.resolve,
plugins.json,
plugins.commonjs,
plugins.babel
]
external: externals.umd,
plugins: plugins.umd
}, {
// cjs
input: 'src/plugin.js',
input: mainFile,
output: [{
file: 'dist/<%= pluginName %>.cjs.js',
file: `dist/${pluginName}.cjs.js`,
format: 'cjs',
globals: moduleGlobals,
globals: globals.module,
banner
}],
external: Object.keys(moduleGlobals).concat([
'global',
'global/document',
'global/window'
]),
plugins: [
plugins.resolve,
plugins.json,
plugins.commonjs,
plugins.babel
]
external: externals.module,
plugins: plugins.module
}, {
// es
input: 'src/plugin.js',
input: mainFile,
output: [{
file: 'dist/<%= pluginName %>.es.js',
file: `dist/${pluginName}.es.js`,
format: 'es',
globals: moduleGlobals,
globals: globals.module,
banner
}],
external: Object.keys(moduleGlobals).concat([
'global',
'global/document',
'global/window'
]),
plugins: [
plugins.resolve,
plugins.json,
plugins.commonjs
]
external: externals.module,
plugins: esPlugins
}, {
// test bundle
input: 'test/**/*.test.js',
output: {
name: '<%= moduleName %>Tests',
name: `${moduleName}Tests`,
file: 'test/dist/bundle.js',
format: 'iife',
globals: testGlobals
globals: globals.test
},
external: testExternals,
plugins: [
plugins.multiEntry,
plugins.resolve,
plugins.json,
plugins.commonjs,
plugins.babel
]
external: externals.test,
plugins: plugins.test
}];

if (!isWatch) {
builds.push({
// minified umd
input: 'src/plugin.js',
input: mainFile,
output: {
name: '<%= moduleName %>',
file: 'dist/<%= pluginName %>.min.js',
name: moduleName,
file: `dist/${pluginName}.min.js`,
format: 'umd',
globals: umdGlobals,
globals: globals.umd,
banner
},
external: Object.keys(umdGlobals),
plugins: [
plugins.resolve,
plugins.json,
plugins.commonjs,
plugins.uglify,
plugins.babel
]
external: externals.umd,
plugins: minPlugins
});
}

Expand Down

0 comments on commit d2b2bd8

Please sign in to comment.