vite-plugin / vite-plugin-dynamic-import

Enhance Vite builtin dynamic import
https://www.npmjs.com/package/vite-plugin-dynamic-import
MIT License
193 stars 11 forks source link

Bare imports do not work in monorepos #41

Closed caghand closed 1 year ago

caghand commented 1 year ago

In monorepos, the web app is just one package (subdirectory) among many packages under the same parent directory. The Vite config file exists under the web app's subdirectory, and config.root is set to that same directory. Note that node_modules is shared among all of those packages, and resides directly under the parent directory. There is nothing under the node_modules subdirectory under the web app, unless a package version required by the web app differs from the package version required by the rest of the monorepo.

Unfortunately, this plugin looks for node_modules under the web app's subdirectory, and not under the parent directory: https://github.com/vite-plugin/vite-plugin-dynamic-import/blob/f2b57dd793366ea32a5c94a3831a6bf20358a2ac/src/resolve.ts#L92 So, it does not work in monorepos. Instead, it should go recursively up the directory tree, searching for node_modules at each level. In other words, it should match the algorithm used by require() in Node.js: https://nodejs.org/api/modules.html#all-together Another example that implements the same algorithm: https://github.com/h2non/resolve-tree

caoxiemeihao commented 1 year ago

vite-plugin-dynamic-import@1.2.5 is out!

caghand commented 1 year ago

Wow, you are so fast!! It doesn't work in some situations though. Like I mentioned above, sometimes, the web app directory has its own version of node_modules. This happens when the web app requires a package version that differs from the package version required by the rest of the monorepo. In fact, in the case of Vite, the web app directory always has its own version of node_modules, to contain the .vite scratch directory. But while your new algorithm is going recursively up the directory tree, it stops scanning as soon as it finds a valid node_modules directory. So, in the case of Vite, it gets stuck in the web app directory and never goes up to the parent directory. In most cases, only the parent directory's version of node_modules contains the packages we want. So, your algorithm should search for the desired package in each of the node_modules directories it encounters while going recursively up the directory tree. As soon as it finds the desired package, it can stop. Just searching for the existence of node_modules is not enough. :)

caoxiemeihao commented 1 year ago

vite-plugin-dynamic-import@1.2.6 is out!

caghand commented 1 year ago

It works perfectly! Thank you! Really appreciate your fast response! :)

caoxiemeihao commented 1 year ago

Do you work for Microsoft?

caghand commented 1 year ago

Yes... We are probably one of few teams at Microsoft that are migrating to Vite. And I'm hoping that there will be more Vite usage in the future. The community is awesome.

caoxiemeihao commented 1 year ago

Nice to meet you, I'm a front-end engineer form China. 👋 I hope we have a chance to work together again. :)

caghand commented 1 year ago

Nice to meet you, too! Yes, I hope so, too!