Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sharedDuringBuild: true doesn't work in some cases #18225

Closed
7 tasks done
hi-ogawa opened this issue Sep 29, 2024 · 0 comments · Fixed by #18351
Closed
7 tasks done

sharedDuringBuild: true doesn't work in some cases #18225

hi-ogawa opened this issue Sep 29, 2024 · 0 comments · Fixed by #18351
Labels
feat: environment API Vite Environment API p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Sep 29, 2024

Describe the bug

This is probably an edge case. It looks like sharedDuringBuild: true doesn't function when there are differences in plugin indices since the logic depends on matching plugin.name at the same index:

for (let i = 0; i < environmentPlugins.length; i++) {
const environmentPlugin = environmentPlugins[i]
const sharedPlugin = config.plugins[i]
if (
config.builder.sharedPlugins ||
environmentPlugin.sharedDuringBuild
) {
if (environmentPlugin.name !== sharedPlugin.name) {
validMixedPlugins = false
break
}
environmentPlugins[i] = sharedPlugin
}
}

For example, this can happen when a custom environment has build.modulePreload: false as it offsets plugin list by:

modulePreload !== false && modulePreload.polyfill
? modulePreloadPolyfillPlugin(config)
: null,

I think most of the case explicitly setting modulePreload: false isn't necessary, but this kind of pattern exists in other places?

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vite-environment-sharedDuringBuild

Steps to reproduce

This reproduction initializes sharedValue in a config file, which is then accessed inside a plugin hook. Adding a console.log, it shows something like this:

// during config loading
[in config loading] { sharedValue: '0agdwk0hf1a' }
[in config loading] { sharedValue: 'el3nuodbd05' }
[in config loading] { sharedValue: 't0dzs2llqoh' }

// during client build
[in plugin.writeBundle] { sharedValue: '0agdwk0hf1a' }

// during worker build
[in plugin.writeBundle] { sharedValue: 't0dzs2llqoh' }   // <--- this should match the first one 0agdwk0hf1a

System Info

(stackblitz) 

  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.0.0-beta.1 => 6.0.0-beta.1

Used Package Manager

npm

Logs

No response

Validations

@github-staff github-staff deleted a comment Sep 29, 2024
@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: environment API Vite Environment API labels Sep 30, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: environment API Vite Environment API p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants