Comments (10)
@barraq you can accomplish the same thing without any code changes to this package by just including multiple instances:
externals: [
nodeExternals(),
nodeExternals({
modulesDir: path.resolve(__dirname, '../../node_modules')
})],
from webpack-node-externals.
+1
Ideally the default modulesDir behavior would be to walk the workspace to its root (consulting all node_modules directories from package to root); thus, webpack-node-externals would work out of the box for monorepos.
There are a few issues with the current solutio of hard-coding module dirs:
- It is brittle (ie, breaks if a package is moved/copied etc within the monorepo).
- Options (eg whitelist) have to be repeated for each declaration.
- Etc.
Also, node itself seems to resolve from node_modules up the directory tree. (I see it do so when I run node scripts that require hoisted libs.) Webpack and other libraries, similarly. So this would be idiomatic, no? If so, perhaps there's a direct/normalized library/facility for doing this walk that could be leveraged.
from webpack-node-externals.
@alexrqs actually I am using this patch for our monorepo at Freeletics. I simply have this configuration:
externals: [nodeExternals({
modulesDirs: ['node_modules', path.resolve(__dirname, '../../node_modules')],
})],
and it works like a charm ;) actually that was the reason of the patch.
from webpack-node-externals.
In case somebody else is having trouble when using lerna or yarn_workspace, I've come across this answer in s.o..
It turns out, webpack-node-externals can "Read the modules from the package.json file instead of the node_modules folder".
By obtaining the dependencies via the package.json instead of depending on what's actually inside the node_modules dir, it wouldn't matter if the actual node modules are scoped out of the package when working as with a monorepo with "Yarn Workspaces" or "lerna".
Just my two cents (honest and perhaps naive question) wouldn't this be better if defaulted to true?
e.g. options.modulesFromFile (=true)
from webpack-node-externals.
In version 2, you can now specify additionalModuleDirs
in the options.
from webpack-node-externals.
I'm having the same issue so that PR witth moduleDirs will be awesome, I just don't know how @barraq resolved the issue with 2 node_modules
folders because when you are using lerna
, the folders inside packages/
will have their own node_modules
with the symlinks created by lerna
and other node packages will be on the root, that makes 2 node_modules
folder to inspect.
This is valid with workspaces only, if you don't use workspaces all is good but for my particular case where I'm using a monorepo full of react components and on the root folder a storybook installation to preview the components, if I don't use workspaces react gets repeated and doesn't work so thats why the approach with webpack-node-externals is important.
right now it kinda works with
externals: [/^[a-z\-0-9]+$/],
but is not the most optimal way as @bryzaguy says
from webpack-node-externals.
how can I whitelist
webpack/hot/poll?1000
in a monorepo setup?
Try:
externals: [
nodeExternals({
modulesDir: path.resolve(__dirname, './node_modules'),
whitelist: ['webpack/hot/poll?1000']
}),
nodeExternals({
modulesDir: path.resolve(__dirname, '../../node_modules'),
whitelist: ['webpack/hot/poll?1000']
})
],
from webpack-node-externals.
If you have many packages and you want to query them all, use this:
import glob from 'glob';
const externals = glob
.sync('*/node_modules/', { absolute: true, cwd: __dirname })
.map((modulesDir) => nodeExternals({ modulesDir }));
from webpack-node-externals.
hi, may I ask the current state of this issue? any update?
from webpack-node-externals.
how can I whitelist webpack/hot/poll?1000
in a monorepo setup?
from webpack-node-externals.
Related Issues (20)
- ReferenceError: requiere is not defined on gulp postcss HOT 1
- WARNING: Could not determine version of module x HOT 1
- README confusingly advises removing "target: 'node'" for Webpack 5 HOT 2
- Unsupported type in webpack 5 HOT 1
- Support for Yarn 2 PnP HOT 1
- Switch to ESM HOT 7
- version 3.0.0 - moduleFromFile fileName not working
- Undesired package being included in the bundled ouput
- Is it only relevant for node target libraries ?
- Support options.skiplist (=[])
- Do we need to set `externalsPresets` when `target: 'node'`?
- Adding a debug mode
- Option 'includeAbsolutePaths' can't work well with inline loader
- How can I bundle assets (i.e css files) from node_modules that required in node_modules? HOT 1
- importType option misses some type definitions. HOT 2
- webpack serve error: interopRequireWildcard":1 Uncaught ReferenceError: require is not defined HOT 2
- Add support for resolving modules with context within monorepos HOT 5
- change location of package.json HOT 2
- Inverse behavior?
- Noob question: After excluding modules from webpack, how do I get them into my Electron app's node_modules folder? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack-node-externals.