Skip to content

Commit

Permalink
misc(build): minify report javascript for lightrider report generator (
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Oct 13, 2019
1 parent dbcb52c commit d50ef9e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
7 changes: 6 additions & 1 deletion build/build-lightrider-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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());
Expand Down
37 changes: 37 additions & 0 deletions build/build-utils.js
Original file line number Diff line number Diff line change
@@ -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,
};
28 changes: 2 additions & 26 deletions build/build-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`;
Expand Down Expand Up @@ -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<void>}
Expand All @@ -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<string>} */
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit d50ef9e

Please sign in to comment.