From bdfd743f7dbed24909e5c4e5568e238faaf56cf1 Mon Sep 17 00:00:00 2001 From: Brendan Kenny Date: Thu, 28 Oct 2021 16:36:14 -0500 Subject: [PATCH] core(build): add LH_ROOT support to inline-fs (#13278) --- build/build-bundle.js | 2 +- build/plugins/inline-fs.js | 5 +++++ build/test/plugins/inline-fs-test.js | 12 ++++++++++++ .../test-definitions/source-maps/expectations.js | 9 ++++----- .../test-definitions/source-maps/package.json | 4 ---- .../source-maps/source-maps-config.js | 2 +- shared/localization/format.js | 2 +- 7 files changed, 24 insertions(+), 12 deletions(-) delete mode 100644 lighthouse-cli/test/smokehouse/test-definitions/source-maps/package.json diff --git a/build/build-bundle.js b/build/build-bundle.js index 9ab96169c3..2baace0a7c 100644 --- a/build/build-bundle.js +++ b/build/build-bundle.js @@ -62,7 +62,7 @@ async function browserifyFile(entryPath, distPath) { file: require.resolve('./banner.txt'), }) // Transform `fs.readFileSync`, etc into inline strings. - .transform(inlineFs({verbose: Boolean(process.env.DEBUG)})) + .transform(inlineFs({verbose: DEBUG})) // Strip everything out of package.json includes except for the version. .transform('package-json-versionify'); diff --git a/build/plugins/inline-fs.js b/build/plugins/inline-fs.js index cb57cecc61..1edf63ca0b 100644 --- a/build/plugins/inline-fs.js +++ b/build/plugins/inline-fs.js @@ -14,6 +14,8 @@ const MagicString = require('magic-string').default; const resolve = require('resolve'); const terser = require('terser'); +const {LH_ROOT} = require('../../root.js'); + // ESTree provides much better types for AST nodes. See https://github.com/acornjs/acorn/issues/946 /** @typedef {import('estree').Node} Node */ /** @typedef {import('estree').SimpleCallExpression} SimpleCallExpression */ @@ -280,6 +282,9 @@ function collapseToStringLiteral(node, filepath) { return path.dirname(filepath); } else if (node.name === '__filename') { return filepath; + } else if (node.name === 'LH_ROOT') { + // Note: hardcoded for LH. Could be be set via inline-fs options instead. + return LH_ROOT; } throw new AstError(`unsupported identifier '${node.name}'`, node); } diff --git a/build/test/plugins/inline-fs-test.js b/build/test/plugins/inline-fs-test.js index dd372393c6..e4b32ee350 100644 --- a/build/test/plugins/inline-fs-test.js +++ b/build/test/plugins/inline-fs-test.js @@ -241,6 +241,18 @@ describe('inline-fs', () => { }); }); + it('substitutes Lighthouse-specific LH_ROOT', async () => { + fs.writeFileSync(tmpPath, 'lh_root text content'); + + const constructedPath = '`${LH_ROOT}/.tmp/inline-fs/test.txt`'; + const content = `const myRootRelativeContent = fs.readFileSync(${constructedPath}, 'utf8');`; + const result = await inlineFs(content, filepath); + expect(result).toEqual({ + code: `const myRootRelativeContent = "lh_root text content";`, + warnings: [], + }); + }); + describe('fs.readFileSync', () => { it('inlines content with quotes', async () => { fs.writeFileSync(tmpPath, `"quoted", and an unbalanced quote: "`); diff --git a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js index 6bc07176a4..48d53434d5 100644 --- a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js +++ b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js @@ -5,12 +5,11 @@ */ 'use strict'; -const fs = require('fs'); +import fs from 'fs'; +import {LH_ROOT} from '../../../../../root.js'; -// TODO(esmodules): brfs does not support es modules, and this file needs to be bundlded, -// so it is commonjs for now. const mapJson = - fs.readFileSync(`${__dirname}/../../../fixtures/source-map/script.js.map`, 'utf-8'); + fs.readFileSync(`${LH_ROOT}/lighthouse-cli/test/fixtures/source-map/script.js.map`, 'utf-8'); const map = JSON.parse(mapJson); /** @@ -42,4 +41,4 @@ const expectations = { }, }; -module.exports = {expectations}; +export {expectations}; diff --git a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/package.json b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/package.json deleted file mode 100644 index 8d10bc1dfd..0000000000 --- a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "type": "commonjs", - "//": "Preserve commonjs in this directory. Temporary file until converted to type: module" -} diff --git a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/source-maps-config.js b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/source-maps-config.js index c671e55846..711d198f08 100644 --- a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/source-maps-config.js +++ b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/source-maps-config.js @@ -17,4 +17,4 @@ const config = { }, }; -module.exports = config; +export default config; diff --git a/shared/localization/format.js b/shared/localization/format.js index aa89d598c0..993ce58b56 100644 --- a/shared/localization/format.js +++ b/shared/localization/format.js @@ -17,7 +17,7 @@ const DEFAULT_LOCALE = 'en-US'; /** * The locale tags for the localized messages available to Lighthouse on disk. - * When bundled, these will be inlined by brfs. + * When bundled, these will be inlined by `inline-fs`. * These locales are considered the "canonical" locales. We support other locales which * are simply aliases to one of these. ex: es-AR (alias) -> es-419 (canonical) */