misc(build): fix smokehouse bundle (#13135)

This commit is contained in:
Connor Clark 2021-10-12 15:33:25 -05:00 коммит произвёл GitHub
Родитель 2faf092f68
Коммит 961626d6d2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 100 добавлений и 11 удалений

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

@ -6,6 +6,8 @@
'use strict'; 'use strict';
const browserify = require('browserify'); const browserify = require('browserify');
const rollup = require('rollup');
const rollupPlugins = require('./rollup-plugins.js');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const bundleBuilder = require('./build-bundle.js'); const bundleBuilder = require('./build-bundle.js');
@ -52,11 +54,31 @@ function buildReportGenerator() {
}); });
} }
async function buildStaticServerBundle() {
const bundle = await rollup.rollup({
input: 'lighthouse-cli/test/fixtures/static-server.js',
plugins: [
rollupPlugins.shim({
'es-main': 'export default function() { return false; }',
}),
rollupPlugins.commonjs(),
rollupPlugins.nodeResolve(),
],
external: ['mime-types', 'glob'],
});
await bundle.write({
file: 'dist/lightrider/static-server.js',
format: 'commonjs',
});
}
async function run() { async function run() {
await Promise.all([ await Promise.all([
buildEntryPoint(), buildEntryPoint(),
buildReportGenerator(), buildReportGenerator(),
buildPsiReport(), buildPsiReport(),
buildStaticServerBundle(),
]); ]);
} }

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

@ -12,19 +12,25 @@ const {LH_ROOT} = require('../root.js');
const distDir = `${LH_ROOT}/dist`; const distDir = `${LH_ROOT}/dist`;
const bundleOutFile = `${distDir}/smokehouse-bundle.js`; const bundleOutFile = `${distDir}/smokehouse-bundle.js`;
const smokehouseLibFilename = './lighthouse-cli/test/smokehouse/frontends/lib.js'; const smokehouseLibFilename = './lighthouse-cli/test/smokehouse/frontends/lib.js';
const smokehouseCliFilename = const smokehouseCliFilename = `${LH_ROOT}/lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js`;
require.resolve('../lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js');
async function build() { async function build() {
const bundle = await rollup.rollup({ const bundle = await rollup.rollup({
input: smokehouseLibFilename, input: smokehouseLibFilename,
context: 'globalThis', context: 'globalThis',
plugins: [ plugins: [
rollupPlugins.nodeResolve(),
rollupPlugins.commonjs(),
rollupPlugins.shim({ rollupPlugins.shim({
[smokehouseCliFilename]: 'export default {}', [smokehouseCliFilename]:
'export function runLighthouse() { throw new Error("not supported"); }',
}), }),
// TODO(esmodules): brfs does not support es modules.
rollupPlugins.brfs({
global: true,
parserOpts: {ecmaVersion: 12, sourceType: 'module'},
}),
rollupPlugins.commonjs(),
rollupPlugins.nodePolyfills(),
rollupPlugins.nodeResolve(),
], ],
}); });

54
build/rollup-brfs.js Normal file
Просмотреть файл

@ -0,0 +1,54 @@
/**
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
// TODO: brfs doesn't work for ES modules, so this is a stopgap solution
// for the present usecases that aren't in esm yet. Will be replaced
// with a full-featured inlining plugin soon.
const path = require('path');
const {Readable} = require('stream');
// @ts-expect-error - no types.
const brfs = require('@wardpeet/brfs');
const EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx'];
const rollupBrfs = function rollUpBrfs(options = {}) {
return {
name: 'brfs',
/**
* @param {string} code
* @param {string} id
*/
transform(code, id) {
const ext = path.extname(id);
if (!EXTENSIONS.includes(ext)) {
return null;
}
return new Promise((resolve, reject) => {
let output = '';
const src = new Readable();
src.push(code);
src.push(null);
const stream = src.pipe(brfs(id, options));
stream.on('data', /** @param {Buffer} data */ function(data) {
output += data.toString();
});
stream.on('end', function() {
resolve({
code: output,
map: {mappings: ''},
});
});
stream.on('error', /** @param {Error} error */ function(error) {
reject(error);
});
});
},
};
};
module.exports = rollupBrfs;

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

@ -16,6 +16,7 @@ function rollupPluginTypeCoerce(module) {
return module; return module;
} }
const brfs = require('./rollup-brfs.js');
const commonjs = rollupPluginTypeCoerce(require('@rollup/plugin-commonjs')); const commonjs = rollupPluginTypeCoerce(require('@rollup/plugin-commonjs'));
const nodePolyfills = rollupPluginTypeCoerce(require('rollup-plugin-polyfill-node')); const nodePolyfills = rollupPluginTypeCoerce(require('rollup-plugin-polyfill-node'));
const {nodeResolve} = require('@rollup/plugin-node-resolve'); const {nodeResolve} = require('@rollup/plugin-node-resolve');
@ -26,6 +27,7 @@ const {terser} = require('rollup-plugin-terser');
const typescript = rollupPluginTypeCoerce(require('@rollup/plugin-typescript')); const typescript = rollupPluginTypeCoerce(require('@rollup/plugin-typescript'));
module.exports = { module.exports = {
brfs,
commonjs, commonjs,
nodePolyfills, nodePolyfills,
nodeResolve, nodeResolve,

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

@ -5,11 +5,12 @@
*/ */
'use strict'; 'use strict';
import fs from 'fs'; const fs = require('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 = const mapJson =
fs.readFileSync(`${LH_ROOT}/lighthouse-cli/test/fixtures/source-map/script.js.map`, 'utf-8'); fs.readFileSync(`${__dirname}/../../../fixtures/source-map/script.js.map`, 'utf-8');
const map = JSON.parse(mapJson); const map = JSON.parse(mapJson);
/** /**
@ -41,4 +42,4 @@ const expectations = {
}, },
}; };
export {expectations}; module.exports = {expectations};

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

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

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

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

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

@ -21,7 +21,7 @@
"build-extension-firefox": "node ./build/build-extension.js firefox", "build-extension-firefox": "node ./build/build-extension.js firefox",
"build-devtools": "yarn reset-link && node ./build/build-bundle.js clients/devtools-entry.js dist/lighthouse-dt-bundle.js && node ./build/build-dt-report-resources.js", "build-devtools": "yarn reset-link && node ./build/build-bundle.js clients/devtools-entry.js dist/lighthouse-dt-bundle.js && node ./build/build-dt-report-resources.js",
"build-smokehouse-bundle": "node ./build/build-smokehouse-bundle.js", "build-smokehouse-bundle": "node ./build/build-smokehouse-bundle.js",
"build-lr": "yarn reset-link && node ./build/build-lightrider-bundles.js && rollup lighthouse-cli/test/fixtures/static-server.js -o dist/lightrider/static-server.js -f commonjs -p commonjs -p node-resolve -e mime-types,glob", "build-lr": "yarn reset-link && node ./build/build-lightrider-bundles.js",
"build-pack": "bash build/build-pack.sh", "build-pack": "bash build/build-pack.sh",
"build-report": "node build/build-report-components.js && yarn eslint --fix report/renderer/components.js && node build/build-report.js", "build-report": "node build/build-report-components.js && yarn eslint --fix report/renderer/components.js && node build/build-report.js",
"build-sample-reports": "yarn build-report && node build/build-sample-reports.js", "build-sample-reports": "yarn build-report && node build/build-sample-reports.js",