loleaflet: Improved build process, don't build unnecessarily

No point in building things that are already built, create
different jake tasks, enclose whole of the build process in a
single jake task - 'build' which would call other various tasks
depending on whether they needs building.

Change-Id: I4b26834440ef638d697aa0793d2ced1e2c49784a
This commit is contained in:
Pranav Kant 2016-12-09 18:06:53 +05:30
parent b940b34ab1
commit ab9d744fe5
3 changed files with 121 additions and 39 deletions

View file

@ -19,7 +19,7 @@ function hint(msg, args) {
return function () {
console.log(msg);
jake.exec('node node_modules/eslint/bin/eslint.js ' + args,
{printStdout: true}, function () {
{printStdout: true}, function () {
console.log('\tCheck passed.\n');
complete();
});
@ -29,33 +29,73 @@ function hint(msg, args) {
desc('Check Leaflet source for errors with ESLint');
task('lint', {async: true}, hint('Checking for JS errors...', 'src dist --config .eslintrc'));
desc('Check admin source for errors with ESLint');
task('lintadmin', {async: true}, hint('Checking for admin JS errors...', 'src/admin --config .eslintrc'));
desc('Check Leaflet specs source for errors with ESLint');
task('lintspec', {async: true}, hint('Checking for specs JS errors...', 'spec/suites --config spec/.eslintrc'));
desc('Combine and compress Leaflet source files');
task('build', {async: true}, function (compsBase32, buildName) {
var v;
jake.exec('git log -1 --pretty=format:"%h"', {breakOnError: false}, function () {
build.build(complete, v, compsBase32, buildName);
}).on('stdout', function (data) {
v = version + ' (' + data.toString() + ')';
}).on('error', function () {
v = version;
desc('Create a combined leaflet file');
file('dist/leaflet-src.js', build.getFiles(), {async: true}, function() {
var lint = jake.Task['lint'];
lint.addListener('complete', function(value) {
var v;
jake.exec('git log -1 --pretty=format:"%h"', {breakOnError: false}, function () {
console.log('Building leaflet-src.js ...');
build.build(complete, v);
}).on('stdout', function (data) {
v = version + ' (' + data.toString() + ')';
}).on('error', function () {
v = version;
});
});
lint.invoke();
});
desc('Browserify and bundle all js and css files');
task('bundle', {async: true}, function (type, debug, minify) {
desc('Create a combined admin file');
file('dist/admin/admin-src.js', build.getAdminFiles(), {async: true}, function() {
var lint = jake.Task['lintadmin'];
lint.addListener('complete', function(value) {
console.log('Building admin-src.js ...');
build.buildadmin(complete);
});
lint.invoke();
});
desc('Create final bundled js file to be used by main lool editor');
file('dist/bundle.js', build.getBundleFiles(), {async: true}, function(debug, minify) {
debug = debug === 'true';
minify = minify === 'true';
if (type === 'admin') {
build.bundleAdmin(debug, minify);
} else {
build.bundle(debug, minify);
}
console.log('Creating bundle.js ...');
build.bundle(debug, minify, complete);
});
desc('Create final bundle js file to be used by admin console');
file('dist/admin-bundle.js', build.getAdminBundleFiles(), {async: true}, function(debug, minify) {
debug = debug === 'true';
minify = minify === 'true';
console.log('Creating admin-bundle.js ...');
build.bundleAdmin(debug, minify, complete);
});
desc('Create final bundled JS files');
task('build', {async: true}, function () {
// TODO: Build both admin-bundle and bundle parallely
var bundlejs = jake.Task['dist/bundle.js'];
bundlejs.addListener('complete', function(value) {
console.log('Finished building loleaflet');
complete();
});
var adminbundlejs = jake.Task['dist/admin-bundle.js'];
adminbundlejs.addListener('complete', function(value) {
console.log('Finished building admin');
bundlejs.invoke();
});
adminbundlejs.invoke();
});
desc('Run PhantomJS tests');

View file

@ -14,12 +14,9 @@ MINIFY=false
.PHONY: build
build:
npm install
jake lint
jake build
rm -rf dist/plugins/draw-$(DRAW_VERSION) && mkdir -p dist/plugins/draw-$(DRAW_VERSION)
cd plugins/draw-$(DRAW_VERSION) && jake build && cp -ar dist ../../dist/plugins/draw-$(DRAW_VERSION)
jake bundle[,$(DEBUG),$(MINIFY)]
jake bundle[admin,$(DEBUG),$(MINIFY)]
jake build
.PHONY: build-l10n
build-l10n:

View file

@ -8,6 +8,45 @@ var fs = require('fs'),
deps = require('./deps.js').deps,
adminDeps = require('./adminDeps.js').adminDeps;
// TODO: Don't hardcode leaflet-draw version here
var JSBundleFiles = [
'main.js',
'dist/leaflet-src.js',
'dist/errormessages.js',
'dist/plugins/draw-0.2.4/dist/leaflet.draw.js'
];
var CSSBundleFiles = [
'main.css',
'dist/leaflet.css',
'dist/selectionMarkers.css',
'dist/loleaflet.css',
'dist/toolbar.css',
'dist/partsPreviewControl.css',
'dist/scrollBar.css',
'dist/searchControl.css',
'dist/spreadsheet.css',
'plugins/draw-0.2.4/dist/leaflet.draw.css',
'dist/sm-simple.css'
];
var adminBundleFiles = [
'main-admin.js',
'dist/admin/admin-src.js',
'dist/admin/bootstrap/ie10-viewport-bug-workaround.css',
'admin.strings.js',
'dist/admin/bootstrap/holder.min.js',
'dist/admin/bootstrap/ie10-viewport-bug-workaround.js'
];
exports.getBundleFiles = function() {
return JSBundleFiles.concat(CSSBundleFiles);
};
exports.getAdminBundleFiles = function() {
return adminBundleFiles;
};
function getFiles(compsBase32) {
var memo = {},
comps;
@ -60,6 +99,7 @@ function getAdminFiles() {
}
exports.getFiles = getFiles;
exports.getAdminFiles = getAdminFiles;
function getSizeDelta(newContent, oldContent, fixCRLF) {
if (!oldContent) {
@ -97,7 +137,7 @@ function bytesToKB(bytes) {
return (bytes / 1024).toFixed(2) + ' KB';
}
function bundle(files, destFilename, debug, minify) {
function bundle(files, destFilename, debug, minify, callback) {
var bundler = browserify(files, {debug: debug});
bundler = bundler.transform(browserifyCss);
if (minify) {
@ -115,15 +155,16 @@ function bundle(files, destFilename, debug, minify) {
bundleFs.on('finish', function() {
console.log('Finish writing to dist/' + destFilename);
callback();
});
};
exports.bundle = function(debug, minify) {
bundle(['main.js'], 'bundle.js', debug, minify);
exports.bundle = function(debug, minify, callback) {
bundle(['main.js'], 'bundle.js', debug, minify, callback);
};
exports.bundleAdmin = function(debug, minify) {
bundle(['main-admin.js'], 'admin-bundle.js', debug, minify);
exports.bundleAdmin = function(debug, minify, callback) {
bundle(['main-admin.js'], 'admin-bundle.js', debug, minify, callback);
};
exports.build = function (callback, version, compsBase32, buildName) {
@ -150,17 +191,6 @@ exports.build = function (callback, version, compsBase32, buildName) {
console.log('\tSaved to ' + srcPath);
}
// Also combine and copy the admin JS files
// TODO: Also minify if admin complexity increases in future
var adminNewSrc = combineFiles(getAdminFiles()),
adminPath = 'dist/admin/admin-src.js',
adminOldSrc = loadSilently(adminPath);
if (adminNewSrc != adminOldSrc) {
fs.writeFileSync(adminPath, adminNewSrc);
console.log('\tAdmin files saved to ' + adminPath);
}
var path = pathPart + '.js',
oldCompressed = loadSilently(path),
newCompressed = copy + UglifyJS.minify(newSrc, {
@ -198,6 +228,21 @@ exports.build = function (callback, version, compsBase32, buildName) {
});
};
exports.buildadmin = function(callback) {
// TODO: Also minify if admin complexity increases in future
var adminNewSrc = combineFiles(getAdminFiles()),
adminPath = 'dist/admin/admin-src.js',
adminOldSrc = loadSilently(adminPath),
adminSrcDelta = getSizeDelta(adminNewSrc, adminOldSrc, true);
if (adminSrcDelta !== ' (unchanged)') {
fs.writeFileSync(adminPath, adminNewSrc);
console.log('\tAdmin files saved to ' + adminPath);
}
callback();
};
exports.test = function(complete, fail) {
var karma = require('karma'),
testConfig = {configFile : __dirname + '/../spec/karma.conf.js'};