-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to exclude node_modules but one #2031
Comments
{
test: /\.js$/,
include: [/node_modules\/MY_MODULE/]
}, |
and not compiled :( my webpack config
|
Include you src directory and the other directory. Don't use exclude. include: [
path.resolve(__dirname, "src"),
path.resolve(__dirname, "node_modules/ui")
] |
@sokra |
I have the same issue, I can't include all |
I don't know if it could be the fix but in lib/LoadersList.js: LoadersList.prototype.matchObject = function matchObject(str, obj) {
if(obj.test && !this.matchPart(str, obj.test)) return false;
if(obj.include && !this.matchPart(str, obj.include)) return false;
if(obj.exclude && this.matchPart(str, obj.exclude)) return false;
return true;
}; Could it be better to have something like: - if(obj.include && !this.matchPart(str, obj.include)) return false;
+ if(obj.include && this.matchPart(str, obj.include)) return true; |
@borm: a solution: {
test: /\.js$/,
exclude: /node_modules\/(?!(MY-MODULE|ANOTHER-ONE)\/).*/,
}, |
@ghigt, oh thanks, but i just use webpack-node-externals |
reason: (1) babel-polyfill confict with other dependence lib that all compiled by webpack config (2) fix the exclude config for the react-nav-bar and other dependence lib reference: https://github.com/react-native-community/react-native-navbar#usage-with-webpack webpack/webpack#2031
dont know why but @sokra solution raised new exception exclude: /node_modules(?!\/my-es6-raw-module)/ UPD IMO exclude as a function (comments below) is better option |
- Remove the restriction on ES6 module processing from babel config (hopefully this is the right option to change) - Rather than exclude all of node_modules, just include the one module we need to process, and implicitly exclude the rest - `include` syntax based on webpack/webpack#2031 (comment)
I didn't see this option listed here, so I thought I might as well drop in my findings. I found it useful to leverage the ability to specify an {
test: /\.js$/,
exclude: function(modulePath) {
return /node_modules/.test(modulePath) &&
!/node_modules\/MY_MODULE/.test(modulePath);
}
} |
example for webpack 2 documentation
|
In Windows, exclude: /node_modules(?!(\/|\\)MY_MODULE)/ |
Linux uses "/" while Windows uses "\" in |
I finally got a node_modules package to compile with The problem was that the package had it's own
My solution is to set var readPackageJson = require('read-pkg')
var babelOptions = readPackageJson.sync().babel
babelOptions.babelrc = false
// ... rest of config
{
test: /\.js$/,
loader: 'babel-loader',
include: [path.join(__dirname, '..', 'node_modules/vue-match-media')],
options: babelOptions,
}, |
Building on @nowells suggestion above and incorporating the comment from @lxjwlt about Windows paths being different, I decided to make a function to build the necessary regexps automatically with the correct path separator:
Usage is to put the above function in your preamble, and then call it to generate the "exclude" value, e.g.:
You can add however many modules you need to exclude from exclusion to the list, although note that the test is O(n) in the number of modules, so if you have a lot of exclusions to process it may be worth finding a better way. |
thanks,It help me |
For anybody trying this on windows, it is necessary to replace node_modules/MY_MODULE with node_modules\MY_MODULE because of windows using backslashes for file paths.. anyone who has ever diagnosed a bug to being a conflict between the direction of slashes on Windows vs Unix you will feel my pain! |
Thanks !! Save my life!! |
it's work! |
@jh3141 the most elegant solution, thanks! |
|
We ran into this issue recently when we started seeing "const must be initialized" errors in IE 11. It turned out that some our dependencies, notably some of the D3 libraries, were no longer being transpiled to ES6. (That's a deliberate decision on the part of D3's maintainer, FYI.) Here's a rule that I added to our Webpack config file to transpile just the libraries affected: {
include: mPath => {
// Some npm modules no longer transpiled to ES5, which
// causes errors such as "const must be initialized" IE 11 and crash
// the build. So we need to transpile just those modules here.
const inclusionReg = /node_modules\/(.*(d3-array))|(.*(d3-delaunay))|(.*(d3-sankey))|(.*(d3-scale))/;
// On Windows, mPath use backslashes for folder separators. We need
// to convert these to forward slashes because our
// test regex, inclusionReg, contains one.
const modulePath = mPath.replace(/\\/g, "/");
if (inclusionReg.test(modulePath)) {
// Don't need to see entire path in console
const pathArray = modulePath.split(DIRNAMENIX);
console.log("TCL: transpiling module: ", pathArray[1] || modulePath);
return true;
}
return false;
},
test: /\.js$/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
}
},
],
}, I find an Note my inclusion Regex: const inclusionReg = /node_modules\/(.*(d3-array))|(.*(d3-delaunay))|(.*(d3-sankey))|(.*(d3-scale))/; This picks up the listed libraries no matter far down they're nested in node_modules; they may be in there as dependencies of dependencies, e.g.:
Finding which dependencies were causing our
You'll need to turn on Regex search in VSCode for this to work. It's the .* icon to the right of the search box. The Regex will find all occurrences of |
why does it not work for this? |
Try adding a backslash before the second to last forward slash.
|
{
test: /\.js$/,
use: ['babel-loader'],
exclude: /node_modules\/(?!(cnchar|cnchar-trad)\/).*/
}
yeat.I had changed for this,but it did not work too.
could you give me a demo in the github?
发自我的iPhone
…------------------ Original ------------------
From: James Johnson <[email protected]>
Date: Sun,Jan 3,2021 2:43 AM
To: webpack/webpack <[email protected]>
Cc: gottayan <[email protected]>, Comment <[email protected]>
Subject: Re: [webpack/webpack] How to exclude node_modules but one (#2031)
Try adding a backslash before the second to last forward slash.
exclude: /node_modules/(?!(cnchar|cnchar-trad)\/).*/,
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
the regular expression is wrong.It can't match the package path in the node_modules. |
This used to work like a charm on Webpack 4, but since migrating to Webpack 5 I get this error in the console:
I have been trying to fix it for a couple of days but I am running out of ideas now. |
Subj,
as example
I create some module in another folder ( /projects/MY_MODULE )
MY_MODULE not compiled, source code
then run npm link
after go to my project and run npm link MY_MODULE
Compile my project and have error two copies React
i`am try
The text was updated successfully, but these errors were encountered: