Bug: When using npm workspaces, it fails with "Error: modules: <package-name> package.json file does not exist"
If using a mono repo and npm workspaces, then vite-plugin-cdn-import is not resolving the location of each package properly, as many packages will be deduped into the root node_modules folder and are not relative to each workspace folder.
Example project: https://stackblitz.com/edit/vitejs-vite-ueqe85na?file=vue-app-1%2Fvite.config.js
Run the following command:
npm run build
This will yield:
failed to load config from /home/projects/vitejs-vite-ueqe85na/vue-app-1/vite.config.js
error during build:
Error: modules: vue package.json file does not exist
at eval (file:///home/projects/vitejs-vite-ueqe85na/node_modules/vite-plugin-cdn-import/dist/index.js:202:13)
at async resolveConfig (file:///home/projects/vitejs-vite-ueqe85na/node_modules/vite/dist/node/chunks/dep-M1IYMR16.js:53535:24)
at async createBuilder (file:///home/projects/vitejs-vite-ueqe85na/node_modules/vite/dist/node/chunks/dep-M1IYMR16.js:51597:18)
at async CAC.eval (file:///home/projects/vitejs-vite-ueqe85na/node_modules/vite/dist/node/cli.js:867:23)
npm ERR! Lifecycle script `build` failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: [email protected]
npm ERR! at location: /home/projects/vitejs-vite-ueqe85na/vue-app-1
Root cause is this approach to find the module path, which is too simple: https://github.com/MMF-FE/vite-plugin-cdn-import/blob/5c7a3c06178f8b70693ff30279b85ef7a6fb39ef/src/index.ts#L19
Instead, use the official module resolution algorithm, e.g., by calling import.meta.resolve(moduleName):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve