optimize browserify startup time

This commit is contained in:
Jare Guo
2017-05-04 15:47:14 +08:00
parent 633e976f69
commit 6c75eb09cb
2 changed files with 56 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
// Based on https://github.com/substack/browser-pack/blob/master/prelude.js
// modules are defined as an array
// [ module function, map of requireuires ]
//
// map of requireuires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the requireuire for previous bundles
(function outer (modules, cache, entry) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof require == "function" && require;
function newRequire(name, jumped){
var module = cache[name];
if(!module) {
var moduleData = modules[name];
if(!moduleData) {
// if we cannot find the module within our internal map or
// cache jump to the current global require ie. the last bundle
// that was added to the page.
var currentRequire = typeof require == "function" && require;
if (!jumped && currentRequire) return currentRequire(name, true);
// If there are other bundles on this page the require from the
// previous one is saved to 'previousRequire'. Repeat this as
// many times as there are bundles until the module is found or
// we exhaust the require chain.
if (previousRequire) return previousRequire(name, true);
var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
}
var exports = {};
module = cache[name] = {exports};
moduleData[0](function(x){
return newRequire(moduleData[1][x] || x);
},module,exports);
}
return module.exports;
}
for(var i=0;i<entry.length;i++) newRequire(entry[i]);
// Override the current require with this new one
return newRequire;
})
+8
View File
@@ -3,6 +3,10 @@
const Path = require('path');
allowReturnOutsideFunctionInBrowserifyTransform();
const Browserify = require('browserify');
const Fs = require('fs');
const preludePath = Path.resolve(__dirname, '../browserify_prelude.js');
const prelude = Fs.readFileSync(preludePath, 'utf8');
exports.uglifyOptions = function (minify, global_defs) {
if (minify) {
@@ -98,6 +102,10 @@ exports.createBundler = function createBundler(entryFiles, babelifyOpt) {
bundleExternal: false, // dont bundle external modules
//standalone: 'engine-framework',
//basedir: tempScriptDir
// define custom prelude to optimize script evaluate time
prelude: prelude,
preludePath: Path.relative(process.cwd(), preludePath),
};
// var presets = [