webpack-dev-middleware
Advanced tools
Comparing version 4.2.0 to 4.3.0
"use strict"; | ||
const middleware = require('./index'); | ||
const middleware = require("./index"); | ||
module.exports = middleware.default; |
@@ -14,2 +14,4 @@ "use strict"; | ||
var _getFilenameFromUrl = _interopRequireDefault(require("./utils/getFilenameFromUrl")); | ||
var _setupHooks = _interopRequireDefault(require("./utils/setupHooks")); | ||
@@ -31,4 +33,4 @@ | ||
(0, _schemaUtils.validate)(_options.default, options, { | ||
name: 'Dev Middleware', | ||
baseDataPath: 'options' | ||
name: "Dev Middleware", | ||
baseDataPath: "options" | ||
}); | ||
@@ -59,3 +61,3 @@ const { | ||
context.logger = context.compiler.getInfrastructureLogger('webpack-dev-middleware'); | ||
context.logger = context.compiler.getInfrastructureLogger("webpack-dev-middleware"); | ||
(0, _setupHooks.default)(context); | ||
@@ -93,2 +95,4 @@ | ||
instance.getFilenameFromUrl = url => (0, _getFilenameFromUrl.default)(context, url); | ||
instance.waitUntilValid = (callback = noop) => { | ||
@@ -95,0 +99,0 @@ (0, _ready.default)(context, callback); |
@@ -22,3 +22,3 @@ "use strict"; | ||
return async function middleware(req, res, next) { | ||
const acceptedMethods = context.options.methods || ['GET', 'HEAD']; // fixes #282. credit @cexoso. in certain edge situations res.locals is undefined. | ||
const acceptedMethods = context.options.methods || ["GET", "HEAD"]; // fixes #282. credit @cexoso. in certain edge situations res.locals is undefined. | ||
// eslint-disable-next-line no-param-reassign | ||
@@ -57,3 +57,3 @@ | ||
if (typeof headers === 'function') { | ||
if (typeof headers === "function") { | ||
headers = headers(req, res, context); | ||
@@ -76,3 +76,3 @@ } | ||
const contentTypeHeader = res.get ? res.get('Content-Type') : res.getHeader('Content-Type'); | ||
const contentTypeHeader = res.get ? res.get("Content-Type") : res.getHeader("Content-Type"); | ||
@@ -88,6 +88,6 @@ if (!contentTypeHeader) { | ||
if (res.set) { | ||
res.set('Content-Type', contentType); | ||
res.set("Content-Type", contentType); | ||
} // Node.js API | ||
else { | ||
res.setHeader('Content-Type', contentType); | ||
res.setHeader("Content-Type", contentType); | ||
} | ||
@@ -118,5 +118,5 @@ } | ||
else { | ||
res.setHeader('Content-Length', content.length); | ||
res.setHeader("Content-Length", content.length); | ||
if (req.method === 'HEAD') { | ||
if (req.method === "HEAD") { | ||
res.end(); | ||
@@ -123,0 +123,0 @@ } else { |
@@ -27,3 +27,3 @@ "use strict"; | ||
const paths = (0, _getPaths.default)(context); | ||
let filename; | ||
let foundFilename; | ||
let urlObject; | ||
@@ -35,3 +35,3 @@ | ||
} catch (_ignoreError) { | ||
return filename; | ||
return; | ||
} | ||
@@ -43,6 +43,7 @@ | ||
} of paths) { | ||
let filename; | ||
let publicPathObject; | ||
try { | ||
publicPathObject = memoizedParse(publicPath !== 'auto' && publicPath ? publicPath : '/', false, true); | ||
publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true); | ||
} catch (_ignoreError) { | ||
@@ -73,5 +74,6 @@ // eslint-disable-next-line no-continue | ||
if (fsStats.isFile()) { | ||
foundFilename = filename; | ||
break; | ||
} else if (fsStats.isDirectory() && (typeof options.index === 'undefined' || options.index)) { | ||
const indexValue = typeof options.index === 'undefined' || typeof options.index === 'boolean' ? 'index.html' : options.index; | ||
} else if (fsStats.isDirectory() && (typeof options.index === "undefined" || options.index)) { | ||
const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index; | ||
filename = _path.default.join(filename, indexValue); | ||
@@ -87,2 +89,3 @@ | ||
if (fsStats.isFile()) { | ||
foundFilename = filename; | ||
break; | ||
@@ -92,5 +95,6 @@ } | ||
} | ||
} | ||
} // eslint-disable-next-line consistent-return | ||
return filename; | ||
return foundFilename; | ||
} |
@@ -21,3 +21,3 @@ "use strict"; | ||
const outputPath = compilation.getPath(compilation.outputOptions.path); | ||
const publicPath = options.publicPath ? compilation.getPath(options.publicPath) : compilation.outputOptions.publicPath ? compilation.getPath(compilation.outputOptions.publicPath) : ''; | ||
const publicPath = options.publicPath ? compilation.getPath(options.publicPath) : compilation.outputOptions.publicPath ? compilation.getPath(compilation.outputOptions.publicPath) : ""; | ||
publicPaths.push({ | ||
@@ -24,0 +24,0 @@ outputPath, |
@@ -16,5 +16,5 @@ "use strict"; | ||
if (res.set) { | ||
res.set('Accept-Ranges', 'bytes'); | ||
res.set("Accept-Ranges", "bytes"); | ||
} else { | ||
res.setHeader('Accept-Ranges', 'bytes'); | ||
res.setHeader("Accept-Ranges", "bytes"); | ||
} | ||
@@ -25,3 +25,3 @@ | ||
if (req.get) { | ||
range = req.get('range'); | ||
range = req.get("range"); | ||
} // Node.js API | ||
@@ -40,3 +40,3 @@ else { | ||
if (res.set) { | ||
res.set('Content-Range', `bytes */${content.length}`); | ||
res.set("Content-Range", `bytes */${content.length}`); | ||
res.status(416); | ||
@@ -47,10 +47,10 @@ } // Node.js API | ||
res.statusCode = 416; | ||
res.setHeader('Content-Range', `bytes */${content.length}`); | ||
res.setHeader("Content-Range", `bytes */${content.length}`); | ||
} | ||
} else if (ranges === -2) { | ||
// malformed header treated as regular response | ||
context.logger.error('A malformed Range header was provided. A regular response will be sent for this request.'); | ||
context.logger.error("A malformed Range header was provided. A regular response will be sent for this request."); | ||
} else if (ranges.length !== 1) { | ||
// multiple ranges treated as regular response | ||
context.logger.error('A Range header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request.'); | ||
context.logger.error("A Range header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request."); | ||
} else { | ||
@@ -65,3 +65,3 @@ // valid range header | ||
res.status(206); | ||
res.set('Content-Range', `bytes ${ranges[0].start}-${ranges[0].end}/${length}`); | ||
res.set("Content-Range", `bytes ${ranges[0].start}-${ranges[0].end}/${length}`); | ||
} // Node.js API | ||
@@ -72,3 +72,3 @@ else { | ||
res.statusCode = 206; | ||
res.setHeader('Content-Range', `bytes ${ranges[0].start}-${ranges[0].end}/${length}`); | ||
res.setHeader("Content-Range", `bytes ${ranges[0].start}-${ranges[0].end}/${length}`); | ||
} // eslint-disable-next-line no-param-reassign | ||
@@ -75,0 +75,0 @@ |
@@ -15,4 +15,4 @@ "use strict"; | ||
const name = req && req.url || callback.name; | ||
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ''}`); | ||
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`); | ||
context.callbacks.push(callback); | ||
} |
@@ -17,3 +17,3 @@ "use strict"; | ||
if (context.state) { | ||
context.logger.log('Compilation starting...'); | ||
context.logger.log("Compilation starting..."); | ||
} // We are now in invalid state | ||
@@ -32,6 +32,6 @@ // eslint-disable-next-line no-param-reassign | ||
if (statsForWebpack4) { | ||
if (typeof statsOptions === 'undefined') { | ||
if (typeof statsOptions === "undefined") { | ||
// eslint-disable-next-line no-param-reassign | ||
statsOptions = {}; | ||
} else if (typeof statsOptions === 'boolean' || typeof statsOptions === 'string') { | ||
} else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") { | ||
// eslint-disable-next-line no-param-reassign | ||
@@ -44,15 +44,15 @@ statsOptions = _webpack.default.Stats.presetToOptions(statsOptions); | ||
if (typeof statsOptions === 'undefined') { | ||
if (typeof statsOptions === "undefined") { | ||
// eslint-disable-next-line no-param-reassign | ||
statsOptions = { | ||
preset: 'normal' | ||
preset: "normal" | ||
}; | ||
} else if (typeof statsOptions === 'boolean') { | ||
} else if (typeof statsOptions === "boolean") { | ||
// eslint-disable-next-line no-param-reassign | ||
statsOptions = statsOptions ? { | ||
preset: 'normal' | ||
preset: "normal" | ||
} : { | ||
preset: 'none' | ||
preset: "none" | ||
}; | ||
} else if (typeof statsOptions === 'string') { | ||
} else if (typeof statsOptions === "string") { | ||
// eslint-disable-next-line no-param-reassign | ||
@@ -87,7 +87,7 @@ statsOptions = { | ||
logger.log('Compilation finished'); | ||
logger.log("Compilation finished"); | ||
const isMultiCompilerMode = Boolean(compiler.compilers); | ||
let statsOptions; | ||
if (typeof options.stats !== 'undefined') { | ||
if (typeof options.stats !== "undefined") { | ||
statsOptions = isMultiCompilerMode ? { | ||
@@ -107,3 +107,3 @@ children: compiler.compilers.map(() => options.stats) | ||
if (typeof childStatsOptions.colors === 'undefined') { | ||
if (typeof childStatsOptions.colors === "undefined") { | ||
// eslint-disable-next-line no-param-reassign | ||
@@ -118,3 +118,3 @@ childStatsOptions.colors = Boolean(_colorette.default.options.enabled); | ||
if (typeof statsOptions.colors === 'undefined') { | ||
if (typeof statsOptions.colors === "undefined") { | ||
statsOptions.colors = Boolean(_colorette.default.options.enabled); | ||
@@ -145,5 +145,5 @@ } | ||
context.compiler.hooks.watchRun.tap('webpack-dev-middleware', invalid); | ||
context.compiler.hooks.invalid.tap('webpack-dev-middleware', invalid); | ||
(context.compiler.webpack ? context.compiler.hooks.afterDone : context.compiler.hooks.done).tap('webpack-dev-middleware', done); | ||
context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid); | ||
context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid); | ||
(context.compiler.webpack ? context.compiler.hooks.afterDone : context.compiler.hooks.done).tap("webpack-dev-middleware", done); | ||
} |
@@ -23,9 +23,9 @@ "use strict"; | ||
if (typeof outputFileSystemFromOptions.join !== 'function') { | ||
throw new Error('Invalid options: options.outputFileSystem.join() method is expected'); | ||
if (typeof outputFileSystemFromOptions.join !== "function") { | ||
throw new Error("Invalid options: options.outputFileSystem.join() method is expected"); | ||
} // Todo remove when we drop webpack@4 support | ||
if (typeof outputFileSystemFromOptions.mkdirp !== 'function') { | ||
throw new Error('Invalid options: options.outputFileSystem.mkdirp() method is expected'); | ||
if (typeof outputFileSystemFromOptions.mkdirp !== "function") { | ||
throw new Error("Invalid options: options.outputFileSystem.mkdirp() method is expected"); | ||
} | ||
@@ -32,0 +32,0 @@ |
@@ -18,3 +18,3 @@ "use strict"; | ||
for (const compiler of compilers) { | ||
compiler.hooks.emit.tap('DevMiddleware', compilation => { | ||
compiler.hooks.emit.tap("DevMiddleware", compilation => { | ||
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) { | ||
@@ -24,3 +24,3 @@ return; | ||
compiler.hooks.assetEmitted.tapAsync('DevMiddleware', (file, info, callback) => { | ||
compiler.hooks.assetEmitted.tapAsync("DevMiddleware", (file, info, callback) => { | ||
let targetPath = null; | ||
@@ -36,3 +36,3 @@ let content = null; // webpack@5 | ||
let targetFile = file; | ||
const queryStringIdx = targetFile.indexOf('?'); | ||
const queryStringIdx = targetFile.indexOf("?"); | ||
@@ -54,3 +54,3 @@ if (queryStringIdx >= 0) { | ||
} = context.options; | ||
const allowWrite = filter && typeof filter === 'function' ? filter(targetPath) : true; | ||
const allowWrite = filter && typeof filter === "function" ? filter(targetPath) : true; | ||
@@ -63,3 +63,3 @@ if (!allowWrite) { | ||
const name = compiler.options.name ? `Child "${compiler.options.name}": ` : ''; | ||
const name = compiler.options.name ? `Child "${compiler.options.name}": ` : ""; | ||
return _fs.default.mkdir(dir, { | ||
@@ -66,0 +66,0 @@ recursive: true |
{ | ||
"name": "webpack-dev-middleware", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "A development middleware for webpack", | ||
@@ -51,7 +51,7 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@babel/cli": "^7.13.16", | ||
"@babel/core": "^7.14.0", | ||
"@babel/preset-env": "^7.14.1", | ||
"@commitlint/cli": "^12.1.1", | ||
"@commitlint/config-conventional": "^12.1.1", | ||
"@babel/cli": "^7.14.3", | ||
"@babel/core": "^7.14.3", | ||
"@babel/preset-env": "^7.14.2", | ||
"@commitlint/cli": "^12.1.4", | ||
"@commitlint/config-conventional": "^12.1.4", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
@@ -68,3 +68,2 @@ "babel-jest": "^26.6.3", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"execa": "^5.0.0", | ||
@@ -75,3 +74,3 @@ "express": "^4.17.1", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.4", | ||
"lint-staged": "^11.0.0", | ||
"npm-run-all": "^4.1.5", | ||
@@ -82,3 +81,3 @@ "prettier": "^2.3.0", | ||
"supertest": "^6.1.3", | ||
"webpack": "^5.36.2" | ||
"webpack": "^5.37.0" | ||
}, | ||
@@ -85,0 +84,0 @@ "keywords": [ |
162
README.md
@@ -41,8 +41,8 @@ <div align="center"> | ||
```js | ||
const webpack = require('webpack'); | ||
const middleware = require('webpack-dev-middleware'); | ||
const webpack = require("webpack"); | ||
const middleware = require("webpack-dev-middleware"); | ||
const compiler = webpack({ | ||
// webpack options | ||
}); | ||
const express = require('express'); | ||
const express = require("express"); | ||
const app = express(); | ||
@@ -56,3 +56,3 @@ | ||
app.listen(3000, () => console.log('Example app listening on port 3000!')); | ||
app.listen(3000, () => console.log("Example app listening on port 3000!")); | ||
``` | ||
@@ -87,3 +87,3 @@ | ||
return { | ||
'Last-Modified': new Date(), | ||
"Last-Modified": new Date(), | ||
}; | ||
@@ -99,3 +99,3 @@ }, | ||
headers: (req, res, context) => { | ||
res.setHeader('Last-Modified', new Date()); | ||
res.setHeader("Last-Modified", new Date()); | ||
}, | ||
@@ -159,3 +159,3 @@ }); | ||
```js | ||
const webpack = require('webpack'); | ||
const webpack = require("webpack"); | ||
const configuration = { | ||
@@ -186,6 +186,6 @@ /* Webpack configuration */ | ||
```js | ||
const webpack = require('webpack'); | ||
const path = require('path'); | ||
const myOutputFileSystem = require('my-fs'); | ||
const mkdirp = require('mkdirp'); | ||
const webpack = require("webpack"); | ||
const path = require("path"); | ||
const myOutputFileSystem = require("my-fs"); | ||
const mkdirp = require("mkdirp"); | ||
@@ -209,29 +209,61 @@ myOutputFileSystem.join = path.join.bind(path); // no need to bind | ||
Instructs a webpack-dev-middleware instance to stop watching for file changes. | ||
Instructs `webpack-dev-middleware` instance to stop watching for file changes. | ||
### Parameters | ||
#### Parameters | ||
#### callback | ||
##### `callback` | ||
Type: `Function` | ||
Required: `No` | ||
A function executed once the middleware has stopped watching. | ||
### `invalidate()` | ||
```js | ||
const express = require("express"); | ||
const webpack = require("webpack"); | ||
const compiler = webpack({ | ||
/* Webpack configuration */ | ||
}); | ||
const middleware = require("webpack-dev-middleware"); | ||
const instance = middleware(compiler); | ||
Instructs a webpack-dev-middleware instance to recompile the bundle. | ||
e.g. after a change to the configuration. | ||
const app = new express(); | ||
app.use(instance); | ||
setTimeout(() => { | ||
// Says `webpack` to stop watch changes | ||
instance.close(); | ||
}, 1000); | ||
``` | ||
### `invalidate(callback)` | ||
Instructs `webpack-dev-middleware` instance to recompile the bundle, e.g. after a change to the configuration. | ||
#### Parameters | ||
##### `callback` | ||
Type: `Function` | ||
Required: `No` | ||
A function executed once the middleware has invalidated. | ||
```js | ||
const webpack = require('webpack'); | ||
const compiler = webpack({ ... }); | ||
const middleware = require('webpack-dev-middleware'); | ||
const express = require("express"); | ||
const webpack = require("webpack"); | ||
const compiler = webpack({ | ||
/* Webpack configuration */ | ||
}); | ||
const middleware = require("webpack-dev-middleware"); | ||
const instance = middleware(compiler); | ||
const app = new express(); | ||
app.use(instance); | ||
setTimeout(() => { | ||
// After a short delay the configuration is changed and a banner plugin is added | ||
// to the config | ||
new webpack.BannerPlugin('A new banner').apply(compiler); | ||
// After a short delay the configuration is changed and a banner plugin is added to the config | ||
new webpack.BannerPlugin("A new banner").apply(compiler); | ||
@@ -248,24 +280,63 @@ // Recompile the bundle with the banner plugin: | ||
### Parameters | ||
#### Parameters | ||
#### callback | ||
##### `callback` | ||
Type: `Function` | ||
Required: `No` | ||
A function executed when the bundle becomes valid. If the bundle is | ||
valid at the time of calling, the callback is executed immediately. | ||
A function executed when the bundle becomes valid. | ||
If the bundle is valid at the time of calling, the callback is executed immediately. | ||
```js | ||
const webpack = require('webpack'); | ||
const compiler = webpack({ ... }); | ||
const middleware = require('webpack-dev-middleware'); | ||
const express = require("express"); | ||
const webpack = require("webpack"); | ||
const compiler = webpack({ | ||
/* Webpack configuration */ | ||
}); | ||
const middleware = require("webpack-dev-middleware"); | ||
const instance = middleware(compiler); | ||
const app = new express(); | ||
app.use(instance); | ||
instance.waitUntilValid(() => { | ||
console.log('Package is in a valid state'); | ||
console.log("Package is in a valid state"); | ||
}); | ||
``` | ||
### `getFilenameFromUrl(url)` | ||
Get filename from URL. | ||
#### Parameters | ||
##### `url` | ||
Type: `String` | ||
Required: `Yes` | ||
URL for the requested file. | ||
```js | ||
const express = require("express"); | ||
const webpack = require("webpack"); | ||
const compiler = webpack({ | ||
/* Webpack configuration */ | ||
}); | ||
const middleware = require("webpack-dev-middleware"); | ||
const instance = middleware(compiler); | ||
const app = new express(); | ||
app.use(instance); | ||
instance.waitUntilValid(() => { | ||
const filename = instance.getFilenameFromUrl("/bundle.js"); | ||
console.log(`Filename is ${filename}`); | ||
}); | ||
``` | ||
## Known Issues | ||
@@ -300,9 +371,12 @@ | ||
```js | ||
const webpack = require('webpack'); | ||
const express = require("express"); | ||
const webpack = require("webpack"); | ||
const compiler = webpack({ | ||
// webpack options | ||
/* Webpack configuration */ | ||
}); | ||
const isObject = require('is-object'); | ||
const middleware = require('webpack-dev-middleware'); | ||
const isObject = require("is-object"); | ||
const middleware = require("webpack-dev-middleware"); | ||
const app = new express(); | ||
// This function makes server rendering of asset references consistent with different webpack chunk/entry configurations | ||
@@ -334,5 +408,5 @@ function normalizeAssets(assets) { | ||
${normalizeAssets(assetsByChunkName.main) | ||
.filter((path) => path.endsWith('.css')) | ||
.filter((path) => path.endsWith(".css")) | ||
.map((path) => outputFileSystem.readFileSync(path.join(outputPath, path))) | ||
.join('\n')} | ||
.join("\n")} | ||
</style> | ||
@@ -343,5 +417,5 @@ </head> | ||
${normalizeAssets(assetsByChunkName.main) | ||
.filter((path) => path.endsWith('.js')) | ||
.filter((path) => path.endsWith(".js")) | ||
.map((path) => `<script src="${path}"></script>`) | ||
.join('\n')} | ||
.join("\n")} | ||
</body> | ||
@@ -388,6 +462,6 @@ </html> | ||
```js | ||
const fastify = require('fastify')(); | ||
const webpack = require('webpack'); | ||
const webpackConfig = require('./webpack.config.js'); | ||
const devMiddleware = require('webpack-dev-middleware'); | ||
const fastify = require("fastify")(); | ||
const webpack = require("webpack"); | ||
const webpackConfig = require("./webpack.config.js"); | ||
const devMiddleware = require("webpack-dev-middleware"); | ||
@@ -398,3 +472,3 @@ const compiler = webpack(webpackConfig); | ||
(async () => { | ||
await fastify.register(require('fastify-express')); | ||
await fastify.register(require("fastify-express")); | ||
await fastify.use(devMiddleware(compiler, { publicPath })); | ||
@@ -401,0 +475,0 @@ await fastify.listen(3000); |
28
652
495
41517
14