Skip to content

Commit f6054a0

Browse files
fix: compatibility with webpack@5 API (#737)
1 parent f7408fd commit f6054a0

File tree

4 files changed

+312
-79
lines changed

4 files changed

+312
-79
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,19 @@ Examples of use with other servers will follow here.
351351
Fastify interop will require the use of `fastify-express` instead of `middie` for providing middleware support. As the authors of `fastify-express` recommend, this should only be used as a stopgap while full Fastify support is worked on.
352352

353353
```js
354-
const fastify = require('fastify')()
355-
const webpack = require('webpack')
356-
const webpackConfig = require('./webpack.config.js')
357-
const devMiddleware = require('webpack-dev-middleware')
358-
359-
const compiler = webpack(webpackConfig)
360-
const { publicPath } = webpackConfig.output
361-
362-
;(async () => {
363-
await fastify.register(require('fastify-express'))
364-
await fastify.use(devMiddleware(compiler, { publicPath }))
365-
await fastify.listen(3000)
366-
})()
354+
const fastify = require('fastify')();
355+
const webpack = require('webpack');
356+
const webpackConfig = require('./webpack.config.js');
357+
const devMiddleware = require('webpack-dev-middleware');
358+
359+
const compiler = webpack(webpackConfig);
360+
const { publicPath } = webpackConfig.output;
361+
362+
(async () => {
363+
await fastify.register(require('fastify-express'));
364+
await fastify.use(devMiddleware(compiler, { publicPath }));
365+
await fastify.listen(3000);
366+
})();
367367
```
368368

369369
## Contributing

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+35-40
Original file line numberDiff line numberDiff line change
@@ -48,51 +48,46 @@ export default function wdm(compiler, options = {}) {
4848

4949
setupOutputFileSystem(context);
5050

51-
let watchOptions;
52-
53-
if (Array.isArray(context.compiler.compilers)) {
54-
watchOptions = context.compiler.compilers.map(
55-
(childCompiler) => childCompiler.options.watchOptions || {}
56-
);
57-
} else {
58-
watchOptions = context.compiler.options.watchOptions || {};
59-
}
60-
6151
// Start watching
62-
context.watching = context.compiler.watch(watchOptions, (error) => {
63-
if (error) {
64-
// TODO: improve that in future
65-
// For example - `writeToDisk` can throw an error and right now it is ends watching.
66-
// We can improve that and keep watching active, but it is require API on webpack side.
67-
// Let's implement that in webpack@5 because it is rare case.
68-
context.logger.error(error);
52+
if (context.compiler.watching) {
53+
context.watching = context.compiler.watching;
54+
} else {
55+
let watchOptions;
56+
57+
if (Array.isArray(context.compiler.compilers)) {
58+
watchOptions = context.compiler.compilers.map(
59+
(childCompiler) => childCompiler.options.watchOptions || {}
60+
);
61+
} else {
62+
watchOptions = context.compiler.options.watchOptions || {};
6963
}
70-
});
71-
72-
return Object.assign(middleware(context), {
73-
waitUntilValid(callback) {
74-
// eslint-disable-next-line no-param-reassign
75-
callback = callback || noop;
7664

77-
ready(context, callback);
78-
},
79-
80-
invalidate(callback) {
81-
// eslint-disable-next-line no-param-reassign
82-
callback = callback || noop;
83-
84-
ready(context, callback);
65+
context.watching = context.compiler.watch(watchOptions, (error) => {
66+
if (error) {
67+
// TODO: improve that in future
68+
// For example - `writeToDisk` can throw an error and right now it is ends watching.
69+
// We can improve that and keep watching active, but it is require API on webpack side.
70+
// Let's implement that in webpack@5 because it is rare case.
71+
context.logger.error(error);
72+
}
73+
});
74+
}
8575

86-
context.watching.invalidate();
87-
},
76+
const instance = middleware(context);
8877

89-
close(callback) {
90-
// eslint-disable-next-line no-param-reassign
91-
callback = callback || noop;
78+
// API
79+
instance.waitUntilValid = (callback = noop) => {
80+
ready(context, callback);
81+
};
82+
instance.invalidate = (callback = noop) => {
83+
ready(context, callback);
9284

93-
context.watching.close(callback);
94-
},
85+
context.watching.invalidate();
86+
};
87+
instance.close = (callback = noop) => {
88+
context.watching.close(callback);
89+
};
90+
instance.context = context;
9591

96-
context,
97-
});
92+
return instance;
9893
}

0 commit comments

Comments
 (0)