-
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 9547d08
Showing
26 changed files
with
651 additions
and
514 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 was deleted.
Oops, something went wrong.
Empty file.
Empty file.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use strict"; | ||
|
||
import path from "path"; | ||
import fs from "fs"; | ||
import ClientBridgeOriginal from "./client-bridge"; | ||
|
||
type BuildOpts = { calibration?: { needsCompatLib?: boolean }}; | ||
|
||
const bundlesCache: Record<string, string> = {}; | ||
|
||
export const ClientBridge = ClientBridgeOriginal; | ||
|
||
export const build = async (browser: WebdriverIO.Browser, opts: BuildOpts = {}) => { | ||
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", "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,67 @@ | ||
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(); |
Oops, something went wrong.