Skip to content

__vite__mapDeps optimization breaks configuration depending on Vite bundling #17575

@skovhus

Description

@skovhus

Describe the bug

#16184 caused a regression in our codebase when upgrading to 5.3.x. We use the renderBuiltUrl to update the asset URL based on some configuration.

Vite config:

    experimental: {
      renderBuiltUrl(filename: string, { hostType }) {
        if (hostType === "js") {
          return { runtime: `window.__toStaticUrl(${JSON.stringify(filename)})` };
        } else {
          return { relative: true };
        }
      },
    },

window.__toStaticUrl is depending on some configuration and is loaded in a file we call preload.ts before our main file entry.ts. Snippet from our index.html file:

<script type="module" src="/src/vite/preload.ts"></script>
<script type="module" src="/src/vite/entry.ts"></script>

In prouduction vite bundles preload and entry into one chunk. This used to work fine, but the challenge with the change in this PR is that instead of lazy evaluating window.__toStaticUrl when building the mapDeps, we now expect the function to be defined beforehand. As the function is not defined we get a TypeError: window.__toStaticUrl is not a function.

Additional context in #16184 (comment)

Reproduction

#16184 (comment)

Steps to reproduce

No response

System Info

n/a

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p4-importantViolate documented behavior or significantly improves performance (priority)regressionThe issue only appears after a new release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions