Bob
Bob

Reputation: 861

Webpack Not Rebuilding on Change

For some reason I can't get webpack to rebuild my files on change. I basically followed the Browsersync - Webpack + TypeScript Recipe.

My webpack.config.js:

let path = require('path');
let webpack = require('webpack');

let config = {
  debug: true,
  devtool: 'eval',
  entry: './app/index.ts',
  output: {
    publicPath: '/',
    path: path.join(__dirname, 'wwwroot'),
    filename: 'bundle.js'
  },
  plugins: [],
  resolve: {
    extensions: ['', '.ts', '.js']
  },
  module: {
    loaders: [
      { test: /\.ts$/, loader: 'ts', include: path.join(__dirname, 'app') }
    ]
  }
};
module.exports = config;

My browser-sync configuration (server.js) which I literally copied from the recipe:

var browserSync          = require('browser-sync').create();
var webpack              = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var stripAnsi            = require('strip-ansi');
var webpackConfig = require('./webpack.config');
var bundler       = webpack(webpackConfig);

bundler.plugin('done', function (stats) {
    if (stats.hasErrors() || stats.hasWarnings()) {
        return browserSync.sockets.emit('fullscreen:message', {
            title: "Webpack Error:",
            body:  stripAnsi(stats.toString()),
            timeout: 100000
        });
    }
    browserSync.reload();
});

browserSync.init({
    server: 'wwwroot',
    open: false,
    logFileChanges: false,
    middleware: [
        webpackDevMiddleware(bundler, {
            publicPath: webpackConfig.output.publicPath,
            stats: {colors: true}
        })
    ],
    plugins: ['bs-fullscreen-message'],
    files: [
    ]
});

And to start it all i simply use the npm scripts section:

"scripts": {
  "build": "node server"
},

Whenever I change a typescript file in app/ nothing happens. What am I doing wrong here?

Upvotes: 3

Views: 3763

Answers (2)

jrsmolley
jrsmolley

Reputation: 419

Not a direct answer, but leaving this here in case it may help someone else too:

When editing a particular app.tsx file in my project (notice the casing), I was encountering a strange problem where webpack would only rebuild the file the first time I saved changes, but not the second or third or fourth, etc.

The problem was because the file I edited was imported by a main.tsx file as follows. (Both files are in the same directory)

// main.tsx
import App from "./App";  // Stupid casing typo!
//...
ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById("root")
);

Interestingly compilation always succeeded on Windows and Unix despite the casing differences.

Once I changed the import statement in main.tsx to the following, I was able to edit app.tsx, save changes, and webpack would rebuild properly every time:

import App from "./app";

Upvotes: 2

Bob
Bob

Reputation: 861

Fixed it by explicitly adding watch section to webpack dev middleware configuration.

As requested here is the config change:

browserSync.init({
  ...
  middleware: [
    webpackDevMiddleware(bundler, {
      // Explicitly set watch options:
      watchOptions: {
        aggregateTimeout: 300,
        poll: true
      }
    })
  ]
});

Upvotes: 4

Related Questions