diff --git a/build/build-lightrider-bundles.js b/build/build-lightrider-bundles.js index 08cee17d764e..0fa8c562a313 100644 --- a/build/build-lightrider-bundles.js +++ b/build/build-lightrider-bundles.js @@ -10,6 +10,7 @@ const fs = require('fs'); const path = require('path'); const makeDir = require('make-dir'); const bundleBuilder = require('./build-bundle.js'); +const {minifyFileTransform} = require('./build-utils.js'); const distDir = path.join(__dirname, '..', 'dist', 'lightrider'); const sourceDir = __dirname + '/../clients/lightrider'; @@ -37,7 +38,11 @@ function buildEntryPoint() { function buildReportGenerator() { browserify(generatorFilename, {standalone: 'ReportGenerator'}) // Transform the fs.readFile etc into inline strings. - .transform('@wardpeet/brfs', {global: true, parserOpts: {ecmaVersion: 10}}) + .transform('@wardpeet/brfs', { + readFileSyncTransform: minifyFileTransform, + global: true, + parserOpts: {ecmaVersion: 10}, + }) .bundle((err, src) => { if (err) throw err; fs.writeFileSync(bundleOutFile, src.toString()); diff --git a/build/build-utils.js b/build/build-utils.js new file mode 100644 index 000000000000..a8a4423b7321 --- /dev/null +++ b/build/build-utils.js @@ -0,0 +1,37 @@ +/** + * @license Copyright 2019 Google Inc. 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'; + +const stream = require('stream'); +const terser = require('terser'); + +/** + * Minifies file which are read by fs.readFileSync (brfs) + * + * @param {string} file + */ +function minifyFileTransform(file) { + return new stream.Transform({ + transform(chunk, enc, next) { + if (file.endsWith('.js')) { + const result = terser.minify(chunk.toString()); + if (result.error) { + throw result.error; + } + + this.push(result.code); + } else { + this.push(chunk); + } + + next(); + }, + }); +} + +module.exports = { + minifyFileTransform, +}; diff --git a/build/build-viewer.js b/build/build-viewer.js index b8b490095d30..c3db40f4ba81 100644 --- a/build/build-viewer.js +++ b/build/build-viewer.js @@ -10,7 +10,6 @@ const path = require('path'); const {promisify} = require('util'); const readFileAsync = promisify(fs.readFile); const writeFileAsync = promisify(fs.writeFile); -const stream = require('stream'); const browserify = require('browserify'); const cpy = require('cpy'); @@ -20,6 +19,7 @@ const lighthousePackage = require('../package.json'); const makeDir = require('make-dir'); const rimraf = require('rimraf'); const terser = require('terser'); +const {minifyFileTransform} = require('./build-utils.js'); const htmlReportAssets = require('../lighthouse-core/report/html/html-report-assets.js'); const sourceDir = `${__dirname}/../lighthouse-viewer`; @@ -99,30 +99,6 @@ async function html() { await safeWriteFileAsync(`${distDir}/index.html`, htmlSrc); } -/** - * Minifies file which are read by fs.readFileSync (brfs) - * - * @param {string} file - */ -function minifyReadFileContent(file) { - return new stream.Transform({ - transform(chunk, enc, next) { - if (file.endsWith('.js')) { - const result = terser.minify(chunk.toString()); - if (result.error) { - throw result.error; - } - - this.push(result.code); - } else { - this.push(chunk); - } - - next(); - }, - }); -} - /** * Combine multiple JS files into single viewer.js file. * @return {Promise} @@ -132,7 +108,7 @@ async function compileJs() { const generatorFilename = `${sourceDir}/../lighthouse-core/report/report-generator.js`; const generatorBrowserify = browserify(generatorFilename, {standalone: 'ReportGenerator'}) .transform('@wardpeet/brfs', { - readFileSyncTransform: minifyReadFileContent, + readFileSyncTransform: minifyFileTransform, }); /** @type {Promise} */ diff --git a/package.json b/package.json index f628bb17e8a2..9696d3bbb6b3 100644 --- a/package.json +++ b/package.json @@ -187,11 +187,15 @@ }, { "path": "./dist/viewer/src/viewer.js", - "maxSize": "76 kB" + "maxSize": "65 kB" }, { "path": "./dist/lighthouse-dt-bundle.js", "maxSize": "400 kB" + }, + { + "path": "./dist/lightrider/report-generator-bundle.js", + "maxSize": "50 kB" } ], "nyc": {