[英]webpack css-loader background-image error loading
我正在研究一個Angular2 + ES2015 + Babel + Webpack項目,當我在CSS文件中包含圖像並使用css-loader
時,我遇到了這個問題,它已經花了幾個小時來解決問題,但到目前為止沒有運氣:
ERROR in ./src/app/home/home.component.css
Module build failed: SyntaxError: /path/to/project/src/app/home/images/penguin.png: Unexpected character '�' (1:0)
> 1 | �PNG
| ^
2 |
3 |
4 | IHDR��>a�gAMA��7��!�IDATx��]xTEמ�B�"�)"��� ��T?@?�iRiD�tPA��)"���$��K���K���wτ���f��M6a���OHؽ3s�3gΜ93#���>���x��}����>���x�Ƅ&���f&�o>
��ɉI�O����#S�a-a
�9�8!�W1<�)O�G�j��B�
at Parser.pp.raise (/path/to/project/node_modules/babylon/lib/parser/location.js:22:13)
at Parser.getTokenFromCode (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:561:12)
at Parser.readToken (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:180:21)
at Parser.readToken (/path/to/project/node_modules/babylon/lib/plugins/flow.js:164:22)
at Parser.nextToken (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:169:21)
at Parser.parse (/path/to/project/node_modules/babylon/lib/parser/index.js:128:12)
at parse (/path/to/project/node_modules/babylon/lib/index.js:47:47)
at File.parse (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:517:34)
at File.parseCode (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:603:20)
at /path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:49:12
at File.wrap (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:563:16)
at Pipeline.transform (/path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
at Object.transformFileSync (/path/to/project/node_modules/babel-core/lib/api/node.js:144:10)
at compile (/path/to/project/node_modules/babel-register/lib/node.js:129:20)
at loader (/path/to/project/node_modules/babel-register/lib/node.js:158:14)
at Object.require.extensions.(anonymous function) [as .js] (/path/to/project/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/path/to/project/src/app/home/home.component.css:6:2430)
at Module._compile (module.js:409:26)
at Object.loaderContext.exec (/path/to/project/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
at Object.module.exports (/path/to/project/node_modules/to-string-loader/src/to-string.js:6:54)
如果你有一個搜索錯誤,你會發現github問題和stackoverflow上的類似情況,但不完全相同。 在所有這些中都指出了缺少圖像加載器(png文件在這里),但在我的情況下,我有一個完美的工作。 如果直接在JS文件上require('../image/penguin.png')
,它就會被加載並導出到輸出路徑!
我的進程和Webpack配置非常復雜,大而且分成不同的文件,但這是有趣的部分
import config from './config';
{
[...]
module: {
preLoaders: [
{
test: /\.js$/,
exclude: [/node_modules/],
loader: 'eslint'
},
{
test: /\.css$/,
exclude: [/node_modules/],
loader: 'postcss?pack=lint'
}
],
loaders: [
{
test: /\.js$/,
exclude: [/node_modules/, /\.(spec|e2e)\.js$/],
loaders: [
'babel',
'angular2-template'
]
},
{
test: /\.css$/,
exclude: [/node_modules/],
loaders: [
'to-string',
'css?-import',
'postcss?pack=process'
]
},
{
test: /\.(png|jpe?g|gif)$/,
loaders: [
'url?limit=10000&name=assets/images/[name].[ext]?[hash:20]',
'image-webpack?bypassOnDebug&optimizationLevel=6'
]
},
[...]
]
},
output: {
path: resolve(config.paths.dist),
publicPath: `http://${config.dev.host}:${config.dev.port}/`,
filename: '[name].js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js'
},
[...]
}
這里是引發錯誤的css
[...]
.post-image {
height: 150px;
background-image: url(./images/penguin.png);
background-position: center center;
background-repeat: no-repeat;
}
如果對css使用to-string-loader
看起來很奇怪,那是因為angular2-template-loader
允許將CSS直接內聯到Angular2 Componet的stylesUrl
由於錯誤堆棧,似乎一旦css-loader完成了這項工作,png文件沒有加載定義的加載器(url!image-webpack),但它加載了babel-register? 所以我從css-loader中console.log
結果,並且有預期的圖像require
:
[...]
exports.push([module.id, ".post-image{height:150px;background-image:url(" + require("./images/penguin.png") + ");background-position:50%;background-repeat:no-repeat}", ""]);
所以我讓babel忽略了png文件
{
"presets": [
"es2015",
"angular2",
"stage-3"
],
"ignore": ["*.png"]
}
這引發了一個略有不同的錯誤,因為babel忽略了圖像,但最終似乎是相同的:
ERROR in ./src/app/home/home.component.css
Module build failed: SyntaxError: Unexpected token ILLEGAL
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at loader (/path/to/project/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/path/to/project/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/path/to/project/src/app/home/home.component.css:6:2430)
at Module._compile (module.js:409:26)
at Object.loaderContext.exec (/path/to/project/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
at Object.module.exports (/path/to/project/node_modules/to-string-loader/src/to-string.js:6:54)
我沒有任何線索,我要去WTF只是在CSS文件上設置圖像背景應該很容易就像餡餅一樣
有什么想法嗎??
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.