Skip to content

Using build.commonjsOptions.dynamicRequireTargets with glob throws an Error #19727

@artur-ptaszek-mck

Description

@artur-ptaszek-mck

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:

  1. Quick fix: where we use new fdir() add .withGlobFunction(picomatch)
  2. 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.3

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)regressionThe issue only appears after a new release

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions