I'm trying to figure out correct approach for a javascript monorepo. Imagine monorepo containing packages / libraries:
root
- node_modules
- packages
+ lib-a
* node_modules
+ lib-b
* node_modules
Now let's say both lib-a
and lib-b
packages use webpack
as their build tool.
I see two approaches
Add wepback
as dependency to root. Include "build" script in both packages: "build": "webpack -p --config webpack.config.js
. webpack.config.js
could include root webpack.config.js
. Then I could use tool like lerna
to run the build from root directory (which means webpack
binary is recognized. However I will be unable to run the build in specific packages since webpack
is not available there. I could probably change the build script to something like "build": "../../node_modules/.bin/webpack -p --config webpack.config.js
Always include webpack
in each package. This means that build
script will succeed. This also means that each package will have the same dependency and I should probably watch that each package uses same webpack
version.
Basically what I'm getting at is how should packages inside monorepo be structured? If any package is published, should it always be possible to build
that package separately.
Your approach #2 is right. You handle each package separately as it was an individual, self-contained package.
The advantage of a monorepo lays not in sharing files through the directory structure but in:
node_modules
with flat structure, effectively deduplicating them.import
/require()
as they were external dependencies. And, thanks to symlinks to node_modules
, your "dependency" packages contain always the latest content without publishing.I know it's not so easy at the beginning, but when you dig into Lerna documentation it's becoming more clear. Besides Lerna main page I recommend reading about hoisting, FAQ and individual commands like bootstrap and publish.
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