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

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

Merged
merged 1 commit into from
Oct 28, 2021
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
2 changes: 1 addition & 1 deletion build/build-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
5 changes: 5 additions & 0 deletions build/plugins/inline-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you want to go the extra mile and publish this plugin to npm eventually? I bet others would like to use it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not today but I left the comment so it would be clear there's not much to do if we ever do want to.

return LH_ROOT;
}
throw new AstError(`unsupported identifier '${node.name}'`, node);
}
Expand Down
12 changes: 12 additions & 0 deletions build/test/plugins/inline-fs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -42,4 +41,4 @@ const expectations = {
},
};

module.exports = {expectations};
export {expectations};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ const config = {
},
};

module.exports = config;
export default config;
2 changes: 1 addition & 1 deletion shared/localization/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down