From 94810f3ff1678ca9150612aed73cdc18898aefea Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 11 Oct 2019 22:22:41 +0200 Subject: [PATCH] report(viewer): minify inlined report-generator bundle (#9596) --- build/build-bundle.js | 2 +- build/build-lightrider-bundles.js | 2 +- build/build-viewer.js | 29 ++++++++++++++++++++++++++++- package.json | 2 +- yarn.lock | 20 ++++++++++---------- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/build/build-bundle.js b/build/build-bundle.js index 0cbc96f348ae..a05679995992 100644 --- a/build/build-bundle.js +++ b/build/build-bundle.js @@ -54,7 +54,7 @@ async function browserifyFile(entryPath, distPath) { bundle // Transform the fs.readFile etc into inline strings. - .transform('brfs', {global: true, parserOpts: {ecmaVersion: 10}}) + .transform('@wardpeet/brfs', {global: true, parserOpts: {ecmaVersion: 10}}) // Strip everything out of package.json includes except for the version. .transform('package-json-versionify'); diff --git a/build/build-lightrider-bundles.js b/build/build-lightrider-bundles.js index 5a040d5338a5..08cee17d764e 100644 --- a/build/build-lightrider-bundles.js +++ b/build/build-lightrider-bundles.js @@ -37,7 +37,7 @@ function buildEntryPoint() { function buildReportGenerator() { browserify(generatorFilename, {standalone: 'ReportGenerator'}) // Transform the fs.readFile etc into inline strings. - .transform('brfs', {global: true, parserOpts: {ecmaVersion: 10}}) + .transform('@wardpeet/brfs', {global: true, parserOpts: {ecmaVersion: 10}}) .bundle((err, src) => { if (err) throw err; fs.writeFileSync(bundleOutFile, src.toString()); diff --git a/build/build-viewer.js b/build/build-viewer.js index 3065e9cf1a90..b8b490095d30 100644 --- a/build/build-viewer.js +++ b/build/build-viewer.js @@ -10,6 +10,7 @@ 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'); @@ -98,6 +99,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} @@ -106,7 +131,9 @@ async function compileJs() { // JS bundle from browserified ReportGenerator. const generatorFilename = `${sourceDir}/../lighthouse-core/report/report-generator.js`; const generatorBrowserify = browserify(generatorFilename, {standalone: 'ReportGenerator'}) - .transform('brfs'); + .transform('@wardpeet/brfs', { + readFileSyncTransform: minifyReadFileContent, + }); /** @type {Promise} */ const generatorJsPromise = new Promise((resolve, reject) => { diff --git a/package.json b/package.json index 730aab34a694..f628bb17e8a2 100644 --- a/package.json +++ b/package.json @@ -95,12 +95,12 @@ "@types/update-notifier": "^1.0.2", "@types/ws": "^4.0.1", "@types/yargs": "^8.0.2", + "@wardpeet/brfs": "2.1.0-0", "angular": "^1.7.4", "archiver": "^3.0.0", "babel-core": "^6.26.0", "babel-plugin-syntax-async-generators": "^6.13.0", "babel-plugin-syntax-object-rest-spread": "^6.13.0", - "brfs": "^2.0.2", "browserify": "^16.2.3", "bundlesize": "^0.17.2", "chalk": "^2.4.1", diff --git a/yarn.lock b/yarn.lock index 1063549acb83..72599abbb095 100644 --- a/yarn.lock +++ b/yarn.lock @@ -727,6 +727,16 @@ 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": + 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== + dependencies: + quote-stream "^1.0.1" + resolve "^1.1.5" + static-module "^3.0.2" + through2 "^2.0.0" + JSONStream@^1.0.3, JSONStream@^1.0.4: version "1.3.5" resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0" @@ -1372,16 +1382,6 @@ braces@^2.3.1: split-string "^3.0.2" to-regex "^3.0.1" -brfs@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/brfs/-/brfs-2.0.2.tgz#44237878fa82aa479ce4f5fe2c1796ec69f07845" - integrity sha512-IrFjVtwu4eTJZyu8w/V2gxU7iLTtcHih67sgEdzrhjLBMHp2uYefUBfdM4k2UvcuWMgV7PQDZHSLeNWnLFKWVQ== - dependencies: - quote-stream "^1.0.1" - resolve "^1.1.5" - static-module "^3.0.2" - through2 "^2.0.0" - brorand@^1.0.1: version "1.1.0" resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"