Closed maissani closed 7 years ago
I have almost the exact problem when using 'to-string-loader', 'css-loader'
in webpack.
Looks like this is a problem with all (most) files css loads using url
..
I am using rc.4.
I'm also experiencing this issue trying to build a project containing a component style CSS file that specifies a background url:
background: url('../assets/img/my-image.jpg') no-repeat;
Not sure if this is relevant, but I've seen variations of the error line based on different images.
JPG encoded on Photoshop 3.0 (I guess?):
(function (exports, require, module, __filename, __dirname) { ����2�Photoshop 3.0
JPG encoded on my Windows box:
(function (exports, require, module, __filename, __dirname) { ����
PNG encoded on my Windows box:
(function (exports, require, module, __filename, __dirname) { �PNG
It looks like the image is trying to be parsed as text. It looks like webpack isn't configured to handle jpg files as a part of its build. (similar issue for css-loader here: https://github.com/webpack/css-loader/issues/1)
I'm looking for a solution, but I'm no WebPack expert - if anyone has any guidance, more than appreciated.
Without inspect more , i can say that url declaration in css files seems to be parsed as a javacript function while it's not.
+1
+1
+1
👍
Github have something called reactions, instead of +1
or thumbsupping issues. Please use it! See https://github.com/blog/2119-add-reactions-to-pull-requests-issues-and-comments
As default, github sends out mail updates to every partipants on new comments.
If you want to be one of them, just subscribe
(the bottom to the right)!
You need to teach webpack how to handle this file type (*.jpg
). Otherwise it's assumed to be a js module.
loaders: [
//...
{
test: /\.(jpg|png)$/,
loader: 'file',
}
]
This uses the webpack/file-loader that copies the referenced assets (the jpg) to the output folder and leaves behind an URL for the browser to load.
I invite you to browse the (long) list of loaders: http://webpack.github.io/docs/list-of-loaders.html
It's quite interesting to see what other loaders are available, like the url-loader
that turns the file contents into data:
urls.
headbang
Thanks @Tragetaschen... You know what? I tried the file-loader and it failed because I set my loader to file-loader
, with the '-loader' suffix like all the other loaders. I didn't notice it's actually just file
.
Have just confirmed that this solves the issue:.
I've submitted a pull request https://github.com/AngularClass/angular2-webpack-starter/pull/830 that adds the loader to webpack.common.js
.
@Martaver Did you miss svg format in your pull request ?
I didn't think of it to be honest. I can add it and send another PR easily enough.
Any other formats you think would be good to add and handle as files?
On Mon, Aug 8, 2016 at 1:00 PM Mehdi Aïssani notifications@github.com wrote:
@Martaver https://github.com/Martaver Did you miss svg format in your pull request ?
— You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/AngularClass/angular2-webpack-starter/issues/759#issuecomment-238192974, or mute the thread https://github.com/notifications/unsubscribe-auth/AF1wMrDHABPuDBgw9n-jQRVBvw1568V_ks5qdv4tgaJpZM4JGHuo .
Just to get this right; after adding the file
, loader, css
files with entries like background: url('img/timeline/delete.png') no-repeat center;
should not give a message like this;
ERROR in ./~/vis/dist/vis.css Module build failed: /.../node_modules/vis/dist/img/timeline/delete.png:1 (function (exports, require, module, filename, dirname) { �PNG ^ SyntaxError: Unexpected token ILLEGAL
right?
I added the file loader as works above, but I am still getting this error.. Anyone else?
Looks like ur loader is being ignored. How did u add it, can u show us the code?
Also, check out the associated PR for a working example. On Sat, 13 Aug 2016 at 02:26, Lars Solberg notifications@github.com wrote:
Just to get this right; after adding the file, loader, css files with entries like background: url('img/timeline/delete.png') no-repeat center; should not give a message like this;
ERROR in ./~/vis/dist/vis.css Module build failed: /.../node_modules/vis/dist/img/timeline/delete.png:1 (function (exports, require, module, filename, dirname) { �PNG ^ SyntaxError: Unexpected token ILLEGAL
right?
I added the file loader as works above, but I am still getting this error.. Anyone else?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/AngularClass/angular2-webpack-starter/issues/759#issuecomment-239583254, or mute the thread https://github.com/notifications/unsubscribe-auth/AF1wMivWcJe_NJHqa4FtAuKHw1Fp4wMPks5qfQE1gaJpZM4JGHuo .
I have it as the PR, example; Relevant part being..
{
test: /\.(jpg|png|gif)$/,
loader: 'file',
},
{
test: /\.css$/,
loaders: ['to-string-loader', 'css-loader'],
},
If I change the css rule to;
{
test: /\.css$/,
loader: 'raw-loader'
},
the error is gone. So at least that shows that it uses my rules. I'm still learning a lot about webpack, so there might be something I am missing. But it is mostly based on angular2-webpack-starter. There are no other rules that collides. Ideas? The error is happening using the vis
library.
Another observation is that I did not get any errors if I changed the loader to eg 'file123'
, so it for sure looks like the rule never hits. Do I need to learn webpack to handle url()
in the css files?
Was looking into your issue, @xeor, and found that my fix, doesn't actually seem to fix anything.
Near as I can tell, the issue went away when I switched to using root-relative background-image url(...)... with some mixup I attributed this to using the file-loader, but so far, it seems that even if I delete these loaders it works, as long as I'm using root-relative URLs.
This relates to css-loader's root relative URLs feature.
The original issue, then, is still unsolved.
A quick workaround, I guess, is to just use root-relative urls, such as: background-image url(/assets/img/myimg.jpeg)
http://stackoverflow.com/questions/37402003/webpack-url-and-file-loaders-dont-working-on-angular-2-required-component-style According to this page, exports-loader can solve this problem:
{
test: /\.css$/,
loaders: ['exports-loader?module.exports.toString()', 'css-loader']
}
This is also happening to. I tried different loader for this. and finally I think there is something wrong with to-string-loader. (raw, style and so on..) When I check the issues of to-string-loader, I got this: https://github.com/gajus/to-string-loader/issues/2
looks like this is related: https://github.com/webpack/style-loader/issues/55
@Martaver thx! Workaround with /full/path/file.png works!
@Martaver your workaround caused the error to go away but it did not load the background image. Any help would be appreciated.
@zxx714 this did not work
@Martaver This worked for me when running a development build, but not a production build.
Following the links in this SO post, using css-to-string-loader
worked for me in the end (instead of to-string-loader
)
http://stackoverflow.com/questions/38424488/webpack-css-loader-background-image-error-loading
https://www.npmjs.com/package/css-to-string-loader
Report a Bug
The current behavior is when you try to add a bacground url into a css that is loaded by styles or styleUrl it return the following error :
The following lines triger the bug
Tested on Linux & Windows
No need to push a plunker => Just get the last angular webpack starter (with the material branch or not) and try to use one background url on any css files that are loaded by styles or styleUrl
No error and the background image displayed on and bacground url working
This break the common use of angular
I think in webpack.comon.js in css, it misss resolve-url-loader