2015-02-02 13:53:09 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-11 22:42:27 +00:00
|
|
|
var browserify = require('browserify'),
|
2015-02-02 13:53:09 +00:00
|
|
|
derequire = require('browserify-derequire'),
|
2015-01-11 22:42:27 +00:00
|
|
|
collapse = require('bundle-collapser/plugin'),
|
|
|
|
fs = require('fs'),
|
2017-11-19 12:31:06 +00:00
|
|
|
path = require('path'),
|
|
|
|
flattenBundle = require('browser-pack-flat/plugin'),
|
|
|
|
commonShake = require('common-shakeify'),
|
|
|
|
unassertify = require('unassertify'),
|
2018-01-08 10:48:38 +00:00
|
|
|
uglify = require('uglify-es'),
|
|
|
|
envify = require('envify');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var pkg = require('../package');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var asyncSeries = require('./helpers').asyncSeries;
|
|
|
|
|
|
|
|
var BANNER = fs.readFileSync(__dirname + '/../resources/banner.txt', 'utf8'),
|
|
|
|
BANNER_MIN = fs.readFileSync(__dirname + '/../resources/banner-min.txt', 'utf8');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
module.exports = function bundleAll(dest, targets, done) {
|
2018-01-05 15:09:39 +00:00
|
|
|
|
|
|
|
var fns = Object.keys(targets).map(function(k) {
|
|
|
|
|
|
|
|
var variant = k;
|
|
|
|
var entry = targets[k];
|
|
|
|
|
|
|
|
return function(done) {
|
|
|
|
console.log('\nbundle ' + variant);
|
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
bundle(dest, variant, entry, done);
|
2018-01-05 15:09:39 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
asyncSeries(fns, done);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2015-02-02 13:53:09 +00:00
|
|
|
function Timer() {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer.prototype.done = function(message) {
|
2017-11-19 12:31:06 +00:00
|
|
|
console.log(message, '[' + (this.now() - this.s) + 'ms]');
|
2015-02-02 13:53:09 +00:00
|
|
|
this.reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
Timer.prototype.reset = function() {
|
2017-11-19 12:31:06 +00:00
|
|
|
this.s = this.now();
|
|
|
|
};
|
|
|
|
|
|
|
|
Timer.prototype.start = function(msg) {
|
|
|
|
this.reset();
|
2015-02-02 13:53:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timer.prototype.now = function() {
|
|
|
|
return new Date().getTime();
|
|
|
|
};
|
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
function processTemplate(str, args) {
|
|
|
|
return str.replace(/\{\{\s*([^\s]+)\s*\}\}/g, function(_, n) {
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var replacement = args[n];
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
if (!replacement) {
|
|
|
|
throw new Error('unknown template {{ ' + n + '}}');
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
return replacement;
|
|
|
|
});
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
function pad(n) {
|
|
|
|
if (n < 10) {
|
|
|
|
return '0' + n;
|
|
|
|
} else {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
function today() {
|
|
|
|
var d = new Date();
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
return [
|
|
|
|
d.getFullYear(),
|
|
|
|
pad(d.getMonth() + 1),
|
|
|
|
pad(d.getDay())
|
|
|
|
].join('-');
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
function bundle(dest, variant, entry, done) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var src = path.resolve(entry);
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var config = {
|
|
|
|
variant: variant,
|
|
|
|
version: pkg.version,
|
|
|
|
date: today()
|
|
|
|
};
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var browserifyOptions = {
|
|
|
|
standalone: 'BpmnJS',
|
|
|
|
builtins: false,
|
|
|
|
insertGlobalVars: {
|
|
|
|
process: function() {
|
|
|
|
return 'undefined';
|
|
|
|
},
|
|
|
|
Buffer: function() {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var targetFileBase = path.join(dest, variant);
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var banner = processTemplate(BANNER, config),
|
|
|
|
bannerMin = processTemplate(BANNER_MIN, config);
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var timer = new Timer();
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var fns = [
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
// production
|
|
|
|
function(done) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
timer.start('build prod');
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
browserify(browserifyOptions)
|
2018-01-08 10:48:38 +00:00
|
|
|
.transform(envify, {
|
|
|
|
NODE_ENV: 'production'
|
|
|
|
})
|
2018-01-05 15:09:39 +00:00
|
|
|
.transform(unassertify)
|
|
|
|
.plugin(commonShake)
|
|
|
|
.plugin(flattenBundle)
|
|
|
|
.plugin(collapse)
|
|
|
|
.plugin(derequire)
|
|
|
|
.add(src)
|
|
|
|
.bundle(function(err, result) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
timer.done('bundled');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var str = result.toString('utf-8');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
var minified = uglify.minify(str, {
|
|
|
|
compress: true,
|
|
|
|
mangle: true,
|
|
|
|
output: {
|
|
|
|
preamble: bannerMin
|
|
|
|
}
|
2017-11-19 12:31:06 +00:00
|
|
|
});
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
timer.done('minified');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
fs.writeFileSync(targetFileBase + '.production.min.js', minified.code, 'utf8');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
timer.done('wrote ' + targetFileBase + '.production.min.js');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
// development
|
|
|
|
function(done) {
|
|
|
|
timer.start('build dev');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
browserify(browserifyOptions)
|
2018-01-08 10:48:38 +00:00
|
|
|
.transform(envify, {
|
|
|
|
NODE_ENV: 'development'
|
|
|
|
})
|
2018-01-05 15:09:39 +00:00
|
|
|
.plugin(collapse)
|
|
|
|
.plugin(derequire)
|
|
|
|
.add(src)
|
|
|
|
.bundle(function(err, result) {
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
timer.done('bundled');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
var code = banner + result.toString('utf-8');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
fs.writeFileSync(targetFileBase + '.development.js', code, 'utf8');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-08 08:21:24 +00:00
|
|
|
timer.done('wrote ' + targetFileBase + '.development.js');
|
2017-11-19 12:31:06 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
done();
|
|
|
|
});
|
2017-11-19 12:31:06 +00:00
|
|
|
}
|
2018-01-05 15:09:39 +00:00
|
|
|
];
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2018-01-05 15:09:39 +00:00
|
|
|
asyncSeries(fns, done);
|
|
|
|
}
|