Skip to content

Chrome DevTools for coding agents

build: generate third-party notices from non-tree-shaken metafile inputs (#3472)

Nikolay Vitkove49d8264c8ac
diff --git a/package.json b/package.json
index 69a8844a..3611632d 100644
--- a/package.json
+++ b/package.json
@@ -46,6 +46,7 @@
"!**/*.js.map",
"!**/*.d.ts",
"!**/*-tsconfig*.json",
+ "!**/*.meta.json",
"!**/*.stamp",
"!**/*.d"
],
diff --git a/scripts/generate-third-party-notices.ts b/scripts/generate-third-party-notices.ts
index e2f0aee2..29a80c42 100644
--- a/scripts/generate-third-party-notices.ts
+++ b/scripts/generate-third-party-notices.ts
@@ -6,8 +6,8 @@
/**
* @file Writes THIRD_PARTY_NOTICES and bundled-packages.json for the bundles
- * in src/third_party. The esbuild depfiles list the files of each bundle.
- * Based on `generateThirdPartyNotices` in Lighthouse's
+ * in src/third_party. The esbuild metafiles list the files included in each
+ * bundle. Based on `generateThirdPartyNotices` in Lighthouse's
* build/build-bundle-mcp.js.
*/
@@ -56,6 +56,14 @@ function getString(value: unknown, key: string): string | undefined {
return typeof property === 'string' ? property : undefined;
}
+function getNumber(value: unknown, key: string): number | undefined {
+ if (!isRecord(value)) {
+ return undefined;
+ }
+ const property = value[key];
+ return typeof property === 'number' ? property : undefined;
+}
+
function getRecord(value: unknown, key: string): Record<string, unknown> {
if (!isRecord(value)) {
return {};
@@ -72,6 +80,18 @@ function readText(filePath: string): string {
return fs.readFileSync(filePath, 'utf-8').replaceAll('\r', '');
}
+function writeIfChanged(filePath: string, content: string): void {
+ try {
+ if (fs.readFileSync(filePath, 'utf-8') === content) {
+ return;
+ }
+ } catch {
+ // File does not exist yet.
+ }
+ fs.mkdirSync(path.dirname(filePath), {recursive: true});
+ fs.writeFileSync(filePath, content);
+}
+
function formatNotice(notice: Notice): string {
const lines = [
`Name: ${notice.name}`,
@@ -94,15 +114,42 @@ function assertAllowedLicense(license: string, source: string): void {
}
}
-/** Returns the files of all bundles as paths relative to the project root. */
-function readBundledFiles(depfiles: string[]): Set<string> {
+function isVirtualModule(modulePath: string): boolean {
+ return (
+ /^(?:[a-z][a-z0-9+.-]+|\([^)]+\)):/i.test(modulePath) ||
+ modulePath.startsWith('<')
+ );
+}
+
+/**
+ * Returns the non-tree-shaken input files of all bundles as paths relative to
+ * the project root.
+ */
+function readBundledFiles(metafiles: string[]): Set<string> {
const files = new Set<string>();
- for (const depfile of depfiles) {
- const content = fs.readFileSync(depfile, 'utf-8');
- const inputs = content.slice(content.indexOf(': ') + 2);
- for (const input of inputs.split(/\s+/)) {
- if (input) {
- const relativePath = path.relative(projectRoot, path.resolve(input));
+ for (const metafilePath of metafiles) {
+ const metafile = readJson(metafilePath);
+ const outputs = getRecord(metafile, 'outputs');
+ const jsOutputs = Object.entries(outputs).filter(([outputPath]) =>
+ outputPath.endsWith('.js'),
+ );
+ if (jsOutputs.length === 0) {
+ throw new Error(`No .js output found in metafile ${metafilePath}`);
+ }
+ for (const [, output] of jsOutputs) {
+ const inputs = getRecord(output, 'inputs');
+ for (const [inputPath, info] of Object.entries(inputs)) {
+ if (isVirtualModule(inputPath)) {
+ continue;
+ }
+ const bytesInOutput = getNumber(info, 'bytesInOutput');
+ if (bytesInOutput === undefined || bytesInOutput <= 0) {
+ continue;
+ }
+ const relativePath = path.relative(
+ projectRoot,
+ path.resolve(inputPath),
+ );
files.add(relativePath.replaceAll('\\', '/'));
}
}
@@ -245,13 +292,13 @@ function writeBundledPackages(
if (devToolsCommit) {
bundled[DEVTOOLS_URL] = devToolsCommit;
}
- fs.writeFileSync(outputPath, JSON.stringify(bundled, null, 2));
+ writeIfChanged(outputPath, JSON.stringify(bundled, null, 2));
}
function main(): void {
const {values} = parseArgs({
options: {
- depfile: {type: 'string', multiple: true, default: []},
+ metafile: {type: 'string', multiple: true, default: []},
notices: {type: 'string'},
'bundled-packages': {type: 'string'},
},
@@ -259,16 +306,16 @@ function main(): void {
});
const noticesPath = values.notices;
const bundledPackagesPath = values['bundled-packages'];
- if (!values.depfile.length || !noticesPath || !bundledPackagesPath) {
+ if (!values.metafile.length || !noticesPath || !bundledPackagesPath) {
throw new Error(
- 'Usage: generate-third-party-notices.ts --depfile <path>... ' +
+ 'Usage: generate-third-party-notices.ts --metafile <path>... ' +
'--notices <path> --bundled-packages <path>',
);
}
const packageDirs = new Set<string>();
const devToolsThirdPartyDirs = new Set<string>();
- for (const file of readBundledFiles(values.depfile)) {
+ for (const file of readBundledFiles(values.metafile)) {
const packageDir = getPackageDir(file);
if (packageDir) {
packageDirs.add(packageDir);
@@ -325,7 +372,7 @@ function main(): void {
),
'utf-8',
);
- fs.writeFileSync(
+ writeIfChanged(
noticesPath,
`${notices.join(DIVIDER).trim()}${DIVIDER}${lighthouseNotices}`,
);
diff --git a/scripts/verify-npm-package.js b/scripts/verify-npm-package.js
index af00a5b5..14aaba4c 100644
--- a/scripts/verify-npm-package.js
+++ b/scripts/verify-npm-package.js
@@ -45,6 +45,7 @@ function verifyPackageContents() {
'.tsbuildinfo',
'-tsconfig.json',
'-tsconfig.ref.json',
+ '.meta.json',
'.stamp',
'.d',
];
diff --git a/src/BUILD.gn b/src/BUILD.gn
index 3c2070e5..8f4e99cb 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -63,6 +63,7 @@ template("third_party_bundle") {
entrypoint = "third_party/${invoker.entrypoint_name}.ts"
output_file_location =
"$target_gen_dir/third_party/${invoker.entrypoint_name}.js"
+ metafile = "$target_gen_dir/$target_name.meta.json"
bundle_all = true
platform = "node"
deps = [ ":third_party_sources" ]
@@ -112,7 +113,7 @@ third_party_bundle("third_party_heap_snapshot_worker_bundle") {
}
# Lists the licenses of the files in the bundles. The files come from the
-# depfiles that the `bundle` template writes next to each target.
+# metafiles that the `bundle` template writes next to each target.
action("third_party_notices") {
script = "//third_party/node/node.py"
_script = "//scripts/generate-third-party-notices.ts"
@@ -124,15 +125,10 @@ action("third_party_notices") {
"third_party_index_bundle",
]
- # The bundles change when their depfiles do. GN cannot track the depfiles.
inputs = [
_script,
"//package-lock.json",
"//package.json",
- "$_third_party_gen_dir/cli.js",
- "$_third_party_gen_dir/devtools-formatter-worker.js",
- "$_third_party_gen_dir/devtools-heap-snapshot-worker.js",
- "$_third_party_gen_dir/index.js",
"third_party/LIGHTHOUSE_MCP_BUNDLE_THIRD_PARTY_NOTICES",
]
deps = []
@@ -143,10 +139,12 @@ action("third_party_notices") {
args = [ rebase_path(_script, root_build_dir) ]
foreach(_bundle, _bundles) {
+ _metafile = "$target_gen_dir/$_bundle.meta.json"
deps += [ ":$_bundle" ]
+ inputs += [ _metafile ]
args += [
- "--depfile",
- rebase_path("$target_gen_dir/$_bundle.d", root_build_dir),
+ "--metafile",
+ rebase_path(_metafile, root_build_dir),
]
}
args += [