From d717a6eeeb2421ebc894ae32c2f3a480722d0597 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 23 Aug 2019 03:21:32 +0200 Subject: [PATCH 1/6] feat(lighthouse-viewer): minify report generator --- build/build-viewer.js | 25 ------------------------- package.json | 2 +- yarn.lock | 2 +- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/build/build-viewer.js b/build/build-viewer.js index b8b490095d30..f4f1d692f889 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'); @@ -99,30 +98,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} diff --git a/package.json b/package.json index f628bb17e8a2..cbd0821c1ab8 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "@types/update-notifier": "^1.0.2", "@types/ws": "^4.0.1", "@types/yargs": "^8.0.2", - "@wardpeet/brfs": "2.1.0-0", + "@wardpeet/brfs": "^2.1.0-0", "angular": "^1.7.4", "archiver": "^3.0.0", "babel-core": "^6.26.0", diff --git a/yarn.lock b/yarn.lock index 72599abbb095..c388e1ce9eaa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -727,7 +727,7 @@ resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-8.0.2.tgz#0f9c7b236e2d78cd8f4b6502de15d0728aa29385" integrity sha512-Upj9YsBZRgjEVPvsaeGru48d2JiyzBNZkmkebHyoaQ+UM9wqj/rp5mkilRjSq/Ga45yfd/zwrNuML9f2gGfVpw== -"@wardpeet/brfs@2.1.0-0": +"@wardpeet/brfs@^2.1.0-0": version "2.1.0-0" resolved "https://registry.yarnpkg.com/@wardpeet/brfs/-/brfs-2.1.0-0.tgz#04e77dc088ca5bbc5b07051860faa45569e799f3" integrity sha512-ra9bDHPwsI+HBKQJk9CLMYaFDHLA7QGEH0teWlIcZ6/CHtFvjRb2YjwLBXc8jgtw+3LvCEuqme/BQaDfo74fUA== From 49dd34228493d5f0bb66d228babef55f53300b42 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Tue, 3 Sep 2019 17:32:05 +0200 Subject: [PATCH 2/6] move inline function to root function --- build/build-viewer.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/build/build-viewer.js b/build/build-viewer.js index f4f1d692f889..67eace1a807f 100644 --- a/build/build-viewer.js +++ b/build/build-viewer.js @@ -98,6 +98,30 @@ 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} From 2382a2cbe7511f606da1e26132bbffd70ade3343 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 11 Oct 2019 00:02:35 -0700 Subject: [PATCH 3/6] minify report assets for lr too --- build/build-lightrider-bundles.js | 7 +++++- build/build-utils.js | 37 +++++++++++++++++++++++++++++++ build/build-viewer.js | 27 ++-------------------- 3 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 build/build-utils.js 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 67eace1a807f..c3db40f4ba81 100644 --- a/build/build-viewer.js +++ b/build/build-viewer.js @@ -19,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`; @@ -98,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} @@ -131,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} */ From 810f5e448d83769efb29996b8be2f7dc9bcd27b0 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 11 Oct 2019 00:13:17 -0700 Subject: [PATCH 4/6] bundlesize --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index cbd0821c1ab8..f93ff589e886 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": { From 33c9c2b0a4bec174a0bc505502bc82c92691a5f7 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 11 Oct 2019 13:29:59 -0700 Subject: [PATCH 5/6] pkg --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f93ff589e886..9696d3bbb6b3 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "@types/update-notifier": "^1.0.2", "@types/ws": "^4.0.1", "@types/yargs": "^8.0.2", - "@wardpeet/brfs": "^2.1.0-0", + "@wardpeet/brfs": "2.1.0-0", "angular": "^1.7.4", "archiver": "^3.0.0", "babel-core": "^6.26.0", From 582a87509bdf6d7b14473805d647ec620328b7c7 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 11 Oct 2019 13:30:07 -0700 Subject: [PATCH 6/6] pkg --- yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index c388e1ce9eaa..72599abbb095 100644 --- a/yarn.lock +++ b/yarn.lock @@ -727,7 +727,7 @@ resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-8.0.2.tgz#0f9c7b236e2d78cd8f4b6502de15d0728aa29385" integrity sha512-Upj9YsBZRgjEVPvsaeGru48d2JiyzBNZkmkebHyoaQ+UM9wqj/rp5mkilRjSq/Ga45yfd/zwrNuML9f2gGfVpw== -"@wardpeet/brfs@^2.1.0-0": +"@wardpeet/brfs@2.1.0-0": version "2.1.0-0" resolved "https://registry.yarnpkg.com/@wardpeet/brfs/-/brfs-2.1.0-0.tgz#04e77dc088ca5bbc5b07051860faa45569e799f3" integrity sha512-ra9bDHPwsI+HBKQJk9CLMYaFDHLA7QGEH0teWlIcZ6/CHtFvjRb2YjwLBXc8jgtw+3LvCEuqme/BQaDfo74fUA==