Dhruv Amir
Dhruv Amir

Reputation: 33

webpack--watch not working

webpack --watch is not working but the webpack does,when I edit some changes in my js the watcher will not make any updates in my DOM(html) so I will have to restart(do webpack again) so that it will update the changes.

codes that might have the bug:

package.json:

`{
  "name": "samplereact",
  "version": "1.0.0",
  "main": "webpack.config.js",
  "dependencies": {
    "babel-loader": "^6.2.4",
    "babel-plugin-add-module-exports": "^0.1.4",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.9.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0",
    "core-js": "^2.4.0",
    "react": "^0.14.8",
    "react-dom": "^0.14.8",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "devDependencies": {},
  "scripts": {
    "dev": "./node_modules/.bin/webpack-dev-server --content-base src --inline --hot",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}
`

webpack.config.js:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
  context: __dirname + "/src",
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./js/scripts.js",
  module: {
    loaders: [
      {
        test: /\.js?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
        }
      }
    ]
  },
  output: {
    path: __dirname + "/src/",
    filename: "scripts.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false })
  ]
};

And my folder structure is this: enter image description here

I only put package.json and webpack.config.js here because I suspected that only one of them is the source why my watch is not working.

btw I followed this tutorial(https://www.youtube.com/watch?v=MhkGQAoc7bc) and even tho' I watched it 5 times,I cant still find the problem why this watch is not working.

Upvotes: 3

Views: 6542

Answers (1)

rossipedia
rossipedia

Reputation: 59367

You're probably running into webpack's sensitivity to path separators, which has bitten me numerous times.

The trick is to use path.sep or path.join instead of / when specifying your context. So you'd change this:

context: __dirname + '/src',
entry: './src/scripts.js',

to this:

context: path.join(__dirname, 'src'),
entry: './scripts.js',

Note that the entry point can still use ./, as it's resolved using require, but the context is what webpack uses for --watch (also, you had src in both context and entry, which I assume is redundant).

Upvotes: 3

Related Questions