I'm using webpack with two entry points:
entry: {
js: './assets/index.js',
css: './assets/sass/all.scss'
},
I want to configure it so when I change a js
or scss
file it automatically run webpack again.
I tried to use webpack-dev-server
with the command:
webpack-dev-server --hot
It looks like it is building again, it outputs the message
webpack: bundle is now VALID.
However if I try to refresh the browser I can't see my changes. (Cache disabled on dev tools and Ctrl+F5 pressed). Restarting node also doesn't work, it is as if it had build somewhere else and not on the configured output file:
output: {
path: path.join(__dirname, '/public'),
filename: 'bundle.js'
},
Using Watch Mode You can instruct webpack to "watch" all files within your dependency graph for changes. If one of these files is updated, the code will be recompiled so you don't have to run the full build manually. Now run npm run watch from the command line and see how webpack compiles your code.
Webpack v4+ will minify your code by default in production mode .
To solve the "Cannot find module 'webpack'" error, make sure to install webpack globally by running the npm i -g webpack command and create a symbolic link from the globally-installed package to node_modules by running the npm link webpack command. Copied! Once you run the two commands, the error should be resolved.
Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API.
If you want webpack to watch for changes simply use the watch
flag:
webpack --watch
With this option webpack will watch for changes and re-compile.
webpack
normally rebuild automatically. In my case it was working fine but suddenly it stopped and the solution was to increase the value into file /proc/sys/fs/inotify/max_user_watches
according to the documentation: https://webpack.js.org/configuration/watch/#not-enough-watchers
run the following command:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
I'm not a webpack expert but what I found is that webpack-dev-server doesn't write to disk. It serves the result from memory through the Express instance it uses to serve the files.
You can test this by changing the path to localhost:8080/webpack-dev-server/bundle.js in your html file and if you run webpack-dev server the changes should show up.
The proper way to handle it is to update your webpack.config.js with a publicPath property:
output: {
path: path.join(__dirname, '/public'),
filename: 'bundle.js',
publicPath: "/public/"
}
In this case the script tag should refer to src="public/bundle.js".
More info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With