Skip to content

Cannot use import subpaths in css url() paths #16155

Description

@IanVS

Describe the bug

I don't know whether this should work or not, but it did when using resolve config previously, it worked fine. Now that I've converted over to using subpath imports, url() calls in my css files do not resolve. I tried using Vite 5.0.x and 5.2.0-beta.0, and neither one worked.

Reproduction

https://stackblitz.com/edit/vitejs-vite-ndby5u?file=src%2FApp.css

Steps to reproduce

Notice that the react logo is not shown when using a subpath import.

System Info

N/A

Used Package Manager

pnpm

Logs

No response

Validations

Activity

  1. bolt-new-by-stackblitz commented on Mar 13, 2024

    @bolt-new-by-stackblitz
  2. swwind commented on Mar 24, 2024

    @swwind

    Defining import map in package.json does not work currently.

    Manually edit config.resolve.alias if you want to map @assets/* to ./src/assets/* in CSS. (#assets/* would not work because it was regarded as a hashtag)

    export default defineConfig({
      resolve: {
        alias: [
          {
            find: /^@assets\//,
            replacement: url.fileURLToPath(new URL('./src/assets/', import.meta.url))
          }
        ]
      }
    });
  3. sapphi-red commented on Mar 29, 2024

    @sapphi-red
    Member

    Simply resolving urls starting from # will break things like mask: url(#star).
    https://stackblitz.com/edit/vitejs-vite-tt4mgn?file=src%2FApp.css
    If we are going to make this work, we'll need to think about how not to break codes like above.

  4. added 2 commits that reference this issue on Mar 24, 2026
    d12ed15
    28f24af
  5. kalvenschraut commented on Apr 20, 2026

    @kalvenschraut
    Contributor

    https://github.com/vuejs/core/pull/13045/changes

    vue implemented this for attributes with their own checks by what I think is seeing if the path resolves to a file then use it instead of as a css selector. Which is similar to my idea in #22014 not sure @sapphi-red if there can be any traction on this. I can work around it for now by adding an alias for each of my monorepo subpackages that have static assets just not ideal to have to maintain. Only thing that doesnt work nice with subpath imports at least for my codebase.

  6. sapphi-red commented on Jun 24, 2026

    @sapphi-red
    Member

    We discussed this issue in our last team meeting, and we're a bit concerned about introducing Vite-specific behavior here. We'd first like to understand how other bundlers handle this.

  7. added
    p2-to-be-discussed
    and removed on Jun 24, 2026
  8. Rich-Harris commented on Aug 17, 2026

    @Rich-Harris
    Contributor

    I would argue that Vite-specific behaviour for url(...) already exists — certain values are treated as asset imports while others are left untouched, and the distinction is implicit. Given this input...

    .foo {
      background-image: url(./path/to/my/stuff/exists.png);
    }
    
    .bar {
      background-image: url(alias/exists.png);
    }
    
    .baz {
      background-image: url(./path/to/my/stuff/missing.png);
    }

    ...vite build will generate this output (albeit minified):

    .foo, .bar {
      background-image: url(/assets/exists-CLDdwZDr.png);
    }
    
    .baz {
      background-image: url(./path/to/my/stuff/missing.png);
    }

    The test isn't 'does this value already have a meaning?', it's 'can we resolve this value to a file in the project?'. So it feels inconsistent not to try and resolve subpath imports — yes, #x/y.png could theoretically be a fragment, in the same way that ./path/to/my/stuff/exists.png could theoretically refer to a path that's relative to the running app rather than the CSS file, but in practice it's vanishingly unlikely.

    So as long as it's restricted to valid subpath imports and not anything with a leading #, resolving seems like the correct move.

  9. moved this from Discussing to P2 - 5 in Team Boardon Aug 18, 2026
  10. moved this from P2 - 5 to Approved in Team Boardon Aug 19, 2026
  11. added this to the 8.4 milestone on Oct 1, 2026
  12. added
    p3-downstream-blocker
    and removed
    p2-to-be-discussed
    on Oct 19, 2026
  13. locked and limited conversation to collaborators on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: cssp3-downstream-blocker

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions