Giter VIP home page Giter VIP logo

Comments (10)

chadly avatar chadly commented on May 20, 2024 87

@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.

atdixon avatar atdixon commented on May 20, 2024 14

+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.

barraq avatar barraq commented on May 20, 2024 8

@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.

alfonsolamadrid avatar alfonsolamadrid commented on May 20, 2024 6

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.

liady avatar liady commented on May 20, 2024 6

In version 2, you can now specify additionalModuleDirs in the options.

from webpack-node-externals.

alexrqs avatar alexrqs commented on May 20, 2024 1

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.

shaunlmason avatar shaunlmason commented on May 20, 2024 1

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.

moshest avatar moshest commented on May 20, 2024 1

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.

sandangel avatar sandangel commented on May 20, 2024

hi, may I ask the current state of this issue? any update?

from webpack-node-externals.

sibelius avatar sibelius commented on May 20, 2024

how can I whitelist webpack/hot/poll?1000 in a monorepo setup?

from webpack-node-externals.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.