|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const ejs = require('ejs'); |
| 4 | +const webpack = require('webpack'); |
| 5 | + |
| 6 | +const data = { |
| 7 | + webpackVersion: webpack.version, |
| 8 | + webpackDevServerVersion: '2.4.1', |
| 9 | + progress: [[0]], |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * @typedef {object} WebpackDevServerWaitpageOptions |
| 14 | + * @property title {string} |
| 15 | + * @property theme {string} |
| 16 | + * @property template {string} |
| 17 | + * @property disableWhenValid {boolean} |
| 18 | + */ |
| 19 | + |
| 20 | +/** @type {WebpackDevServerWaitpageOptions} */ |
| 21 | +const defaultOptions = { |
| 22 | + title: 'Development Server', |
| 23 | + theme: 'pl-loading', |
| 24 | + disableWhenValid: true, |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * webpack-dev-server-waitpage middleware factory |
| 29 | + * @param server {Server} The server argument passed to webpack-dev-server's 'before' function |
| 30 | + * @param [options] {WebpackDevServerWaitpageOptions} An optional object of options (see Readme for more information) |
| 31 | + * @returns {Function} Koa compatible middleware |
| 32 | + */ |
| 33 | +const webpackDevServerWaitpage = (server, options) => { |
| 34 | + if (!server) |
| 35 | + throw new Error( |
| 36 | + `webpack-dev-server's compilers argument must be supplied as first parameter.` |
| 37 | + ); |
| 38 | + |
| 39 | + /** @type {WebpackDevServerWaitpageOptions} */ |
| 40 | + options = Object.assign({}, defaultOptions, options); |
| 41 | + |
| 42 | + const compilers = server.compilers; |
| 43 | + // || [server.middleware.context.compiler]; |
| 44 | + for (let i = 0; i < compilers.length; i++) { |
| 45 | + new webpack.ProgressPlugin(function() { |
| 46 | + data.progress[i] = arguments; |
| 47 | + }).apply(compilers[i]); |
| 48 | + } |
| 49 | + |
| 50 | + let template = options.template; |
| 51 | + if (!template) { |
| 52 | + if ( |
| 53 | + fs |
| 54 | + .readdirSync(__dirname) |
| 55 | + .filter(x => x.endsWith('.ejs')) |
| 56 | + .map(x => x.slice(0, -4)) |
| 57 | + .indexOf(options.theme) < 0 |
| 58 | + ) |
| 59 | + throw new Error(`Unknown theme provided: ${options.theme}`); |
| 60 | + template = fs.readFileSync( |
| 61 | + path.resolve(__dirname, options.theme + '.ejs'), |
| 62 | + 'utf8' |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + // eslint-disable-next-line no-return-assign |
| 67 | + Object.keys(options).forEach(key => (data[key] = options[key])); // expend data with options |
| 68 | + |
| 69 | + let wasValid = false; |
| 70 | + |
| 71 | + return async (req, res, next) => { |
| 72 | + const valid = data.progress.every(p => p[0] === 1); |
| 73 | + wasValid = wasValid || valid; |
| 74 | + |
| 75 | + if ( |
| 76 | + valid || // already valid |
| 77 | + (options.disableWhenValid && wasValid) || // if after valid state should be disabled |
| 78 | + req.method !== 'GET' |
| 79 | + ) { |
| 80 | + return await next(); |
| 81 | + } else { |
| 82 | + res.setHeader('Content-Type', 'text/html'); |
| 83 | + res.end(ejs.render(template, data)); |
| 84 | + } |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +module.exports = webpackDevServerWaitpage; |
0 commit comments