Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 63 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"@vitejs/plugin-basic-ssl": "2.3.0",
"@vitejs/plugin-react": "6.0.3",
"@zip.js/zip.js": "2.7.73",
"chokidar": "3.6.0",
"chokidar": "4.0.3",
"chromium-bidi": "12.1.0",
"colors": "1.4.0",
"commander": "15.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/serverRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import chokidar from 'chokidar';

import { packageJSON, packageRoot } from './package';

import type { FSWatcher } from 'chokidar';
// Only client depenencies with backward compatibility guarantees should be imported here.
import type { LaunchOptions } from '../types/types';

Expand Down Expand Up @@ -58,7 +59,7 @@ export interface ServerRegistryEvents {

class ServerRegistry extends EventEmitter {
private _descriptors = new Map<string, BrowserDescriptor>();
private _watcher: chokidar.FSWatcher | undefined;
private _watcher: FSWatcher | undefined;
private _watcherRefs = 0;
private _ready: Promise<void> | undefined;

Expand Down
3 changes: 0 additions & 3 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,5 @@
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.63.0-next"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
}
12 changes: 10 additions & 2 deletions packages/playwright/src/runner/fsWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ export class FSWatcher {
if (!this._watchedPaths.length)
return;

const ignored = [...this._ignoredFolders, '**/node_modules/**'];
// Chokidar calls the matcher with paths normalized to forward slashes.
const ignoredPrefixes = this._ignoredFolders.map(folder => folder.replace(/\\/g, '/') + '/');
const ignored = (file: string) => {
if (file.split('/').includes('node_modules'))
return true;
return ignoredPrefixes.some(prefix => (file + '/').startsWith(prefix));
};
this._fsWatcher = chokidar.watch(watchedPaths, { ignoreInitial: true, ignored }).on('all', async (event, file) => {
if (event !== 'add' && event !== 'addDir' && event !== 'change' && event !== 'unlink' && event !== 'unlinkDir')
return;
if (this._throttleTimer)
clearTimeout(this._throttleTimer);
this._collector.push({ event, file });
this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250);
});

await new Promise((resolve, reject) => this._fsWatcher!.once('ready', resolve).once('error', reject));
await new Promise<void>((resolve, reject) => this._fsWatcher!.once('ready', () => resolve()).once('error', reject));
}

async close() {
Expand Down
4 changes: 2 additions & 2 deletions tests/installation/bundle-licenses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { test, expect } from './npmTest';
// summary and is only checked for presence.
const EXPECTED: Record<string, Record<string, number>> = {
'playwright-core': {
'lib/serverRegistry.js.LICENSE': 10,
'lib/utilsBundle.js.LICENSE': 80,
'lib/serverRegistry.js.LICENSE': 2,
'lib/utilsBundle.js.LICENSE': 75,
// Vendored WebP codec (libwebp compiled to WASM), not a bundle sidecar.
'lib/webp_codec.LICENSE': 0,
},
Expand Down
65 changes: 54 additions & 11 deletions utils/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const chokidar = require('chokidar');
const fs = require('fs');
const { workspace } = require('../workspace');
const { build, context } = require('esbuild');
const { minimatch } = require('minimatch');

/**
* @typedef {{
Expand Down Expand Up @@ -79,6 +80,43 @@ function filePath(relative) {
return path.join(ROOT, ...relative.split('/'));
}

/**
* @param {string} p
* @returns {string}
*/
function toPosixPath(p) {
return p.split(path.sep).join('/');
}

/**
* Chokidar v4 dropped glob support: watch the static directory prefix of a
* glob instead, and filter emitted paths with `pathMatcher`.
* @param {string} pattern
* @returns {string}
*/
function globBase(pattern) {
const magicIndex = pattern.search(/[*?{[]/);
if (magicIndex === -1)
return pattern;
return pattern.slice(0, pattern.lastIndexOf(path.sep, magicIndex));
}

/**
* @param {string[]} patterns absolute files, directories or globs
* @returns {(file: string) => boolean}
*/
function pathMatcher(patterns) {
const posixPatterns = patterns.map(toPosixPath);
return file => {
const posixFile = toPosixPath(file);
return posixPatterns.some(pattern => {
if (pattern.search(/[*?{[]/) === -1)
return posixFile === pattern || posixFile.startsWith(pattern + '/');
return minimatch(posixFile, pattern, { dot: true });
});
};
}

/**
* Resolve a CLI shipped by a node_modules package to an absolute path, so we
* can spawn it via `node` directly instead of going through `npx`/`npm exec`
Expand Down Expand Up @@ -185,14 +223,15 @@ async function runWatch() {
clearTimeout(timeout);
timeout = setTimeout(callback, 500);
};
chokidar.watch([...paths, ...mustExist, onChange.script].filter(Boolean).map(filePath)).on('all', reschedule);
chokidar.watch([...paths, ...mustExist, onChange.script].filter(Boolean).map(filePath).map(globBase)).on('all', reschedule);
callback();
}

for (const { files, from, to, ignored } of copyFiles) {
const watcher = chokidar.watch([filePath(files)], { ignored });
const matches = pathMatcher([filePath(files)]);
const watcher = chokidar.watch(globBase(filePath(files)), { ignored: pathMatcher(ignored || []) });
watcher.on('all', (event, file) => {
if (event === 'add' || event === 'change')
if ((event === 'add' || event === 'change') && matches(file))
copyFile(file, from, to);
});
}
Expand All @@ -212,11 +251,11 @@ async function runWatch() {

async function runBuild() {
for (const { files, from, to, ignored } of copyFiles) {
const watcher = chokidar.watch([filePath(files)], {
ignored
});
const matches = pathMatcher([filePath(files)]);
const watcher = chokidar.watch(globBase(filePath(files)), { ignored: pathMatcher(ignored || []) });
watcher.on('add', file => {
copyFile(file, from, to);
if (matches(file))
copyFile(file, from, to);
});
await new Promise(x => watcher.once('ready', x));
watcher.close();
Expand Down Expand Up @@ -330,9 +369,14 @@ class EsbuildStep extends Step {
this._context = await context(this._options);
disposables.push(() => this._context?.dispose());

const watcher = chokidar.watch([...this._options.entryPoints, ...(this._watchPaths || [])]);
const watchPaths = [...this._options.entryPoints, ...(this._watchPaths || [])];
const matches = pathMatcher(watchPaths);
const watcher = chokidar.watch([...new Set(watchPaths.map(globBase))]);
await new Promise(x => watcher.once('ready', x));
watcher.on('all', () => this._rebuild());
watcher.on('all', (event, file) => {
if (matches(file))
this._rebuild();
});

await this._rebuild();
console.log('==== Esbuild watching:', this._relativeEntryPoints().join(', '), `(started in ${Date.now() - start}ms)`);
Expand Down Expand Up @@ -623,7 +667,6 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright-core/src/serverRegistry.js')],
outfile: filePath('packages/playwright-core/lib/serverRegistry.js'),
external: ['fsevents'],
}, [filePath('packages/playwright-core/src/*')]));

const playwrightCoreSrc = filePath('packages/playwright-core/src');
Expand All @@ -634,7 +677,7 @@ steps.push(new EsbuildStep({
bundle: true,
entryPoints: [filePath('packages/playwright-core/src/utilsBundle.ts')],
outfile: filePath('packages/playwright-core/lib/utilsBundle.js'),
external: ['fsevents', 'express', '@anthropic-ai/sdk'],
external: ['express', '@anthropic-ai/sdk'],
alias: {
'raw-body': filePath('utils/build/raw-body.ts'),
},
Expand Down
Loading