Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unused webpack chunked files

I'm looking for information on how to delete old webpack chunked files. Here is my current webpack configuration:

var path = require('path'); var webpack = require('webpack');  module.exports = {   debug: false,   outputPathinfo: true,   displayErrorDetails: true,   context: __dirname,   entry: {     common: ['./src/common.coffee'],     a: './src/a.cjsx',     b: './src/b.cjsx'   },   output: {     filename: '[name]-[chunkhash].js',     chunkFileName: '[name].[chunkhash].js',     path: path.join(__dirname, 'js')   },   plugins: [     new webpack.optimize.CommonsChunkPlugin('common', 'common-[chunkhash].js'),     new webpack.optimize.UglifyJsPlugin({       compress: { warnings: false }     })   ],   module: {     preLoaders: [       {         test: /\.coffee$/,         exclude: /node_modules/,         loader: 'coffeelint-loader'       }     ],     loaders: [       { test: /\.coffee/, loader: 'coffee' },       { test: /\.cjsx$/, loaders: ['coffee', 'cjsx'] },       { test: /\.js$/, loader: 'jsx-loader?harmony' }     ]   } } 

If I am running $(npm bin)/webpack --config webpack.js --watch and make changes to a.cjsx, it compiles a newer version of that file with a new chunkedhash. However, the old one remains and I'd like it to be deleted right away.

  1. How can I delete the old version of the chunked file?
  2. Is there a way for me to hook into an after callback once watch finishes compiling?
like image 203
Jey Balachandran Avatar asked Jul 30 '15 23:07

Jey Balachandran


2 Answers

There is a clean-webpack-plugin for those purposes, or you can write a simple bash script for npm:

 "scripts": {     "build": "rm -r dist/* && webpack -p",     "clean": "rm -r dist/*"   } 
like image 172
arturkin Avatar answered Sep 19 '22 23:09

arturkin


Here is the webpack-clean-obsolete-chunks plugin, which do what you want. It searches for all updated chunks and deletes obsolete files after each webpack compilation.

like image 36
GProst Avatar answered Sep 18 '22 23:09

GProst