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'),
|
|
|
|
concat = require('source-map-concat'),
|
|
|
|
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'),
|
|
|
|
uglify = require('uglify-es');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
var assign = Object.assign;
|
2015-01-11 22:42:27 +00:00
|
|
|
|
|
|
|
var BANNER = fs.readFileSync(__dirname + '/banner.txt', 'utf8'),
|
|
|
|
BANNER_MIN = fs.readFileSync(__dirname + '/banner-min.txt', 'utf8');
|
|
|
|
|
2016-01-26 08:15:45 +00:00
|
|
|
var SOURCE_MAP_HEADER = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,';
|
2015-01-11 22:42:27 +00:00
|
|
|
|
|
|
|
|
2016-01-26 08:15:45 +00:00
|
|
|
function extractSourceMap(content) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
|
|
|
var idx = content.indexOf(SOURCE_MAP_HEADER),
|
|
|
|
map, code;
|
|
|
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
code = content.substring(0, idx);
|
|
|
|
map = content.substring(idx + SOURCE_MAP_HEADER.length);
|
|
|
|
|
|
|
|
map = new Buffer(map, 'base64').toString();
|
|
|
|
|
|
|
|
map = map.replace(/\\\\/g, '/'); // convert \\ -> /
|
|
|
|
|
|
|
|
var dir = __dirname;
|
|
|
|
|
|
|
|
var dirPattern = dir.replace(/\\/g, '/').replace(/\./g, '\\.') + '/';
|
|
|
|
|
|
|
|
var pattern = new RegExp(dirPattern, 'g');
|
|
|
|
|
|
|
|
map = map.replace(pattern, '');
|
|
|
|
|
|
|
|
return {
|
|
|
|
code: code,
|
|
|
|
map: JSON.parse(map)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error('no attached source map');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
console.log('start: ' + msg);
|
|
|
|
|
|
|
|
this.reset();
|
2015-02-02 13:53:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timer.prototype.now = function() {
|
|
|
|
return new Date().getTime();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-11 22:42:27 +00:00
|
|
|
module.exports = function(grunt) {
|
|
|
|
|
|
|
|
grunt.registerMultiTask('bundle', function(target) {
|
|
|
|
|
|
|
|
var data = this.data,
|
|
|
|
variant = data.name,
|
|
|
|
dest = data.dest,
|
|
|
|
src = path.resolve(data.src);
|
|
|
|
|
|
|
|
grunt.config.set('config.variant', variant);
|
|
|
|
|
|
|
|
var done = this.async();
|
|
|
|
|
|
|
|
var browserifyOptions = {
|
|
|
|
standalone: 'BpmnJS',
|
2015-02-02 13:53:09 +00:00
|
|
|
builtins: false,
|
|
|
|
insertGlobalVars: {
|
2017-11-19 12:31:06 +00:00
|
|
|
process: function() {
|
|
|
|
return 'undefined';
|
2015-02-02 13:53:09 +00:00
|
|
|
},
|
2017-11-19 12:31:06 +00:00
|
|
|
Buffer: function() {
|
|
|
|
return 'undefined';
|
2015-02-02 13:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var targetFileBase = path.join(dest, variant);
|
|
|
|
|
|
|
|
var banner = grunt.template.process(BANNER, grunt.config.get()),
|
|
|
|
bannerMin = grunt.template.process(BANNER_MIN, grunt.config.get());
|
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
var timer = new Timer();
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
var fns = [
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
// production
|
|
|
|
function(done) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
timer.start('build prod');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
browserify(browserifyOptions)
|
|
|
|
.transform(unassertify)
|
|
|
|
.plugin(commonShake)
|
|
|
|
.plugin(flattenBundle)
|
|
|
|
.plugin(collapse)
|
|
|
|
.plugin(derequire)
|
|
|
|
.add(src)
|
|
|
|
.bundle(function(err, result) {
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
timer.done('bundled');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
var str = result.toString('utf-8');
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
var minified = uglify.minify(str, {
|
|
|
|
compress: true,
|
|
|
|
mangle: true,
|
|
|
|
output: {
|
|
|
|
preamble: bannerMin
|
|
|
|
}
|
|
|
|
});
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
timer.done('minified');
|
|
|
|
|
|
|
|
grunt.file.write(targetFileBase + '.min.js', minified.code, 'utf8');
|
|
|
|
|
|
|
|
timer.done('saved');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
// development
|
|
|
|
function(done) {
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
timer.start('build dev');
|
2015-01-11 22:42:27 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
browserify(assign({ debug: true }, browserifyOptions))
|
|
|
|
.plugin(collapse)
|
|
|
|
.plugin(derequire)
|
|
|
|
.add(src)
|
|
|
|
.bundle(function(err, result) {
|
|
|
|
|
|
|
|
timer.done('bundled');
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var bundled = extractSourceMap(result.toString('utf8'));
|
|
|
|
|
|
|
|
timer.done('extracted source map');
|
|
|
|
|
|
|
|
var bannerBundled =
|
|
|
|
concat([ bundled ])
|
|
|
|
.prepend(banner + '\n')
|
|
|
|
.add('//# sourceMappingURL=./' + variant + '.js.map')
|
|
|
|
.toStringWithSourceMap();
|
|
|
|
|
|
|
|
timer.done('added banner');
|
|
|
|
|
|
|
|
grunt.file.write(targetFileBase + '.js', bannerBundled.code, 'utf8');
|
|
|
|
grunt.file.write(targetFileBase + '.js.map', bannerBundled.map, 'utf8');
|
|
|
|
|
|
|
|
timer.done('all saved');
|
|
|
|
} catch (e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
function next(err) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fn = fns.shift();
|
|
|
|
|
|
|
|
if (!fn) {
|
|
|
|
return done();
|
|
|
|
} else {
|
|
|
|
fn(next);
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 13:53:09 +00:00
|
|
|
|
2017-11-19 12:31:06 +00:00
|
|
|
next();
|
2015-01-11 22:42:27 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|