Skip to content

Commit

Permalink
misc(build): extract bfcache strings from devtools (#14452)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Oct 21, 2022
1 parent 70d5652 commit 5f0c10d
Show file tree
Hide file tree
Showing 8 changed files with 1,412 additions and 35 deletions.
89 changes: 63 additions & 26 deletions build/build-cdt-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import fs from 'fs';

import {LH_ROOT} from '../root.js';

// eslint-disable-next-line max-len
const inFile = `${LH_ROOT}/node_modules/chrome-devtools-frontend/front_end/models/issues_manager/DeprecationIssue.ts`;
const outFile = `${LH_ROOT}/core/lib/deprecations-strings.js`;

const code = fs.readFileSync(inFile, 'utf-8');

/**
* @param {string} text
* @param {string|RegExp} searchValue
Expand Down Expand Up @@ -44,24 +38,13 @@ function extract(text, startPattern, endPattern, replacements = []) {
return subText;
}

const uiStringsDeclare = extract(code, 'const UIStrings', '};', [
// Some patterns are supported in DevTools UIStrings, but not ours.
[/\\\\/g, ''],
[`\\'plan-b\\'`, 'plan-b'],
]);
const getDescriptionDeclare =
extract(code, 'getDescription(): MarkdownIssueDescription', '});\n }', [
['getDescription(): MarkdownIssueDescription', 'function getDescription(issueDetails)'],
['this.#issueDetails', 'issueDetails'],
[`let messageFunction = (): string => '';`, `let message;`],
[/messageFunction/g, 'message'],
[/i18nLazyString/g, 'str_'],
['resolveLazyDescription', ''],
['links,', 'links, message,'],
[/Protocol\.Audits\.DeprecationIssueType\.(\w+)/g, `'$1'`],
]);

fs.writeFileSync(outFile, `// auto-generated by build/build-cdt-strings.js
/**
* @param {string} uiStringsDeclare
* @param {string} extraCode
*/
function createStringsModule(uiStringsDeclare, extraCode) {
return `
// auto-generated by build/build-cdt-strings.js
/* eslint-disable */
import * as i18n from '../lib/i18n/i18n.js';
Expand All @@ -70,6 +53,35 @@ ${uiStringsDeclare}
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
${extraCode}`.trimStart();
}

// core/lib/deprecations-strings.js
{
// eslint-disable-next-line max-len
const inFile = `${LH_ROOT}/node_modules/chrome-devtools-frontend/front_end/models/issues_manager/DeprecationIssue.ts`;
const outFile = `${LH_ROOT}/core/lib/deprecations-strings.js`;

const input = fs.readFileSync(inFile, 'utf-8');

const uiStringsDeclare = extract(input, 'const UIStrings', '};', [
// Some patterns are supported in DevTools UIStrings, but not ours.
[/\\\\/g, ''],
[`\\'plan-b\\'`, 'plan-b'],
]);
const getDescriptionDeclare =
extract(input, 'getDescription(): MarkdownIssueDescription', '});\n }', [
['getDescription(): MarkdownIssueDescription', 'function getDescription(issueDetails)'],
['this.#issueDetails', 'issueDetails'],
[`let messageFunction = (): string => '';`, `let message;`],
[/messageFunction/g, 'message'],
[/i18nLazyString/g, 'str_'],
['resolveLazyDescription', ''],
['links,', 'links, message,'],
[/Protocol\.Audits\.DeprecationIssueType\.(\w+)/g, `'$1'`],
]);

const extraCode = `
/**
* @param {LH.Crdp.Audits.DeprecationIssueDetails} issueDetails
*/
Expand All @@ -78,5 +90,30 @@ ${getDescriptionDeclare}
export {
getDescription as getIssueDetailDescription,
UIStrings,
};
`);
};`;
fs.writeFileSync(outFile, createStringsModule(uiStringsDeclare, extraCode));
}

// core/lib/bfcache-strings.js
{
// eslint-disable-next-line max-len
const inFile = `${LH_ROOT}/node_modules/chrome-devtools-frontend/front_end/panels/application/components/BackForwardCacheStrings.ts`;
const outFile = `${LH_ROOT}/core/lib/bfcache-strings.js`;

const input = fs.readFileSync(inFile, 'utf-8');

const uiStringsDeclare = extract(input, 'const UIStrings', '};');
const notRestoredReasonDescriptionDeclare =
extract(input, 'const NotRestoredReasonDescription', '};', [
[/i18nLazyString/g, 'str_'],
]);

const extraCode = `
${notRestoredReasonDescriptionDeclare}
export {
NotRestoredReasonDescription,
UIStrings,
};`;
fs.writeFileSync(outFile, createStringsModule(uiStringsDeclare, extraCode));
}
Loading

0 comments on commit 5f0c10d

Please sign in to comment.