-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
Describe the bug
Basically using any option in build.commonjsOptions.dynamicRequireTargets in vite.config.mjs results in an error.
error during build:
Error: Please specify a glob function to use glob matching.
at Builder.globWithOptions (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:3604:19)
at Builder.glob (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:3598:21)
at getDynamicRequireModules (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:5303:8)
at commonjs (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:7197:48)
at Object.eval (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:51290:36)
at resolveEnvironmentPlugins (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:12997:36)
at async BuildEnvironment.init (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:51994:21)
at async setupEnvironment (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:52038:5)
at async createBuilder (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/chunks/dep-DDxXL6bt.js:52042:5)
at async CAC.eval (file:///home/projects/vitejs-vite-hoyinrgw/node_modules/vite/dist/node/cli.js:874:23)
What I found out so far that the key problem of this failure is that all sub-dependencies are included in dist/chunks/dep*.js like picomatch and fdir. These two are key to explaining that issue.
Fdir has these lines in their source code https://github.com/thecodrr/fdir/blob/master/src/builder/index.ts#L17-L24 that are failing only in Vite's bundle, because require is not a defined. Why? Vite is "type": "module", but fdir is "type": "commonjs".
There are 2 ways to solve it:
- Quick fix: where we use
new fdir()add.withGlobFunction(picomatch) - Don't bundle these dependencies in Vite's chunks
First one seems simple enough, but I think it's just a symptom of a bigger problem that some dependencies could work differently after being bundled to release package which can cause unexpected results.
With the first one I'm happy to contribute, but 2nd one seems to be much bigger that's why I wanted to open a discussion :)
Reproduction
https://stackblitz.com/edit/vitejs-vite-hoyinrgw
Steps to reproduce
Run npm install and npm run build
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 18.20.3 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.2.3 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
vite: ^6.2.2 => 6.2.3Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.