rsbuild-dev-middleware
v0.1.2
Published
A development middleware for Rsbuild
Downloads
2,168
Readme
webpack-dev-middleware
An express-style development middleware for use with webpack bundles and allows for serving of the files emitted from webpack. This should be used for development only.
Some of the benefits of using this middleware include:
- No files are written to disk, rather it handles files in memory
- If files changed in watch mode, the middleware delays requests until compiling has completed.
- Supports hot module reload (HMR).
Getting Started
First thing's first, install the module:
npm install webpack-dev-middleware --save-dev
Warning
We do not recommend installing this module globally.
Usage
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");
const compiler = webpack({
// webpack options
});
const express = require("express");
const app = express();
app.use(
middleware(compiler, {
// webpack-dev-middleware options
}),
);
app.listen(3000, () => console.log("Example app listening on port 3000!"));
Options
| Name | Type | Default | Description |
| :-------------------------------: | :-----------------: | :----------------------------------------: | :------------------------------------------------------------------------------------------------------------------- |
| index
| Boolean\|String
| index.html
| If false
(but not undefined
), the server will not respond to requests to the root URL. |
| publicPath
| String
| output.publicPath
(from a configuration) | The public path that the middleware is bound to. |
| writeToDisk
| Boolean\|Function
| false
| Instructs the module to write files to the configured location on disk as specified in your webpack
configuration. |
The middleware accepts an options
Object. The following is a property reference for the Object.
index
Type: Boolean|String
Default: index.html
If false
(but not undefined
), the server will not respond to requests to the root URL.
lastModified
Type: Boolean
Default: undefined
Enable or disable Last-Modified
header. Uses the file system's last modified value.
publicPath
Type: String
Default: output.publicPath
(from a configuration)
The public path that the middleware is bound to.
Best Practice: use the same publicPath
defined in your webpack config. For more information about publicPath
, please see the webpack documentation.
writeToDisk
Type: Boolean|Function
Default: false
If true
, the option will instruct the module to write files to the configured location on disk as specified in your webpack
config file.
Setting writeToDisk: true
won't change the behavior of the webpack-dev-middleware
, and bundle files accessed through the browser will still be served from memory.
This option provides the same capabilities as the WriteFilePlugin
.
This option also accepts a Function
value, which can be used to filter which files are written to disk.
The function follows the same premise as Array#filter
in which a return value of false
will not write the file, and a return value of true
will write the file to disk. eg.
const webpack = require("webpack");
const configuration = {
/* Webpack configuration */
};
const compiler = webpack(configuration);
middleware(compiler, {
writeToDisk: (filePath) => {
return /superman\.css$/.test(filePath);
},
});
API
webpack-dev-middleware
also provides convenience methods that can be use to
interact with the middleware at runtime:
close(callback)
Instructs webpack-dev-middleware
instance to stop watching for file changes.
Parameters
callback
Type: Function
Required: No
A function executed once the middleware has stopped watching.
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(() => {
// 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.
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);
// Recompile the bundle with the banner plugin:
instance.invalidate();
}, 1000);
waitUntilValid(callback)
Executes a callback function when the compiler bundle is valid, typically after compilation.
Parameters
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.
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");
});
getFilenameFromUrl(url)
Get filename from URL.
Parameters
url
Type: String
Required: Yes
URL for the requested file.
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}`);
});
FAQ
Avoid blocking requests to non-webpack resources.
Since output.publicPath
and output.filename
/output.chunkFilename
can be dynamic, it's not possible to know which files are webpack bundles (and they public paths) and which are not, so we can't avoid blocking requests.
But there is a solution to avoid it - mount the middleware to a non-root route, for example:
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");
const compiler = webpack({
// webpack options
});
const express = require("express");
const app = express();
// Mounting the middleware to the non-root route allows avoids this.
// Note - check your public path, if you want to handle `/dist/`, you need to setup `output.publicPath` to `/` value.
app.use(
"/dist/",
middleware(compiler, {
// webpack-dev-middleware options
}),
);
app.listen(3000, () => console.log("Example app listening on port 3000!"));
Support
We do our best to keep Issues in the repository focused on bugs, features, and needed modifications to the code for the module. Because of that, we ask users with general support, "how-to", or "why isn't this working" questions to try one of the other support channels that are available.
Your first-stop-shop for support for webpack-dev-server should by the excellent documentation for the module. If you see an opportunity for improvement of those docs, please head over to the webpack.js.org repo and open a pull request.
From there, we encourage users to visit the webpack discussions and
talk to the fine folks there. If your quest for answers comes up dry in chat,
head over to StackOverflow and do a quick search or open a new
question. Remember; It's always much easier to answer questions that include your
webpack.config.js
and relevant files!
If you're twitter-savvy you can tweet #webpack with your question and someone should be able to reach out and lend a hand.
If you have discovered a :bug:, have a feature suggestion, or would like to see a modification, please feel free to create an issue on Github. Note: The issue template isn't optional, so please be sure not to remove it, and please fill it out completely.
Other servers
Examples of use with other servers will follow here.
Connect
const connect = require("connect");
const http = require("http");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config.js");
const devMiddleware = require("webpack-dev-middleware");
const compiler = webpack(webpackConfig);
const devMiddlewareOptions = {
/** Your webpack-dev-middleware-options */
};
const app = connect();
app.use(devMiddleware(compiler, devMiddlewareOptions));
http.createServer(app).listen(3000);
Router
const http = require("http");
const Router = require("router");
const finalhandler = require("finalhandler");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config.js");
const devMiddleware = require("webpack-dev-middleware");
const compiler = webpack(webpackConfig);
const devMiddlewareOptions = {
/** Your webpack-dev-middleware-options */
};
const router = Router();
router.use(devMiddleware(compiler, devMiddlewareOptions));
var server = http.createServer((req, res) => {
router(req, res, finalhandler(req, res));
});
server.listen(3000);
Express
const express = require("express");
const webpack = require("webpack");
const webpackConfig = require("./webpack.config.js");
const devMiddleware = require("webpack-dev-middleware");
const compiler = webpack(webpackConfig);
const devMiddlewareOptions = {
/** Your webpack-dev-middleware-options */
};
const app = express();
app.use(devMiddleware(compiler, devMiddlewareOptions));
app.listen(3000, () => console.log("Example app listening on port 3000!"));
Contributing
Please take a moment to read our contributing guidelines if you haven't yet done so.