forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clients: simplify viewer build process (GoogleChrome#6426)
- Loading branch information
1 parent
d0a2a52
commit 9615070
Showing
17 changed files
with
367 additions
and
4,695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
**/node_modules/** | ||
**/third_party/** | ||
|
||
**/dist/** | ||
/dist/** | ||
|
||
coverage/** | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ npm-debug.log | |
.vscode | ||
.tmp | ||
|
||
dist | ||
/dist | ||
coverage | ||
lcov.info | ||
.nyc_output | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
assets/ | ||
build/ | ||
coverage/ | ||
dist/ | ||
|
||
lighthouse-core/scripts/* | ||
lighthouse-core/test/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/** | ||
* @license Copyright 2018 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 fs = require('fs'); | ||
const path = require('path'); | ||
const {promisify} = require('util'); | ||
const readFileAsync = promisify(fs.readFile); | ||
const writeFileAsync = promisify(fs.writeFile); | ||
|
||
const browserify = require('browserify'); | ||
const cpy = require('cpy'); | ||
const ghPages = promisify(require('gh-pages').publish); | ||
const glob = promisify(require('glob')); | ||
const lighthousePackage = require('../package.json'); | ||
const makeDir = require('make-dir'); | ||
const rimraf = require('rimraf'); | ||
const uglifyEs = require('uglify-es'); // Use uglify-es to get ES6 support. | ||
|
||
const htmlReportAssets = require('../lighthouse-core/report/html/html-report-assets.js'); | ||
const sourceDir = `${__dirname}/../lighthouse-viewer`; | ||
const distDir = `${__dirname}/../dist/viewer`; | ||
|
||
const license = `/* | ||
* @license Copyright 2018 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. | ||
*/`; | ||
|
||
/** | ||
* Evaluates path glob and loads all identified files as an array of strings. | ||
* @param {string} pattern | ||
* @return {Promise<Array<string>>} | ||
*/ | ||
async function loadFiles(pattern) { | ||
const filePaths = await glob(pattern); | ||
return Promise.all(filePaths.map(path => readFileAsync(path, {encoding: 'utf8'}))); | ||
} | ||
|
||
/** | ||
* Write a file to filePath, creating parent directories if needed. | ||
* @param {string} filePath | ||
* @param {string} data | ||
* @return {Promise<void>} | ||
*/ | ||
async function safeWriteFileAsync(filePath, data) { | ||
const fileDir = path.dirname(filePath); | ||
await makeDir(fileDir); | ||
return writeFileAsync(filePath, data); | ||
} | ||
|
||
/** | ||
* Copy static assets. | ||
* @return {Promise<void>} | ||
*/ | ||
async function copyAssets() { | ||
await cpy([ | ||
'images/**/*', | ||
'sw.js', | ||
'manifest.json', | ||
], distDir, { | ||
cwd: `${sourceDir}/app/`, | ||
parents: true, | ||
}); | ||
|
||
// Copy polyfills. | ||
return cpy([ | ||
'../node_modules/url-search-params/build/url-search-params.js', | ||
'../node_modules/whatwg-fetch/fetch.js', | ||
], `${distDir}/src/polyfills`, {cwd: sourceDir}); | ||
} | ||
|
||
/** | ||
* Concat report and viewer stylesheets into single viewer.css file. | ||
* @return {Promise<void>} | ||
*/ | ||
async function css() { | ||
const reportCss = htmlReportAssets.REPORT_CSS; | ||
const viewerCss = await readFileAsync(`${sourceDir}/app/styles/viewer.css`, {encoding: 'utf8'}); | ||
await safeWriteFileAsync(`${distDir}/styles/viewer.css`, [reportCss, viewerCss].join('\n')); | ||
} | ||
|
||
/** | ||
* Insert report templates into html and copy to dist. | ||
* @return {Promise<void>} | ||
*/ | ||
async function html() { | ||
let htmlSrc = await readFileAsync(`${sourceDir}/app/index.html`, {encoding: 'utf8'}); | ||
htmlSrc = htmlSrc.replace(/%%LIGHTHOUSE_TEMPLATES%%/, htmlReportAssets.REPORT_TEMPLATES); | ||
|
||
await safeWriteFileAsync(`${distDir}/index.html`, htmlSrc); | ||
} | ||
|
||
/** | ||
* Combine multiple JS files into single viewer.js file. | ||
* @return {Promise<void>} | ||
*/ | ||
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'); | ||
const generatorBundle = promisify(generatorBrowserify.bundle.bind(generatorBrowserify)); | ||
const generatorJs = (await generatorBundle()).toString(); | ||
|
||
// Report renderer scripts. | ||
const rendererJs = htmlReportAssets.REPORT_JAVASCRIPT; | ||
|
||
// idb-keyval dependency. | ||
const idbKeyvalPath = require.resolve('idb-keyval/dist/idb-keyval-min.js'); | ||
const idbKeyvalJs = await readFileAsync(idbKeyvalPath, 'utf8'); | ||
|
||
// Current Lighthouse version as a global variable. | ||
const versionJs = `window.LH_CURRENT_VERSION = '${lighthousePackage.version}';`; | ||
|
||
// Viewer-specific JS files. | ||
const viewJsFiles = await loadFiles(`${sourceDir}/app/src/*.js`); | ||
|
||
const contents = [ | ||
generatorJs, | ||
rendererJs, | ||
idbKeyvalJs, | ||
versionJs, | ||
...viewJsFiles, | ||
]; | ||
const options = { | ||
output: {preamble: license}, // Insert license at top. | ||
}; | ||
const uglified = uglifyEs.minify(contents, options); | ||
if (uglified.error) { | ||
throw uglified.error; | ||
} | ||
|
||
await safeWriteFileAsync(`${distDir}/src/viewer.js`, uglified.code); | ||
} | ||
|
||
/** | ||
* Build viewer, optionally deploying to gh-pages if `--deploy` flag was set. | ||
*/ | ||
async function run() { | ||
// Clean and build. | ||
rimraf.sync(distDir); | ||
await Promise.all([ | ||
compileJs(), | ||
html(), | ||
css(), | ||
copyAssets(), | ||
]); | ||
|
||
const argv = process.argv.slice(2); | ||
if (argv.includes('--deploy')) { | ||
await ghPages(`${distDir}/**/*`, { | ||
add: true, // keep existing files | ||
dest: 'viewer', | ||
}, () => {}); | ||
} | ||
} | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.