Skip to content

Commit c3a362a

Browse files
committed
fix: speed up install deps
1 parent a37f5fa commit c3a362a

File tree

23 files changed

+3855
-8669
lines changed

23 files changed

+3855
-8669
lines changed

package-lock.json

Lines changed: 3604 additions & 8432 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"build": "tsc --build && npm run copy-static && npm run build-bundles",
1212
"copy-static": "copyfiles 'src/browser/client-scripts/*' build",
13-
"build-node-bundle": "esbuild ./src/bundle/index.ts --outdir=./build/src/bundle --bundle --format=cjs --platform=node --target=ES2021",
13+
"build-node-bundle": "esbuild ./src/bundle/cjs/index.ts --outdir=./build/src/bundle/cjs --bundle --format=cjs --platform=node --target=ES2021",
1414
"build-browser-bundle": "node ./src/browser/client-scripts/build.js",
1515
"build-bundles": "concurrently -c 'auto' 'npm:build-browser-bundle' 'npm:build-node-bundle --minify'",
1616
"check-types": "tsc --project tsconfig.spec.json",
@@ -55,14 +55,9 @@
5555
"@gemini-testing/commander": "2.15.4",
5656
"@jspm/core": "2.0.1",
5757
"@puppeteer/browsers": "2.4.0",
58-
"@types/debug": "4.1.12",
59-
"@types/yallist": "4.0.4",
6058
"@vitest/spy": "2.1.4",
61-
"@wdio/globals": "8.39.0",
6259
"@wdio/protocols": "8.38.0",
63-
"@wdio/types": "8.39.0",
6460
"@wdio/utils": "8.39.0",
65-
"@wdio/utils-cjs": "npm:@wdio/[email protected]",
6661
"bluebird": "3.5.1",
6762
"chalk": "2.4.2",
6863
"clear-require": "1.0.1",
@@ -78,7 +73,6 @@
7873
"geckodriver": "4.5.0",
7974
"gemini-configparser": "1.4.1",
8075
"get-port": "5.1.1",
81-
"glob-extra": "5.0.2",
8276
"import-meta-resolve": "4.0.0",
8377
"local-pkg": "0.4.3",
8478
"lodash": "4.17.21",
@@ -116,7 +110,7 @@
116110
"@commitlint/config-conventional": "^19.0.3",
117111
"@cspotcode/source-map-support": "0.8.0",
118112
"@sinonjs/fake-timers": "10.3.0",
119-
"@swc/core": "1.3.40",
113+
"@swc/core": "1.9.2",
120114
"@types/babel__code-frame": "7.0.6",
121115
"@types/babel__core": "7.20.5",
122116
"@types/bluebird": "3.5.38",
@@ -125,6 +119,7 @@
125119
"@types/chai-as-promised": "7.1.5",
126120
"@types/clear-require": "3.2.1",
127121
"@types/cli-progress": "3.11.6",
122+
"@types/debug": "4.1.12",
128123
"@types/escape-string-regexp": "2.0.1",
129124
"@types/fs-extra": "11.0.4",
130125
"@types/lodash": "4.14.191",
@@ -137,8 +132,11 @@
137132
"@types/sinonjs__fake-timers": "8.1.2",
138133
"@types/urijs": "1.19.25",
139134
"@types/url-join": "4.0.3",
135+
"@types/yallist": "4.0.4",
140136
"@typescript-eslint/eslint-plugin": "6.12.0",
141137
"@typescript-eslint/parser": "6.12.0",
138+
"@wdio/types": "8.39.0",
139+
"@wdio/utils-cjs": "npm:@wdio/[email protected]",
142140
"aliasify": "1.9.0",
143141
"app-module-path": "2.2.0",
144142
"browserify": "13.3.0",
@@ -151,6 +149,7 @@
151149
"eslint": "8.25.0",
152150
"eslint-config-gemini-testing": "2.8.0",
153151
"eslint-config-prettier": "8.7.0",
152+
"glob-extra": "5.0.2",
154153
"husky": "0.11.4",
155154
"jsdom": "^24.0.0",
156155
"jsdom-global": "3.0.2",

src/browser/browser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import crypto from "crypto";
22
import { RequestOptions } from "https";
3-
4-
import { RemoteCapability } from "@wdio/types/build/Capabilities";
53
import _ from "lodash";
64

75
import { SAVE_HISTORY_MODE } from "../constants/config";
@@ -16,6 +14,8 @@ import { BrowserConfig } from "../config/browser-config";
1614
import Callstack from "./history/callstack";
1715
import type { WdProcess, WebdriverPool } from "../browser-pool/webdriver-pool";
1816

17+
import type { Capabilities } from "@wdio/types";
18+
1919
const CUSTOM_SESSION_OPTS = [
2020
"outputDir",
2121
"agent",
@@ -184,7 +184,7 @@ export class Browser {
184184
return this._state;
185185
}
186186

187-
get capabilities(): RemoteCapability {
187+
get capabilities(): Capabilities.RemoteCapability {
188188
return this.publicAPI.capabilities;
189189
}
190190

src/browser/existing-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const url = require("url");
55
const Promise = require("bluebird");
66
const _ = require("lodash");
77
const webdriverio = require("webdriverio");
8-
const { sessionEnvironmentDetector } = require("@wdio/utils-cjs");
8+
const { sessionEnvironmentDetector } = require("../bundle/@wdio-utils");
99
const { Browser } = require("./browser");
1010
const commandsList = require("./commands");
1111
const Camera = require("./camera");

src/bundle/@wdio-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const bundle = require("./cjs");
3+
4+
export const sessionEnvironmentDetector: typeof import("@wdio/utils-cjs").sessionEnvironmentDetector =
5+
bundle.wdioUtils.sessionEnvironmentDetector;

src/bundle/cjs/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./test-transformer";
2+
export * from "./vendors/@wdio/utils-cjs";
3+
export * from "./vendors/glob-extra";

src/bundle/cjs/test-transformer.ts

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import * as nodePath from "node:path";
2+
import * as babel from "@babel/core";
3+
import { addHook } from "pirates";
4+
import { requireModuleSync } from "../../utils/module";
5+
6+
import type { NodePath, PluginObj, TransformOptions } from "@babel/core";
7+
8+
const STYLE_EXTESTION_RE = /\.(css|less|scss|sass|styl|stylus|pcss)$/;
9+
const IGNORE_STYLE_ERRORS = ["Unexpected token"];
10+
11+
export const TRANSFORM_EXTENSIONS = [".jsx", ".cjsx", ".mjsx", ".tsx", ".ctsx", ".mtsx"];
12+
export const JS_EXTENSION_RE = /^\.([cm]?[tj]sx?|json)$/;
13+
14+
export const setupTransformHook = (opts: { removeNonJsImports?: boolean } = {}): VoidFunction => {
15+
const transformOptions: TransformOptions = {
16+
browserslistConfigFile: false,
17+
babelrc: false,
18+
configFile: false,
19+
compact: false,
20+
presets: [require("@babel/preset-typescript")],
21+
sourceMaps: "inline",
22+
plugins: [
23+
[
24+
require("@babel/plugin-transform-react-jsx"),
25+
{
26+
throwIfNamespace: false,
27+
runtime: "automatic",
28+
},
29+
],
30+
require("@babel/plugin-transform-modules-commonjs"),
31+
],
32+
};
33+
34+
const customIgnoreImportsPlugin = ({ types: t }: { types: typeof babel.types }): PluginObj => ({
35+
name: "ignore-imports",
36+
visitor: {
37+
ImportDeclaration(path: NodePath<babel.types.ImportDeclaration>): void {
38+
const extname = nodePath.extname(path.node.source.value);
39+
40+
if (extname && !extname.match(JS_EXTENSION_RE)) {
41+
path.remove();
42+
return;
43+
}
44+
45+
try {
46+
requireModuleSync(path.node.source.value);
47+
} catch (err) {
48+
if (shouldIgnoreImportError(err as Error)) {
49+
path.remove();
50+
return;
51+
}
52+
53+
if ((err as NodeJS.ErrnoException).code === "ERR_REQUIRE_ESM") {
54+
mockEsmModuleImport(t, path);
55+
return;
56+
}
57+
}
58+
},
59+
},
60+
});
61+
62+
if (opts.removeNonJsImports) {
63+
transformOptions.plugins!.push([customIgnoreImportsPlugin]);
64+
}
65+
66+
const revertTransformHook = addHook(
67+
(originalCode, filename) => {
68+
return babel.transform(originalCode, { filename, ...transformOptions })!.code as string;
69+
},
70+
{ exts: TRANSFORM_EXTENSIONS },
71+
);
72+
73+
return revertTransformHook;
74+
};
75+
76+
function shouldIgnoreImportError(err: Error): boolean {
77+
const shouldIgnoreImport = IGNORE_STYLE_ERRORS.some(ignoreImportErr => {
78+
return (err as Error).message.startsWith(ignoreImportErr);
79+
});
80+
81+
if (!shouldIgnoreImport) {
82+
return false;
83+
}
84+
85+
const firstStackFrame = (err as Error).stack?.split("\n")[0] || "";
86+
const filePath = firstStackFrame.split(":")[0];
87+
const isStyleFilePath = STYLE_EXTESTION_RE.test(filePath);
88+
89+
return isStyleFilePath;
90+
}
91+
92+
/**
93+
* Replace esm module import with a Proxy.
94+
* Examples:
95+
* 1) `import pkg from "package"` -> `const pkg = new Proxy({}, {get: ..., apply: ...})`
96+
* 2) `import {a, b as c} from "package"` -> `const {a, c} = new Proxy({}, {get: ..., apply: ...})`
97+
*/
98+
function mockEsmModuleImport(t: typeof babel.types, path: NodePath<babel.types.ImportDeclaration>): void {
99+
const variableKey = genVarDeclKey(t, path.node);
100+
const variableValue = genProxy(t, [
101+
t.objectExpression([]),
102+
t.objectExpression([genProxyGetHandler(t), genProxyApplyHandler(t)]),
103+
]);
104+
105+
const variableDecl = t.variableDeclaration("const", [t.variableDeclarator(variableKey, variableValue)]);
106+
107+
path.replaceWith(variableDecl);
108+
}
109+
110+
/**
111+
* Generates the name of variables from the import declaration.
112+
* Examples:
113+
* 1) `import pkg from "package"` -> `pkg`
114+
* 2) `import {a, b as c} from "package"` -> `const {a, с} `
115+
*/
116+
function genVarDeclKey(
117+
t: typeof babel.types,
118+
node: NodePath<babel.types.ImportDeclaration>["node"],
119+
): babel.types.Identifier | babel.types.ObjectPattern {
120+
if (node.specifiers.length === 1) {
121+
if (["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(node.specifiers[0].type)) {
122+
return t.identifier(node.specifiers[0].local.name);
123+
}
124+
125+
return t.objectPattern([
126+
t.objectProperty(
127+
t.identifier(node.specifiers[0].local.name),
128+
t.identifier(node.specifiers[0].local.name),
129+
false,
130+
true,
131+
),
132+
]);
133+
}
134+
135+
const objectProperties = node.specifiers.map(spec => {
136+
return t.objectProperty(t.identifier(spec.local.name), t.identifier(spec.local.name), false, true);
137+
});
138+
139+
return t.objectPattern(objectProperties);
140+
}
141+
142+
// Generates Proxy expression with passed arguments: `new Proxy(args)`
143+
function genProxy(t: typeof babel.types, args: babel.types.Expression[]): babel.types.NewExpression {
144+
return t.newExpression(t.identifier("Proxy"), args);
145+
}
146+
147+
/**
148+
* Generates "get" handler for Proxy:
149+
*
150+
* get: function (target, prop) {
151+
* return prop in target ? target[prop] : new Proxy(() => {}, this);
152+
* }
153+
*/
154+
function genProxyGetHandler(t: typeof babel.types): babel.types.ObjectProperty {
155+
return t.objectProperty(
156+
t.identifier("get"),
157+
t.functionExpression(
158+
null,
159+
[t.identifier("target"), t.identifier("prop")],
160+
t.blockStatement([
161+
t.returnStatement(
162+
t.conditionalExpression(
163+
t.binaryExpression("in", t.identifier("prop"), t.identifier("target")),
164+
t.memberExpression(t.identifier("target"), t.identifier("prop"), true),
165+
genProxy(t, [t.arrowFunctionExpression([], t.blockStatement([])), t.thisExpression()]),
166+
),
167+
),
168+
]),
169+
),
170+
);
171+
}
172+
173+
/**
174+
* Generates "apply" handler for Proxy:
175+
*
176+
* apply: function () {
177+
* return new Proxy(() => {}, this);
178+
* }
179+
*/
180+
function genProxyApplyHandler(t: typeof babel.types): babel.types.ObjectProperty {
181+
return t.objectProperty(
182+
t.identifier("apply"),
183+
t.functionExpression(
184+
null,
185+
[],
186+
t.blockStatement([
187+
t.returnStatement(
188+
genProxy(t, [t.arrowFunctionExpression([], t.blockStatement([])), t.thisExpression()]),
189+
),
190+
]),
191+
),
192+
);
193+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as wdioUtilsFns from "@wdio/utils-cjs";
2+
export const wdioUtils = wdioUtilsFns;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as globExtraFns from "glob-extra";
2+
export const globExtra = globExtraFns;

src/bundle/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)