-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: build browser bundles at build-time
- Loading branch information
1 parent
1ac9d1b
commit 37abf5f
Showing
23 changed files
with
692 additions
and
306 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,25 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
const Promise = require("bluebird"); | ||
const browserify = require("browserify"); | ||
const fs = require("fs"); | ||
const ClientBridge = require("./client-bridge"); | ||
|
||
const bundlesCache = {}; | ||
|
||
exports.ClientBridge = ClientBridge; | ||
|
||
exports.build = (browser, opts = {}) => { | ||
const script = browserify({ | ||
entries: "./index", | ||
basedir: path.join(__dirname, "..", "client-scripts"), | ||
}); | ||
|
||
script.transform( | ||
{ | ||
sourcemap: false, | ||
global: true, | ||
compress: { screw_ie8: false }, // eslint-disable-line camelcase | ||
mangle: { screw_ie8: false }, // eslint-disable-line camelcase | ||
output: { screw_ie8: false }, // eslint-disable-line camelcase | ||
}, | ||
"uglifyify", | ||
); | ||
|
||
const lib = opts.calibration && opts.calibration.needsCompatLib ? "./lib.compat.js" : "./lib.native.js"; | ||
const ignoreAreas = opts.supportDeprecated ? "./ignore-areas.deprecated.js" : "./ignore-areas.js"; | ||
|
||
script.transform( | ||
{ | ||
aliases: { | ||
"./lib": { relative: lib }, | ||
"./ignore-areas": { relative: ignoreAreas }, | ||
}, | ||
verbose: false, | ||
}, | ||
"aliasify", | ||
); | ||
|
||
return Promise.fromCallback(cb => script.bundle(cb)).then(buf => { | ||
const scripts = buf.toString(); | ||
|
||
return ClientBridge.create(browser, scripts); | ||
}); | ||
exports.build = async (browser, opts = {}) => { | ||
const needsCompatLib = opts.calibration && opts.calibration.needsCompatLib; | ||
|
||
const scriptFileName = needsCompatLib ? "bundle.compat.js" : "bundle.js"; | ||
|
||
if (bundlesCache[scriptFileName]) { | ||
return ClientBridge.create(browser, bundlesCache[scriptFileName]); | ||
} | ||
|
||
const scriptFilePath = path.join(__dirname, "..", "client-scripts", scriptFileName); | ||
const bundle = await fs.promises.readFile(scriptFilePath, { encoding: "utf8" }); | ||
bundlesCache[scriptFileName] = bundle; | ||
|
||
return ClientBridge.create(browser, bundle); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const path = require("path"); | ||
const browserify = require("browserify"); | ||
const uglifyify = require("uglifyify"); | ||
const aliasify = require("aliasify"); | ||
const fs = require("fs-extra"); | ||
|
||
/** | ||
* @param {object} opts | ||
* @param {boolean} opts.needsCompatLib | ||
* @returns {Promise<Buffer>} | ||
*/ | ||
const bundleScript = async opts => { | ||
const lib = opts.needsCompatLib ? "./lib.compat.js" : "./lib.native.js"; | ||
const script = browserify({ | ||
entries: "./scripts/index.js", | ||
basedir: __dirname | ||
}); | ||
|
||
script.transform( | ||
{ | ||
sourcemap: false, | ||
global: true, | ||
compress: { screw_ie8: false }, // eslint-disable-line camelcase | ||
mangle: { screw_ie8: false }, // eslint-disable-line camelcase | ||
output: { screw_ie8: false } // eslint-disable-line camelcase | ||
}, | ||
uglifyify | ||
); | ||
|
||
script.transform( | ||
{ | ||
aliases: { | ||
"./lib": { relative: lib } | ||
}, | ||
verbose: false | ||
}, | ||
aliasify | ||
); | ||
|
||
return new Promise((resolve, reject) => { | ||
script.bundle((err, buffer) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
|
||
resolve(buffer); | ||
}); | ||
}); | ||
}; | ||
|
||
async function main() { | ||
const targetDir = path.join("build", path.relative(process.cwd(), __dirname)); | ||
|
||
await fs.ensureDir(targetDir); | ||
|
||
await Promise.all( | ||
[ | ||
{ needsCompatLib: true, fileName: "bundle.compat.js" }, | ||
{ needsCompatLib: false, fileName: "bundle.js" } | ||
].map(async ({ needsCompatLib, fileName }) => { | ||
const buffer = await bundleScript({ needsCompatLib }); | ||
const filePath = path.join(targetDir, fileName); | ||
|
||
await fs.writeFile(filePath, buffer); | ||
}) | ||
); | ||
} | ||
|
||
module.exports = main(); |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.