I'm using webpack 4 and I have installed a module with a lot of subfolders, the structure would be:
/node_modules/my_main_package/what_i_need_to_include
I need to exclude the whole node_modules
except what_i_need_to_include
. Here is what I tried in my webpack.base.babel
so far:
The first approach:
module: {
rules: [
{
test: /\.js$/,
exclude: '/node_modules/',
include: '/node_modules/my_main_package/what_i_need_to_include',
use: {
loader: 'babel-loader',
options: options.babelQuery,
},
},
...
The second approach:
{
test: /\.js$/,
exclude: '/node_modules\/(?!my_main_package/what_i_need_to_include).*/'
...
}
both times unsuccessfully.
How can I get this working? What am I doing wrong?
Webpack builds a dependency graph used internally Now all modules that are used in your app are included in the dependency graph. Your project have many installed dependencies in the node_modules folder that should not be included in your client-side JavaScript production bundle.
Actually, those 'include' and 'exclude' properties are telling the loaders whether to include/exclude the files described (such as the contents of node_modules ), not webpack itself.
Webpack is a static module bundler for JavaScript applications. It takes modules, whether that's a custom file that we created or something that was installed through NPM, and converts these modules to static assets.
I have to thank Mr. The Bear, he gave me a link with a working solution, and I will answer my question myself for sharing a solution, since very few people read the comments and this may probably would be a question for others. here is the solution:
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\/(?!(my_main_package\/what_i_need_to_include)\/).*/,
use: {
loader: 'babel-loader',
options: options.babelQuery,
},
},
...
}
Hope it might be helpful.
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