Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collect): Make it easy to use with SPA #159

Merged
merged 4 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cli/src/collect/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function buildCommand(yargs) {
staticDistDir: {
description: 'The build directory where your HTML files to run Lighthouse on are located.',
},
isSinglePageApplication: {
description:
'If the application is created by Single Page Application, enable redirect to index.html.',
},
chromePath: {
description: 'The path to the Chrome or Chromium executable to use for collection.',
default: process.env.CHROME_PATH || ChromeLauncher.getInstallations()[0],
Expand Down Expand Up @@ -125,7 +129,7 @@ async function startServerAndDetermineUrls(options) {
}

const pathToBuildDir = path.resolve(process.cwd(), options.staticDistDir);
const server = new FallbackServer(pathToBuildDir);
const server = new FallbackServer(pathToBuildDir, options.isSinglePageApplication);
await server.listen();
process.stdout.write(`Started a web server on port ${server.port}...\n`);

Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/collect/fallback-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ const createHttpServer = require('http').createServer;
class FallbackServer {
/**
* @param {string} pathToBuildDir
* @param {boolean|undefined} isSinglePageApplication
*/
constructor(pathToBuildDir) {
constructor(pathToBuildDir, isSinglePageApplication) {
this._pathToBuildDir = pathToBuildDir;
this._app = express();
this._app.use(compression());
this._app.use('/', express.static(pathToBuildDir));
if (isSinglePageApplication) {
this._app.use('/*', (req, res) => res.sendFile(pathToBuildDir + '/index.html'));
}
this._port = 0;
/** @type {undefined|ReturnType<typeof createHttpServer>} */
this._server = undefined;
Expand Down
51 changes: 51 additions & 0 deletions packages/cli/test/fallback-server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
/* eslint-env jest */
const path = require('path');
const {startFallbackServer} = require('./test-utils.js');
const request = require('request');

describe('fallbackServer', () => {
const fixturesDir = path.join(__dirname, 'fixtures');

it('should "/" request result without isSinglePageApplication', async () => {
const server = await startFallbackServer(fixturesDir, false);
const body = await new Promise(resolve => {
request({url: `http://localhost:${server.port}`}, (error, response, body) => {
resolve(body);
});
});
expect(body.includes('index test page for staticDistDir usage')).toEqual(true);
});

it('should "/" request result with isSinglePageApplication', async () => {
const server = await startFallbackServer(fixturesDir, true);
const body = await new Promise(resolve => {
request({url: `http://localhost:${server.port}`}, (error, response, body) => {
resolve(body);
});
});
expect(body.includes('index test page for staticDistDir usage')).toEqual(true);
});

it('should "/japan" request result without isSinglePageApplication', async () => {
const server = await startFallbackServer(fixturesDir, false);

const body = await new Promise(resolve => {
request({url: `http://localhost:${server.port}/japan`}, (error, response, body) => {
resolve(body);
});
});

expect(body.includes('Cannot GET /japan')).toEqual(true);
});

it('should "/japan" request result with isSinglePageApplication', async () => {
const server = await startFallbackServer(fixturesDir, true);
const body = await new Promise(resolve => {
request({url: `http://localhost:${server.port}/japan`}, (error, response, body) => {
resolve(body);
});
});
expect(body.includes('index test page for staticDistDir usage')).toEqual(true);
});
});
15 changes: 15 additions & 0 deletions packages/cli/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');
const rimraf = require('rimraf');
const {spawn, spawnSync} = require('child_process');
const testingLibrary = require('@testing-library/dom');
const FallbackServer = require('../src/collect/fallback-server.js');

const CLI_PATH = path.join(__dirname, '../src/cli.js');
const UUID_REGEX = /[0-9a-f-]{36}/gi;
Expand Down Expand Up @@ -107,6 +108,19 @@ function runCLI(args, overrides = {}) {
return {stdout, stderr, status, matches: {uuids}};
}

/**
* @param {string} staticDistDir
* @param {boolean} isSinglePageApplication
* @returns {Promise<FallbackServer>}
*/
async function startFallbackServer(staticDistDir, isSinglePageApplication) {
const pathToBuildDir = path.resolve(process.cwd(), staticDistDir);
const server = new FallbackServer(pathToBuildDir, isSinglePageApplication);
await server.listen();
process.stdout.write(`Started a web server on port ${server.port}...\n`);
return server;
}

module.exports = {
CLI_PATH,
runCLI,
Expand All @@ -116,4 +130,5 @@ module.exports = {
safeDeleteFile,
withTmpDir,
cleanStdOutput,
startFallbackServer,
};
1 change: 1 addition & 0 deletions types/collect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare global {
export interface Options {
url?: string | string[];
staticDistDir?: string;
isSinglePageApplication?: boolean;
startServerCommand?: string;
startServerReadyPattern: string;
chromePath?: string;
Expand Down