Skip to content

[Bug]: Error using url(...) in css file #641

@alefduarte

Description

@alefduarte

Version

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
    Memory: 2.82 GB / 31.67 GB
  Browsers:
    Edge: Chromium (127.0.2651.86)
    Internet Explorer: 11.0.19041.4355

Details

I have a CSS file with my local font

@font-face {
  font-family: 'Gilroy';
  font-style: normal;
  font-weight: 300;
  src: url('../assets/fonts/Gilroy-Light.otf');
}

When I build it, I get the error

File: C:\xxx\packages\ui\dist\context\context.css:1:1
  × Module not found: Can't resolve '/static/font/Gilroy-Light.otf' in 'C:\xxx\packages\ui\dist\context'
   ╭─[5:36]
 3 │ import ___CSS_LOADER_API_IMPORT___ from "../../../../node_modules/@rsbuild/core/compiled/css-loader/api.js";
 4 │ import ___CSS_LOADER_GET_URL_IMPORT___ from "../../../../node_modules/@rsbuild/core/compiled/css-loader/getUrl.js";
 5 │ var ___CSS_LOADER_URL_IMPORT_0___ = new URL("/static/font/Gilroy-Light.otf", import.meta.url);
   ╰────

If I check the built CSS in the dist folder, it changes the file to

@font-face {
  font-family: Gilroy;
  font-style: normal;
  font-weight: 300;
  src: url(/static/font/Gilroy-Light.otf);
}

If I manually change the src to

  src: URL(../static/font/Gilroy-Light.otf);

then it works.
I also tried to use

tools: {
    cssLoader: {
      url: false,
    },
  },

but then the fonts are not exported.

I tried an example using codesandbox and in this example, only by using URL(...) I got an error:

error   Compile error: 
Failed to compile, check the errors for troubleshooting.
File: /project/workspace/packages/ui/src/styles/fonts.css?__rslib_entry__:1:1
  × Module build failed:
  ╰─▶   × TypeError: Cannot read properties of undefined (reading '__esModule')
        │     at handleExports (file:///project/workspace/node_modules/.pnpm/@[email protected][email protected]/node_modules/@rslib/core/dist/libCssExtractLoader.js:34:45)
        │     at file:///project/workspace/node_modules/.pnpm/@[email protected][email protected]/node_modules/@rslib/core/dist/libCssExtractLoader.js:121:9
        │     at /project/workspace/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@rspack/core/dist/index.js:6301:13

if I try url: false it builds fine but the font file is not exported.

Reproduce link

https://codesandbox.io/p/devbox/sx9t7q

Reproduce Steps

  1. run pnpm i to install all dependencies
  2. run pnpm build --filter=@acme/ui to build only the ui package.

You'll see the error already

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions