Skip to content

Commit 87741e6

Browse files
committed
detect playwright version
1 parent 2e46fe7 commit 87741e6

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

packages/build/src/extensions/playwright.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";
2+
import type { BuildManifest, BuildTarget } from "@trigger.dev/core/v3";
23

34
type PlaywrightBrowser = "chromium" | "firefox" | "webkit";
45

@@ -14,6 +15,11 @@ interface PlaywrightExtensionOptions {
1415
* @default true
1516
*/
1617
headless?: boolean;
18+
19+
/**
20+
* Playwright version override. If not provided, we will try to detect the version automatically.
21+
*/
22+
version?: string;
1723
}
1824

1925
/**
@@ -204,20 +210,51 @@ export function playwright(options: PlaywrightExtensionOptions = {}) {
204210
*/
205211
class PlaywrightExtension implements BuildExtension {
206212
public readonly name = "PlaywrightExtension";
207-
private readonly options: Required<PlaywrightExtensionOptions>;
213+
private moduleExternals: string[];
214+
215+
private readonly options: Required<Omit<PlaywrightExtensionOptions, "version">> & {
216+
version?: string;
217+
};
208218

209-
constructor({ browsers = ["chromium"], headless = true }: PlaywrightExtensionOptions = {}) {
219+
constructor({
220+
browsers = ["chromium"],
221+
headless = true,
222+
version,
223+
}: PlaywrightExtensionOptions = {}) {
210224
if (browsers && browsers.length === 0) {
211225
throw new Error("At least one browser must be specified");
212226
}
213-
this.options = { browsers, headless };
227+
this.options = { browsers, headless, version };
228+
this.moduleExternals = ["playwright"];
229+
}
230+
231+
externalsForTarget(target: BuildTarget) {
232+
if (target === "dev") {
233+
return [];
234+
}
235+
236+
return this.moduleExternals;
214237
}
215238

216-
onBuildComplete(context: BuildContext) {
239+
onBuildComplete(context: BuildContext, manifest: BuildManifest) {
217240
if (context.target === "dev") return;
218241

242+
// Detect Playwright version from manifest.externals or use override
243+
const playwrightExternal = manifest.externals?.find(
244+
(external: any) => external.name === "playwright" || external.name === "@playwright/test"
245+
);
246+
const version = playwrightExternal?.version ?? this.options.version;
247+
248+
if (!version) {
249+
throw new Error(
250+
"PlaywrightExtension could not determine the version of playwright. Please provide a version in the PlaywrightExtension options."
251+
);
252+
}
253+
219254
context.logger.debug(
220-
`Adding ${this.name} to the build with browsers: ${this.options.browsers.join(", ")}`
255+
`Adding ${this.name} to the build with browsers: ${this.options.browsers.join(
256+
", "
257+
)}, version: ${version}`
221258
);
222259

223260
const instructions: string[] = [
@@ -231,8 +268,8 @@ class PlaywrightExtension implements BuildExtension {
231268
npm \
232269
&& apt-get clean && rm -rf /var/lib/apt/lists/*`,
233270

234-
// Install Playwright globally
235-
`RUN npm install -g playwright`,
271+
// Install Playwright globally with detected version
272+
`RUN npm install -g playwright@${version}`,
236273
];
237274

238275
const deps = [...debian12Deps.tools, ...Object.values(debian12Deps.lib2package)];
@@ -299,6 +336,9 @@ class PlaywrightExtension implements BuildExtension {
299336
env: envVars,
300337
override: true,
301338
},
339+
dependencies: {
340+
playwright: version,
341+
},
302342
});
303343
}
304344
}

0 commit comments

Comments
 (0)