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.

Not compatible with Webpack file and url loaders?

See original GitHub issue

Do you want to request a feature or report a bug? Bug.

What is the current behavior? Webpack’s file-loader or url-loader (they come standard with create-react-app) let you require() (or import) any file type (like images and fonts). At build time, the require statement gets replaced with a URL that you can use to load the file at runtime. Unfortunately Linaria seems to break any time I reference anything that gets loaded this way. It appears to attempt to load the file as if it were a JS module. E.g. both of these fail:

css`background-image: url(`require('image.svg')`);`
css`@font-face { font-family: Test; src: url(${require('webfont.woff')})}`;

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test. Quick create-react-app that attempts to load an SVG and a web font on the home page: https://github.com/steadicat/linaria-test

What is the expected behavior? Expected behavior is for Linaria to let the loader run first so that those requires become plains strings to be included in the CSS.

Please provide your exact Babel configuration and mention your Linaria, Node, Yarn/npm version and operating system. See app above.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:24 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
steadicatcommented, Nov 2, 2017

I think I just stumbled upon the answer to the “why it doesn’t work” question. Here’s what the raw JS looks like after url-loader does its thing:

/***/ "./src/logo.svg":
/*!**********************!*\
  !*** ./src/logo.svg ***!
  \**********************/
/*! no static exports found */
/*! all exports used */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__.p + "static/media/logo.5d5d9eef.svg";

/***/ }),

/* [...SNIP...] */

var logo = __webpack_require__(/*! ./logo.svg */ "./src/logo.svg");

It appears that url-loader does not replace the require statement with a string, it actually creates a dummy module that exports a string. I don’t see how Linaria can work around that. 😞

0reactions
steadicatcommented, Nov 14, 2017

The solution of using plain CSS url() works for me. There are two actionable things:

  1. Document this more clearly. I saw a mention of this at the bottom of this file. Would be great if this was in the README.
  2. Fix the paths. Currently url()s referenced inside Linaria CSS are relative to the location of the Linaria generated CSS, which is awkward. It would be great if the paths got rewritten to be relative to the current file.
Read more comments on GitHub >

github_iconTop Results From Across the Web

url-loader | webpack - JS.ORG
A loader for webpack which transforms files into base64 URIs. Getting Started. To begin, you'll need to install url-loader : $ npm install...
Read more >
Webpack url-loader or file-loader not working react app
Images are not loading in the browser using Webpack 4 with either of url-loader, or file-loader. Small images are not in data url(or...
Read more >
Load images and fonts with Webpack file loader like a pro
Webpack file -loader is a loader used mainly for supporting images such as SVG and PNG, and fonts in your webpack project. If...
Read more >
sass-loader - npm
Webpack needs to distinguish between bootstrap and ~bootstrap because CSS and Sass files have no special syntax for importing relative files ...
Read more >
Webpack 5: Asset Modules - Medium
The core concept of Webpack is its loaders. You need loaders to load your styles, fonts, images, files etc. If you use Create-React-App...
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