Skip to content

Commit

Permalink
Update the build script
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Aug 18, 2018
1 parent e9ac57d commit 9c8bb0e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 89 deletions.
174 changes: 86 additions & 88 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

/* eslint-env node */
/* eslint require-jsdoc: "off" */

/* eslint no-throw-literal: "off" */

const fs = require('fs-extra');
const glob = require('glob');
const {compile}= require('google-closure-compiler-js');
const {rollup} = require('rollup');
const memory = require('rollup-plugin-memory');
const virtual = require('rollup-plugin-virtual');
const nodeResolve = require('rollup-plugin-node-resolve');
const path = require('path');
const {SourceMapGenerator, SourceMapConsumer} = require('source-map');
Expand All @@ -34,91 +34,89 @@ const kebabCase = (str) => {
};


module.exports = (output, autotrackPlugins = []) => {
const entryPath = path.resolve(__dirname, '../lib/index.js');
const entry = autotrackPlugins.length === 0 ? entryPath : {
path: entryPath,
contents: autotrackPlugins
.map((plugin) => `import './plugins/${kebabCase(plugin)}';`)
.join('\n'),
};
const plugins = [nodeResolve()];
if (autotrackPlugins.length) plugins.push(memory());

return new Promise((resolve, reject) => {
rollup({entry, plugins}).then((bundle) => {
try {
const rollupResult = bundle.generate({
format: 'es',
dest: output,
sourceMap: true,
});

const externsDir = path.resolve(__dirname, '../lib/externs');
const externs = glob.sync(path.join(externsDir, '*.js'))
.reduce((acc, cur) => acc + fs.readFileSync(cur, 'utf-8'), '');

const closureFlags = {
jsCode: [{
src: rollupResult.code,
path: path.basename(output),
}],
compilationLevel: 'ADVANCED',
useTypesForOptimization: true,
outputWrapper:
'(function(){%output%})();\n' +
`//# sourceMappingURL=${path.basename(output)}.map`,
assumeFunctionWrapper: true,
rewritePolyfills: false,
warningLevel: 'VERBOSE',
createSourceMap: true,
externs: [{src: externs}],
};

const closureResult = compile(closureFlags);

if (closureResult.errors.length || closureResult.warnings.length) {
const rollupMap = new SourceMapConsumer(rollupResult.map);

// Remap errors from the closure compiler output to the original
// files before rollup bundled them.
const remap = (type) => (item) => {
let {line, column, source} = rollupMap.originalPositionFor({
line: item.lineNo,
column: item.charNo,
});
source = path.relative('.', path.resolve(__dirname, '..', source));
return {type, line, column, source, desc: item.description};
};

reject({
errors: [
...closureResult.errors.map(remap('error')),
...closureResult.warnings.map(remap('warning')),
],
});
} else {
// Currently, closure compiler doesn't support applying its generated
// source map to an existing source map, so we do it manually.
const fromMap = JSON.parse(closureResult.sourceMap);
const toMap = rollupResult.map;

const generator = SourceMapGenerator.fromSourceMap(
new SourceMapConsumer(fromMap));

generator.applySourceMap(
new SourceMapConsumer(toMap), path.basename(output));

const sourceMap = generator.toString();

resolve({
code: closureResult.compiledCode,
map: sourceMap,
});
}
} catch(err) {
reject(err);
}
}).catch(reject);
module.exports = async (output, autotrackPlugins = []) => {
const input = path.resolve(__dirname, '../lib/index.js');

const plugins = [];
if (autotrackPlugins.length) {
const pluginPath = path.resolve(__dirname, '../lib/plugins');

// Generate the input file based on the autotrack plugins to bundle.
plugins.push(virtual({
[input]: autotrackPlugins
.map((plugin) => `import '${pluginPath}/${kebabCase(plugin)}';`)
.join('\n'),
}));
}
plugins.push(nodeResolve());

const bundle = await rollup({input, plugins});
const rollupResult = await bundle.generate({
format: 'es',
dest: output,
sourcemap: true, // Note: lowercase "m" in sourcemap.
});

const externsDir = path.resolve(__dirname, '../lib/externs');
const externs = glob.sync(path.join(externsDir, '*.js'))
.reduce((acc, cur) => acc + fs.readFileSync(cur, 'utf-8'), '');

const closureFlags = {
jsCode: [{
src: rollupResult.code,
path: path.basename(output),
}],
compilationLevel: 'ADVANCED',
useTypesForOptimization: true,
outputWrapper:
'(function(){%output%})();\n' +
`//# sourceMappingURL=${path.basename(output)}.map`,
assumeFunctionWrapper: true,
rewritePolyfills: false,
warningLevel: 'VERBOSE',
createSourceMap: true,
externs: [{src: externs}],
};

const closureResult = compile(closureFlags);

if (closureResult.errors.length || closureResult.warnings.length) {
const rollupMap = await new SourceMapConsumer(rollupResult.map);

// Remap errors from the closure compiler output to the original
// files before rollup bundled them.
const remap = (type) => (item) => {
let {line, column, source} = rollupMap.originalPositionFor({
line: item.lineNo,
column: item.charNo,
});
source = path.relative('.', path.resolve(__dirname, '..', source));
return {type, line, column, source, desc: item.description};
};

throw {
errors: [
...closureResult.errors.map(remap('error')),
...closureResult.warnings.map(remap('warning')),
],
};
} else {
// Currently, closure compiler doesn't support applying its generated
// source map to an existing source map, so we do it manually.
const fromMap = JSON.parse(closureResult.sourceMap);
const toMap = rollupResult.map;

const generator = SourceMapGenerator.fromSourceMap(
await new SourceMapConsumer(fromMap));

generator.applySourceMap(
await new SourceMapConsumer(toMap), path.basename(output));

const sourceMap = generator.toString();

return {
code: closureResult.compiledCode,
map: sourceMap,
};
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"google-closure-compiler-js": "^20180610.0.0",
"gzip-size": "^5.0.0",
"rollup": "^0.64.1",
"rollup-plugin-memory": "^3.0.0",
"rollup-plugin-virtual": "^1.0.1",
"rollup-plugin-node-resolve": "^3.3.0",
"source-map": "^0.7.3"
},
Expand Down

0 comments on commit 9c8bb0e

Please sign in to comment.