Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc(build): extract bfcache strings from devtools #14452

Merged
merged 3 commits into from
Oct 21, 2022
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
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