core(build): add LH_ROOT support to inline-fs (#13278)

This commit is contained in:
Brendan Kenny 2021-10-28 16:36:14 -05:00 коммит произвёл GitHub
Родитель 18b12a5b58
Коммит bdfd743f7d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 24 добавлений и 12 удалений

Просмотреть файл

@ -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');

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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: "`);

Просмотреть файл

@ -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};

Просмотреть файл

@ -1,4 +0,0 @@
{
"type": "commonjs",
"//": "Preserve commonjs in this directory. Temporary file until converted to type: module"
}

Просмотреть файл

@ -17,4 +17,4 @@ const config = {
},
};
module.exports = config;
export default config;

Просмотреть файл

@ -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)
*/