For some reason, the solutions above didn't work for me in my TypeScript project, what worked was configuring webpack production and development mode options.
Step 1
Install webpack merge (This allows you to have a similar configuration)
npm install --save-dev webpack-merge
Step 2.
Create webpack.dev.js and webpack.prod.js files in the same directory as your webpack.config.js.
touch webpack.dev.js webpack.prod.js
Step 3.
Paste this code inside your webpack.dev.js file:
// webpack.dev.js const { merge } = require("webpack-merge"); const config = require("./webpack.config.js"); module.exports = merge(config, { mode: "development", devtool: "inline-source-map", });
- Paste the same code in the
webpack.prod.js file but change the mode to 'production' and delete the devtool key.
// webpack.prod.js const { merge } = require("webpack-merge"); const config = require("./webpack.config.js"); module.exports = merge(config, { mode: "production", });
Step 4.
Update your package.json file to use the new configuration files:
// package.json "scripts": { "dev": "webpack --watch --config webpack.dev.js", "build": "webpack --watch --config webpack.prod.js" },
Now simply execute npm run dev to see your changes
With this solution, you don't need to run build for every change and node_modules is excluded from the compiler.
You can read more about this solution webpack production mode