Skip to content

Commit

Permalink
report(viewer): minify inlined report-generator bundle (#9596)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and paulirish committed Nov 6, 2019
1 parent 81d5c4f commit b065f0a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build/build-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion build/build-lightrider-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
29 changes: 28 additions & 1 deletion build/build-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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<void>}
Expand All @@ -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<string>} */
const generatorJsPromise = new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b065f0a

Please sign in to comment.