Skip to content

Commit

Permalink
perf: build browser bundles at build-time
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovRoman committed May 8, 2024
1 parent 1ac9d1b commit 9547d08
Show file tree
Hide file tree
Showing 26 changed files with 651 additions and 514 deletions.
568 changes: 441 additions & 127 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"typings"
],
"scripts": {
"build": "tsc --build && npm run copy-static && npm run build-bundle -- --minify",
"build-bundle": "esbuild ./src/bundle/index.ts --outdir=./build/src/bundle --bundle --format=cjs --platform=node --target=ES2021",
"copy-static": "copyfiles 'src/browser/client-scripts/*' build",
"build": "tsc --build && npm run build-bundles",
"build-node-bundle": "esbuild ./src/bundle/index.ts --outdir=./build/src/bundle --bundle --format=cjs --platform=node --target=ES2021",
"build-browser-bundle": "node ./src/browser/client-scripts/build.js",
"build-bundles": "concurrently -c 'auto' 'npm:build-browser-bundle' 'npm:build-node-bundle --minify'",
"check-types": "tsc --project tsconfig.spec.json",
"clean": "rimraf build/ *.tsbuildinfo",
"lint": "eslint --cache . && prettier --check .",
Expand All @@ -24,9 +25,9 @@
"preversion": "npm run lint && npm test",
"commitmsg": "commitlint -e",
"release": "standard-version",
"watch": "npm run copy-static && concurrently -c 'auto' 'npm:watch:src' 'npm:watch:bundle'",
"watch": "npm run build-browser-bundle && concurrently -c 'auto' 'npm:watch:src' 'npm:watch:bundle'",
"watch:src": "tsc --build --watch",
"watch:bundle": "npm run build-bundle -- --watch"
"watch:bundle": "npm run build-node-bundle -- --watch"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -56,9 +57,7 @@
"@wdio/types": "8.21.0",
"@wdio/utils": "8.35.0",
"@wdio/utils-cjs": "npm:@wdio/utils@7.26.0",
"aliasify": "1.9.0",
"bluebird": "3.5.1",
"browserify": "13.3.0",
"chalk": "2.4.2",
"clear-require": "1.0.1",
"debug": "2.6.9",
Expand Down Expand Up @@ -86,7 +85,6 @@
"strftime": "0.10.2",
"strip-ansi": "6.0.1",
"temp": "0.8.3",
"uglifyify": "3.0.4",
"urijs": "1.19.11",
"url-join": "4.0.1",
"vite": "5.1.6",
Expand Down Expand Up @@ -121,7 +119,9 @@
"@types/url-join": "4.0.3",
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0",
"aliasify": "1.9.0",
"app-module-path": "2.2.0",
"browserify": "13.3.0",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"concurrently": "8.2.2",
Expand All @@ -145,7 +145,8 @@
"standard-version": "9.5.0",
"ts-node": "10.9.1",
"type-fest": "3.11.1",
"typescript": "5.3.2"
"typescript": "5.3.2",
"uglifyify": "3.0.4"
},
"peerDependencies": {
"ts-node": ">=10.5.0"
Expand Down
134 changes: 0 additions & 134 deletions src/browser/calibrator.js

This file was deleted.

Empty file.
Empty file added src/browser/calibrator/index.js
Empty file.
1 change: 0 additions & 1 deletion src/browser/client-bridge/client-bridge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const Promise = require("bluebird");
const { ClientBridgeError } = require("./error");

module.exports = class ClientBridge {
Expand Down
46 changes: 0 additions & 46 deletions src/browser/client-bridge/index.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/browser/client-bridge/index.ts
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);
};
67 changes: 67 additions & 0 deletions src/browser/client-scripts/build.js
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();

0 comments on commit 9547d08

Please sign in to comment.