Search code examples
webpackwebpack-dev-serverincremental-build

Configure webpack to write to file and watch for changes


I have this webpack configuration:

module.exports = {

  entry: ['babel-polyfill', './lib/index.js'],

  output: {
    path: path.resolve(__dirname + '/dist'),
    filename: 'suman.js'
  },

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets: ['latest'],
          plugins: ['transform-runtime']
        }
      },
      {
        test: /(\/lib\/init\/|cli.js$)/,
        loader: 'ignore-loader'
      }
    ]
  },

  resolve: {
    alias: {
      fs: require.resolve('suman-browser-polyfills/modules/fs'),
      // assert: require.resolve('suman-browser-polyfills/modules/assert'),
      process: require.resolve('suman-browser-polyfills/modules/process'),
    },
    extensions: ['.js']
  }
};

as you can see Webpack is configured to write to a file, denoted by the "output" property in the configuration.

However, when I use webpack-dev-server, it will create a build, but it won't write it to the "output" file, it seems to just serve it to the browser.

What I am looking to do is have Webpack do incremental builds, but write to the file upon changes.


Solution

  • I believe the answer is simply to use webpack --watch, there is no need for me to use webpack-dev-server for my use case.