Skip to content

Commit 15e73ae

Browse files
committed
Clean up old .map files.
Also, make the .map file names different on every run in watch mode.
1 parent c9b2ecb commit 15e73ae

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

webpack.config.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ function StatusFilePlugin(mode) {
5959
};
6060
}
6161

62+
// A plugin which deletes old .map files. We can't use clean-webpack-plugin to
63+
// delete *.map before every compilation, because in watch mode, if a .js file
64+
// doesn't change then its .map file won't be regenerated.
65+
function CleanMapFilesPlugin() {
66+
this.apply = function(compiler) {
67+
compiler.hooks.afterEmit.tapAsync('CleanMapFilesPlugin', function(compilation, callback) {
68+
fs.readdirSync(outputPath).forEach(file => {
69+
if (file.match(/\.map$/) && !compilation.assets[file] &&
70+
compilation.assets[file.replace(/\.\w+\.map$/, '')]) {
71+
fs.unlinkSync(outputPath + '/' + file);
72+
}
73+
});
74+
callback();
75+
});
76+
}
77+
}
78+
6279
module.exports = function (env, args) {
6380
const mode = args.mode;
6481

@@ -70,11 +87,12 @@ module.exports = function (env, args) {
7087
output: {
7188
path: outputPath,
7289
filename: mode == 'development' ? '[name].dev.js' : '[name].min.js',
73-
sourceMapFilename: '[file].' + Date.now() + '.map', // it seems Chrome caches source maps even if "Disable cache" is enabled
90+
sourceMapFilename: '[file].[hash].map', // it seems Chrome caches source maps even if "Disable cache" is enabled
7491
},
7592
plugins: [
7693
new MiniCssExtractPlugin({ filename: 'style.css' }),
7794
new StatusFilePlugin(mode == 'development' ? 'dev' : 'prod'),
95+
new CleanMapFilesPlugin(),
7896
],
7997
module: {
8098
rules: [

0 commit comments

Comments
 (0)