diff --git a/build/build-lightrider-bundles.js b/build/build-lightrider-bundles.js index 54863bc3fc..dde3181bc5 100644 --- a/build/build-lightrider-bundles.js +++ b/build/build-lightrider-bundles.js @@ -6,6 +6,8 @@ 'use strict'; const browserify = require('browserify'); +const rollup = require('rollup'); +const rollupPlugins = require('./rollup-plugins.js'); const fs = require('fs'); const path = require('path'); 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() { await Promise.all([ buildEntryPoint(), buildReportGenerator(), buildPsiReport(), + buildStaticServerBundle(), ]); } diff --git a/build/build-smokehouse-bundle.js b/build/build-smokehouse-bundle.js index db46639fca..0d00700745 100644 --- a/build/build-smokehouse-bundle.js +++ b/build/build-smokehouse-bundle.js @@ -12,19 +12,25 @@ const {LH_ROOT} = require('../root.js'); const distDir = `${LH_ROOT}/dist`; const bundleOutFile = `${distDir}/smokehouse-bundle.js`; const smokehouseLibFilename = './lighthouse-cli/test/smokehouse/frontends/lib.js'; -const smokehouseCliFilename = - require.resolve('../lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js'); +const smokehouseCliFilename = `${LH_ROOT}/lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js`; async function build() { const bundle = await rollup.rollup({ input: smokehouseLibFilename, context: 'globalThis', plugins: [ - rollupPlugins.nodeResolve(), - rollupPlugins.commonjs(), 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(), ], }); diff --git a/build/rollup-brfs.js b/build/rollup-brfs.js new file mode 100644 index 0000000000..f2dbd755e1 --- /dev/null +++ b/build/rollup-brfs.js @@ -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; diff --git a/build/rollup-plugins.js b/build/rollup-plugins.js index d674479d22..1dabc48c51 100644 --- a/build/rollup-plugins.js +++ b/build/rollup-plugins.js @@ -16,6 +16,7 @@ function rollupPluginTypeCoerce(module) { return module; } +const brfs = require('./rollup-brfs.js'); const commonjs = rollupPluginTypeCoerce(require('@rollup/plugin-commonjs')); const nodePolyfills = rollupPluginTypeCoerce(require('rollup-plugin-polyfill-node')); const {nodeResolve} = require('@rollup/plugin-node-resolve'); @@ -26,6 +27,7 @@ const {terser} = require('rollup-plugin-terser'); const typescript = rollupPluginTypeCoerce(require('@rollup/plugin-typescript')); module.exports = { + brfs, commonjs, nodePolyfills, nodeResolve, 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 48d53434d5..6bc07176a4 100644 --- a/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js +++ b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/expectations.js @@ -5,11 +5,12 @@ */ 'use strict'; -import fs from 'fs'; -import {LH_ROOT} from '../../../../../root.js'; +const fs = require('fs'); +// 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(`${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); /** @@ -41,4 +42,4 @@ const expectations = { }, }; -export {expectations}; +module.exports = {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 new file mode 100644 index 0000000000..8d10bc1dfd --- /dev/null +++ b/lighthouse-cli/test/smokehouse/test-definitions/source-maps/package.json @@ -0,0 +1,4 @@ +{ + "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 711d198f08..c671e55846 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 = { }, }; -export default config; +module.exports = config; diff --git a/package.json b/package.json index 3a137e0911..2ac8a4ae8b 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "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-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-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",