Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch certain node_modules changes with webpack-dev-server

I'm currently experimenting with a monorepo architecture.

What I would like to do is in my web package where I run webpack dev server I'd like it to watch certain node_modules (symlinked local packages) for changes and trigger a "rebuild".

This way I'd be able to build dependencies separately and my browser would react to those changes.

My webpack config is the following:

var loaders = require('./../../../build/loaders-default'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var path = require('path'); var webpack = require('webpack');  module.exports = {     entry: ['./src/index.ts'],     output: {         filename: 'build.js',         path: path.join(__dirname, 'dist')     },     resolve: {         extensions: ['.ts', '.js', '.json']     },     resolveLoader: {         modules: ['node_modules']     },     devtool: 'inline-source-map',     devServer: {         proxy: [             {                 context: ['/api-v1/**', '/api-v2/**'],                 target: 'https://other-server.example.com',                 secure: false             }         ]     },     plugins: [         new HtmlWebpackPlugin({             template: './src/index.html',             inject: 'body',             hash: true         }),         new webpack.ProvidePlugin({             $: 'jquery',             jQuery: 'jquery',             'window.jQuery': 'jquery',             'window.jquery': 'jquery'         })     ],     module:{         loaders: loaders     } }; 

Loaders are just the usual stuff included.

like image 817
noherczeg Avatar asked Jan 07 '17 14:01

noherczeg


People also ask

How we can make the webpack to watch for changes?

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.

How do I watch Webpacks?

Other Ways to Enable Watch Mode You can also enable watch mode from your Webpack config file: module. exports = { mode: 'development', watch: true, // Enable watch mode entry: { app: `${__dirname}/app. js` }, target: 'web', output: { path: `${__dirname}/bin`, filename: '[name].

What is Contentbase in webpack?

Exists only in webpack-dev-server. It's only needed if you want to serve static files. For example, you want a folder of mp4 vacation movies to be available for the app, but you don't want to run them through the bundle, because that would be silly.


1 Answers

You can config in in webpack.config file or in WebpackDevServer option, to watch for changes also in node_modules (i think that by default webpack watching for changes in all files)

https://webpack.js.org/configuration/watch/#watchoptions-ignored

in the following example webpack ignored all changes in node_modules folder except specific module.

watchOptions: {   ignored: [     /node_modules([\\]+|\/)+(?!some_npm_module_name)/,      /\some_npm_module_name([\\]+|\/)node_modules/   ] } 

ignored[0] = Regex to ignore all node_modules that not started with some_npm_module_name

ignored[1] = Regex to ignore all node_modules inside some_npm_module_name

You may also used this link npm linked modules don’t find their dependencies

like image 130
Elhay Avichzer Avatar answered Sep 29 '22 09:09

Elhay Avichzer