Skip to content

Instantly share code, notes, and snippets.

@fivethreeo
Last active June 25, 2021 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fivethreeo/0e2c6a467b6e1236b862082a317ca938 to your computer and use it in GitHub Desktop.
Save fivethreeo/0e2c6a467b6e1236b862082a317ca938 to your computer and use it in GitHub Desktop.
const WebpackLastFilesLoggerPlugin = require('./WebpackLastFilesLoggerPlugin');
module.exports = {
modifyWebpackConfig({ // note Config not Options
env: {
target, // the target 'node' or 'web'
dev, // is this a development build? true or false
},
webpackConfig, // the created webpack config
webpackObject, // the imported webpack node module
options: {
razzleOptions, // the modified options passed to Razzle in the `options` key in `razzle.config.js` (options: { key: 'value'})
webpackOptions, // the modified options that will be used to configure webpack/ webpack loaders and plugins
},
paths, // the modified paths that will be used by Razzle.
}) {
webpackConfig.plugins.push(new WebpackLastFilesLoggerPlugin());
return webpackConfig;
},
};
"use strict";
// https://github.com/storybookjs/storybook/issues/14342
// https://webpack.js.org/contribute/plugin-patterns/#monitoring-the-watch-graph
/**
* Logs the last changed files during webpack rebuilds.
*/
class WebpackLastFilesLoggerPlugin {
constructor() {
this.startTime = Date.now();
this.prevTimestamps = new Map();
}
apply(compiler) {
compiler.hooks.emit.tapAsync('MyPlugin', (compilation, callback) => {
const changedFiles = Array.from(compilation.fileTimestamps.keys()).filter((watchfile) => {
return ((this.prevTimestamps.get(watchfile) || this.startTime) <
(compilation.fileTimestamps.get(watchfile) || Infinity));
});
if (changedFiles.length) {
console.log(`
The following files changed since last build:
${changedFiles.map((file) => `- ${file}`).join('\n')}
`);
}
this.prevTimestamps = compilation.fileTimestamps;
callback();
});
}
}
module.exports = WebpackLastFilesLoggerPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment