Limit requests sent to webpack middleware
This commit is contained in:
@@ -79,6 +79,7 @@
|
|||||||
"proj4": "2.5.0",
|
"proj4": "2.5.0",
|
||||||
"puppeteer": "^1.10.0",
|
"puppeteer": "^1.10.0",
|
||||||
"rollup": "0.67.0",
|
"rollup": "0.67.0",
|
||||||
|
"serve-static": "^1.13.2",
|
||||||
"sinon": "^6.0.0",
|
"sinon": "^6.0.0",
|
||||||
"typescript": "^3.1.0-dev.20180905",
|
"typescript": "^3.1.0-dev.20180905",
|
||||||
"uglifyjs-webpack-plugin": "^2.0.1",
|
"uglifyjs-webpack-plugin": "^2.0.1",
|
||||||
|
|||||||
+22
-15
@@ -2,7 +2,7 @@
|
|||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const config = require('./webpack.config');
|
const config = require('./webpack.config');
|
||||||
const middleware = require('webpack-dev-middleware');
|
const webpackMiddleware = require('webpack-dev-middleware');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const png = require('pngjs');
|
const png = require('pngjs');
|
||||||
@@ -12,6 +12,7 @@ const pixelmatch = require('pixelmatch');
|
|||||||
const yargs = require('yargs');
|
const yargs = require('yargs');
|
||||||
const log = require('loglevelnext');
|
const log = require('loglevelnext');
|
||||||
const globby = require('globby');
|
const globby = require('globby');
|
||||||
|
const serveStatic = require('serve-static');
|
||||||
|
|
||||||
const compiler = webpack(Object.assign({mode: 'development'}, config));
|
const compiler = webpack(Object.assign({mode: 'development'}, config));
|
||||||
|
|
||||||
@@ -19,14 +20,10 @@ function getHref(entry) {
|
|||||||
return path.dirname(entry).slice(1) + '/';
|
return path.dirname(entry).slice(1) + '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const staticHandler = serveStatic(__dirname);
|
||||||
|
|
||||||
function notFound(req, res) {
|
function notFound(req, res) {
|
||||||
return () => {
|
return () => {
|
||||||
if (req.url === '/favicon.ico') {
|
|
||||||
res.writeHead(204);
|
|
||||||
res.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const items = [];
|
const items = [];
|
||||||
for (const key in config.entry) {
|
for (const key in config.entry) {
|
||||||
const href = getHref(config.entry[key]);
|
const href = getHref(config.entry[key]);
|
||||||
@@ -42,7 +39,7 @@ function notFound(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function serve(options) {
|
function serve(options) {
|
||||||
const handler = middleware(compiler, {
|
const webpackHandler = webpackMiddleware(compiler, {
|
||||||
lazy: true,
|
lazy: true,
|
||||||
logger: options.log,
|
logger: options.log,
|
||||||
stats: 'minimal'
|
stats: 'minimal'
|
||||||
@@ -50,7 +47,18 @@ function serve(options) {
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
handler(req, res, notFound(req, res));
|
if (req.url === '/favicon.ico') {
|
||||||
|
res.writeHead(204);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.extname(req.url) === '.js') {
|
||||||
|
webpackHandler(req, res, notFound(req, res));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
staticHandler(req, res, notFound(req, res));
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(options.port, options.host, err => {
|
server.listen(options.port, options.host, err => {
|
||||||
@@ -141,8 +149,10 @@ async function renderPage(page, entry, options) {
|
|||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
options.log.debug('navigating', entry);
|
||||||
await page.goto(`http://${options.host}:${options.port}${getHref(entry)}`, {waitUntil: 'networkidle0'});
|
await page.goto(`http://${options.host}:${options.port}${getHref(entry)}`, {waitUntil: 'networkidle0'});
|
||||||
await renderCalled;
|
await renderCalled;
|
||||||
|
options.log.debug('screenshot', entry);
|
||||||
await page.screenshot({path: getActualScreenshotPath(entry)});
|
await page.screenshot({path: getActualScreenshotPath(entry)});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,14 +242,14 @@ async function getOutdated(entries, options) {
|
|||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
const passPath = getPassFilePath(entry);
|
const passPath = getPassFilePath(entry);
|
||||||
const passTime = await getLatest(passPath);
|
const passTime = await getLatest(passPath);
|
||||||
options.log.debug(entry, 'pass time', passTime);
|
options.log.debug('pass time', entry, passTime);
|
||||||
if (passTime < libTime) {
|
if (passTime < libTime) {
|
||||||
outdated.push(entry);
|
outdated.push(entry);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const caseTime = await getLatest(path.join(__dirname, path.dirname(entry), '**', '*'));
|
const caseTime = await getLatest(path.join(__dirname, path.dirname(entry), '**', '*'));
|
||||||
options.log.debug(entry, 'case time', caseTime);
|
options.log.debug('case time', entry, caseTime);
|
||||||
if (passTime < caseTime) {
|
if (passTime < caseTime) {
|
||||||
outdated.push(entry);
|
outdated.push(entry);
|
||||||
continue;
|
continue;
|
||||||
@@ -254,7 +264,7 @@ async function main(entries, options) {
|
|||||||
if (!options.force) {
|
if (!options.force) {
|
||||||
entries = await getOutdated(entries, options);
|
entries = await getOutdated(entries, options);
|
||||||
}
|
}
|
||||||
if (entries.length === 0) {
|
if (!options.interactive && entries.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,9 +328,6 @@ if (require.main === module) {
|
|||||||
|
|
||||||
const entries = Object.keys(config.entry).map(key => config.entry[key]);
|
const entries = Object.keys(config.entry).map(key => config.entry[key]);
|
||||||
|
|
||||||
if (options.interactive) {
|
|
||||||
options.force = true;
|
|
||||||
}
|
|
||||||
options.log = log.create({name: 'rendering', level: options.logLevel});
|
options.log = log.create({name: 'rendering', level: options.logLevel});
|
||||||
|
|
||||||
main(entries, options).catch(err => {
|
main(entries, options).catch(err => {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
const CopyPlugin = require('copy-webpack-plugin');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
@@ -15,22 +14,5 @@ module.exports = {
|
|||||||
context: __dirname,
|
context: __dirname,
|
||||||
target: 'web',
|
target: 'web',
|
||||||
entry: entry,
|
entry: entry,
|
||||||
module: {
|
|
||||||
rules: [{
|
|
||||||
use: {
|
|
||||||
loader: 'buble-loader'
|
|
||||||
},
|
|
||||||
test: /\.js$/,
|
|
||||||
include: [
|
|
||||||
path.join(__dirname, '..', 'src')
|
|
||||||
]
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
new CopyPlugin([
|
|
||||||
{from: '../src/ol/ol.css', to: 'css'},
|
|
||||||
{from: 'cases/**/*.html'}
|
|
||||||
])
|
|
||||||
],
|
|
||||||
devtool: 'source-map'
|
devtool: 'source-map'
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user