Skip to content

resolve.alias behaves differently in dev vs prod #20622

@Squidgical

Description

@Squidgical

Describe the bug

Consider this vite config;

import { defineConfig } from "vite";
import path from "node:path";

export default defineConfig({
  resolve: {
    alias: [
      {
        find: "$okay:/",
        replacement: path.resolve(__dirname, "src"),
      },
      {
        find: "broke:/",
        replacement: path.resolve(__dirname, "src"),
      },
    ],
  },
});

When running vite dev, the alias broke:/ is not transformed and gets caught by the platform module loader.

When running vite preview, both aliases are transformed and point to the src directory.

The way these aliases are used is $okay://*, broke://*. Apparently a $ prefix is not valid as part of a protocol, so that one gets passed on as expected in dev, however because broke is valid the module loader in the browser/node tries to load it, and then throws when it realises it can't handle that protocol.

Evidently the broken example ought to work in production, because it does work in production. Imo it also ought to work in development.

Discovered this when I had the thought "hey, I could import my resources from res://* that could be fun" and tried it out.

Reproduction

https://github.com/Squidgical/vite-issue_resolve.alias

Steps to reproduce

Steps are in the reproduction repo readme.

  • run the dev server, observe
  • run the preview server, observe

System Info

System:
    OS: Linux 6.12 Pop!_OS 22.04 LTS
    CPU: (16) x64 AMD Ryzen 9 7940HS w/ Radeon 780M Graphics
    Memory: 13.90 GB / 27.21 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.18.0 - ~/.nvm/versions/node/v22.18.0/bin/node
    npm: 10.9.3 - ~/.nvm/versions/node/v22.18.0/bin/npm
    pnpm: 10.13.1 - ~/.local/share/pnpm/pnpm
    bun: 1.2.16 - ~/.bun/bin/bun
  npmPackages:
    vite: ^7.1.2 => 7.1.2

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions