Skip to content

Commit 95e3993

Browse files
authored
core(build): inline-fs error if file missing, ignorePaths (#14436)
1 parent e27f9be commit 95e3993

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

build/build-bundle.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ async function buildBundle(entryPath, distPath, opts = {minify: true}) {
192192
}),
193193
rollupPlugins.json(),
194194
rollupPlugins.removeModuleDirCalls(),
195-
rollupPlugins.inlineFs({verbose: false}),
195+
rollupPlugins.inlineFs({
196+
verbose: Boolean(process.env.DEBUG),
197+
ignorePaths: [
198+
require.resolve('puppeteer-core/lib/esm/puppeteer/common/Page.js'),
199+
],
200+
}),
196201
rollupPlugins.commonjs({
197202
// https://github.com/rollup/plugins/issues/922
198203
ignoreGlobal: true,

build/build-extension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function buildEntryPoint() {
4040
'___BROWSER_BRAND___': browserBrand,
4141
}),
4242
rollupPlugins.nodeResolve(),
43-
rollupPlugins.inlineFs({verbose: false}),
43+
rollupPlugins.inlineFs({verbose: Boolean(process.env.DEBUG)}),
4444
rollupPlugins.terser(),
4545
],
4646
});

build/plugins/inline-fs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ async function inlineFs(code, filepath) {
9898
parsed.callee.property);
9999
}
100100
} catch (err) {
101+
// Consider missing files to be a fatal error.
102+
if (err.code === 'ENOENT') {
103+
throw err;
104+
}
105+
101106
// Use the specific node with the error if available; fallback to fs.method location.
102107
const offsets = getNodeOffsets(err.node || parsed);
103108
const location = acorn.getLineInfo(code, offsets.start);

build/plugins/rollup-plugin-inline-fs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {LH_ROOT} from '../../root.js';
1414
/**
1515
* @typedef Options
1616
* @property {boolean} [verbose] If true, turns on verbose logging, e.g. log instances where fs methods could not be inlined.
17+
* @property {string[]} [ignorePaths] Absoulte paths of files to not process for inlining.
1718
*/
1819

1920
/**
@@ -29,9 +30,13 @@ function rollupInlineFs(options = {}) {
2930
/**
3031
* @param {string} originalCode
3132
* @param {string} filepath
32-
* @return {Promise<string|null>}
33+
* @return {Promise<import('rollup').TransformResult>}
3334
*/
3435
async transform(originalCode, filepath) {
36+
if (options.ignorePaths?.includes(filepath)) {
37+
return;
38+
}
39+
3540
// TODO(bckenny): add source maps, watch files.
3641
const {code, warnings} = await inlineFs(originalCode, filepath);
3742

build/test/plugins/inline-fs-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ describe('inline-fs', () => {
265265
});
266266
});
267267

268+
it('throws fatal error if file is missing', async () => {
269+
const content = `const myTextContent = fs.readFileSync('i-never-exist.lol', 'utf8');`;
270+
await expect(inlineFs(content, filepath)).rejects.toThrow('ENOENT');
271+
});
272+
268273
it('inlines multiple fs.readFileSync calls', async () => {
269274
fs.writeFileSync(tmpPath, 'some text content');
270275
// eslint-disable-next-line max-len

0 commit comments

Comments
 (0)