forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-bundle.js
183 lines (156 loc) · 6.66 KB
/
build-bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @license Copyright 2018 The Lighthouse Authors. 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';
/**
* @fileoverview Script to bundle lighthouse entry points so that they can be run
* in the browser (as long as they have access to a debugger protocol Connection).
*/
const fs = require('fs');
const path = require('path');
const mkdir = fs.promises.mkdir;
const LighthouseRunner = require('../lighthouse-core/runner.js');
const babel = require('babel-core');
const browserify = require('browserify');
const pkg = require('../package.json');
const VERSION = pkg.version;
const COMMIT_HASH = require('child_process')
.execSync('git rev-parse HEAD')
.toString().trim();
const audits = LighthouseRunner.getAuditList()
.map(f => './lighthouse-core/audits/' + f.replace(/\.js$/, ''));
const gatherers = LighthouseRunner.getGathererList()
.map(f => './lighthouse-core/gather/gatherers/' + f.replace(/\.js$/, ''));
const locales = fs.readdirSync(__dirname + '/../lighthouse-core/lib/i18n/locales/')
.map(f => require.resolve(`../lighthouse-core/lib/i18n/locales/${f}`));
// HACK: manually include the lighthouse-plugin-publisher-ads audits.
/** @type {Array<string>} */
// @ts-ignore
const pubAdsAudits = require('lighthouse-plugin-publisher-ads/plugin.js').audits.map(a => a.path);
/** @param {string} file */
const isDevtools = file => path.basename(file).includes('devtools');
/** @param {string} file */
const isLightrider = file => path.basename(file).includes('lightrider');
const BANNER = `// lighthouse, browserified. ${VERSION} (${COMMIT_HASH})\n` +
'// @ts-nocheck\n'; // To prevent tsc stepping into any required bundles.
const DEBUG = false; // true for sourcemaps
/**
* Browserify starting at the file at entryPath. Contains entry-point-specific
* ignores (e.g. for DevTools or the extension) to trim the bundle depending on
* the eventual use case.
* @param {string} entryPath
* @param {string} distPath
* @return {Promise<void>}
*/
async function browserifyFile(entryPath, distPath) {
let bundle = browserify(entryPath, {debug: DEBUG});
bundle
// Transform the fs.readFile etc into inline strings.
.transform('@wardpeet/brfs', {global: true, parserOpts: {ecmaVersion: 10}})
// Strip everything out of package.json includes except for the version.
.transform('package-json-versionify');
// scripts will need some additional transforms, ignores and requires…
bundle.ignore('source-map')
.ignore('debug/node')
.ignore('intl')
.ignore('intl-pluralrules')
.ignore('raven')
.ignore('rimraf')
.ignore('pako/lib/zlib/inflate.js');
// Don't include the desktop protocol connection.
bundle.ignore(require.resolve('../lighthouse-core/gather/connections/cri.js'));
// Don't include the stringified report in DevTools - see devtools-report-assets.js
// Don't include in Lightrider - HTML generation isn't supported, so report assets aren't needed.
if (isDevtools(entryPath) || isLightrider(entryPath)) {
bundle.ignore(require.resolve('../lighthouse-core/report/html/html-report-assets.js'));
}
// Don't include locales in DevTools.
if (isDevtools(entryPath)) {
// @ts-ignore bundle.ignore does accept an array of strings.
bundle.ignore(locales);
}
// Expose the audits, gatherers, and computed artifacts so they can be dynamically loaded.
// Exposed path must be a relative path from lighthouse-core/config/config-helpers.js (where loading occurs).
const corePath = './lighthouse-core/';
const driverPath = `${corePath}gather/`;
audits.forEach(audit => {
bundle = bundle.require(audit, {expose: audit.replace(corePath, '../')});
});
gatherers.forEach(gatherer => {
bundle = bundle.require(gatherer, {expose: gatherer.replace(driverPath, '../gather/')});
});
// HACK: manually include the lighthouse-plugin-publisher-ads audits.
// TODO: there should be a test for this.
if (isDevtools(entryPath)) {
bundle.require('lighthouse-plugin-publisher-ads');
pubAdsAudits.forEach(pubAdAudit => {
bundle = bundle.require(pubAdAudit);
});
}
// browerify's url shim doesn't work with .URL in node_modules,
// and within robots-parser, it does `var URL = require('url').URL`, so we expose our own.
// @see https://github.com/GoogleChrome/lighthouse/issues/5273
const pathToURLShim = require.resolve('../lighthouse-core/lib/url-shim.js');
bundle = bundle.require(pathToURLShim, {expose: 'url'});
const bundleStream = bundle.bundle();
// Make sure path exists.
await mkdir(path.dirname(distPath), {recursive: true});
return new Promise((resolve, reject) => {
const writeStream = fs.createWriteStream(distPath);
writeStream.on('finish', resolve);
writeStream.on('error', reject);
bundleStream.pipe(writeStream);
});
}
/**
* Minimally minify a javascript file, in place.
* @param {string} filePath
*/
function minifyScript(filePath) {
const opts = {
compact: true, // Do not include superfluous whitespace characters and line terminators.
retainLines: true, // Keep things on the same line (looks wonky but helps with stacktraces)
comments: false, // Don't output comments
shouldPrintComment: () => false, // Don't include @license or @preserve comments either
plugins: [
'syntax-object-rest-spread',
'syntax-async-generators',
],
// sourceMaps: 'both'
};
const minified = BANNER + babel.transformFileSync(filePath, opts).code;
fs.writeFileSync(filePath, minified);
}
/**
* Browserify starting at entryPath, writing the minified result to distPath.
* @param {string} entryPath
* @param {string} distPath
* @return {Promise<void>}
*/
async function build(entryPath, distPath) {
await browserifyFile(entryPath, distPath);
if (!DEBUG) {
minifyScript(distPath);
}
}
/**
* @param {Array<string>} argv
*/
async function cli(argv) {
// Take paths relative to cwd and build.
const [entryPath, distPath] = argv.slice(2)
.map(filePath => path.resolve(process.cwd(), filePath));
build(entryPath, distPath);
}
// @ts-ignore Test if called from the CLI or as a module.
if (require.main === module) {
cli(process.argv);
} else {
module.exports = {
/** The commit hash for the current HEAD. */
COMMIT_HASH,
build,
};
}