question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS imported to JS using ?inline contain JS code for URL handling instead of actual URLs when built for production

See original GitHub issue

Describe the bug

One of our project which works fine with Vite 2.x has code like

import previewHacks from './preview-hacks.css?inline';

injectGlobalCss(previewHacks.toString());

where preview-hacks.css contains

@import url('line-awesome/dist/line-awesome/css/line-awesome.min.css');

The line-awesome.min.css file contains

@font-face{
  font-family:'Line Awesome Brands';
  font-style:normal;
  font-weight:400;
  font-display:auto;
  src:url(../fonts/la-brands-400.eot);
  src:url(../fonts/la-brands-400.eot?#iefix) format("embedded-opentype"),url(../fonts/la-brands-400.woff2) format("woff2"),url(../fo
 }

With Vite 3.0.0-3.0.7 the CSS string previewHacks.toString() contains in dev mode

@font-face {
  font-family: 'Line Awesome Brands';
  font-style: normal;
  font-weight: normal;
  font-display: auto;
  src: url("/VAADIN/@fs/path-to-project/node_modules/line-awesome/dist/line-awesome/fonts/la-brands-400.eot");

but when built for production it contains

@font-face{font-family:Line Awesome Brands;font-style:normal;font-weight:400;font-display:auto;src:url("+new URL('la-brands-400.c0e32387.eot', import.meta.url).href+");

Reproduction

Reproduces in a private Vaadin project. So far I have been unable to trigger it in a simple project. @patak-dev has access to the project though

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 8.07 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 17.9.0 - ~/.nvm/versions/node/v17.9.0/bin/node
    npm: 8.5.5 - ~/.nvm/versions/node/v17.9.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Chrome Canary: 106.0.5241.0
    Firefox: 102.0
    Safari: 15.5
  npmPackages:
    vite: ^3.0.7 => 3.0.7

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Aug 16, 2022

I think this will break if we inject new URL(foo) without wrapping with unsafeCSS. (stackblitz)

If you must use an expression in a css literal that is not itself a css literal, and you are confident that the expression is from a fully trusted source such as a constant defined in your own code, then you can wrap the expression with the unsafeCSS function: lit docs

1reaction
Artur-commented, Aug 16, 2022

Well… I created that PR 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

css-loader | webpack - JS.ORG
The css-loader interprets @import and url() like import/require() and will resolve them. Getting Started. Warning. To use css-loader, webpack@5 is required. To ...
Read more >
Webpack style-loader / css-loader: url() path resolution not ...
My problem was that all the url() rules (including fonts and images) were being loaded by css-loader as [object Module], and they where...
Read more >
Features | Vite
Vite uses esbuild to transpile TypeScript into JavaScript which is about 20~30x ... In addition, all CSS url() references, even if the imported...
Read more >
Styling - Remix
You can also import CSS files directly into your modules and Remix will: ... prefer your stylesheets had a URL instead of being...
Read more >
import - CSS: Cascading Style Sheets - MDN Web Docs
These conditional imports specify comma-separated media queries after the URL. In the absence of any media query, the import is unconditional.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found