问题描述
我正在使用webpack迈出第一步……到目前为止还不是很成功.在我的.css文件中,有一个用url定义的图像背景:
I am making my first steps in using webpack...so far not very successful.In my .css file, there is a image background defined with a url:
background-image: url("patterns/header-profile.png");
该应用在启动时会出现以下错误:
The app spectacularly craches on launch with this error:
ERROR in ./~/css-loader?"modules":true,"sourceMap":true,"importLoaders":1,"localIdentName":"[local]__[hash:base64:5]"}!./~/postcss-loader!./src/assets/css/in
spinia.css
Module not found: Error: Can't resolve 'patterns/header-profile.png' in 'C:\---- absolute path ---\src\assets\css'
首先这是我的项目结构:
First of all this is my project structure:
./src/
--> assets
--> patterns
--> header-profile.png
--> mystyle.css // <-- sos: this is where background img is defined
--> index.html
--> index.tsx
第二个是我的webpack.config.json:
Second this is my webpack.config.json:
var webpack = require('webpack');
var path = require('path');
// variables
var isProduction = process.argv.indexOf('-p') >= 0;
var sourcePath = path.join(__dirname, './src');
var outPath = path.join(__dirname, './dist');
// plugins
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
context: sourcePath,
entry: {
main: './index.tsx',
vendor: [
'react',
'react-dom',
'react-redux',
'react-router',
'react-router-redux',
'redux'
]
},
output: {
path: outPath,
publicPath: './',
filename: 'bundle.js',
},
target: 'web',
resolve: {
extensions: ['.js', '.ts', '.tsx'],
// Fix webpack's default behavior to not load packages with jsnext:main module
// https://github.com/Microsoft/TypeScript/issues/11677
mainFields: ['main']
},
module: {
loaders: [
// .ts, .tsx
{
test: /\.tsx?$/,
loader: isProduction
? 'awesome-typescript-loader?module=es6'
: [
'react-hot-loader',
'awesome-typescript-loader'
]
},
// css
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
publicPath: './',
loader: [
{
loader: 'css-loader',
query: {
modules: true,
sourceMap: !isProduction,
importLoaders: 1,
localIdentName: '[local]__[hash:base64:5]'
}
},
{
loader: 'postcss-loader'
}
]
})
},
// static assets
{ test: /\.html$/, loader: 'html-loader' },
{ test: /\.png$/, loader: "url-loader?limit=10000" },
{ test: /\.jpg$/, loader: 'file-loader' },
],
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
context: sourcePath,
postcss: [
require('postcss-import')({ addDependencyTo: webpack }),
require('postcss-url')(),
require('postcss-cssnext')(),
require('postcss-reporter')(),
require('postcss-browser-reporter')({ disabled: isProduction }),
]
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor.bundle.js',
minChunks: Infinity
}),
new webpack.optimize.AggressiveMergingPlugin(),
new ExtractTextPlugin({
filename: 'styles.css',
disable: !isProduction
}),
new HtmlWebpackPlugin({
template: 'index.html'
})
],
devServer: {
contentBase: sourcePath,
hot: true,
stats: {
warnings: false
},
},
node: {
// workaround for webpack-dev-server issue
// https://github.com/webpack/webpack-dev-server/issues/60#issuecomment-103411179
fs: 'empty',
net: 'empty'
}
};
最后这就是我在索引js文件中要求mystyle.css
的方式:
And finally this is the way I require mystyle.css
in the index js file:
require('/assets/css/inspinia.css');
我环顾了stackoverflow,github和google.我看到其他人也遇到了类似的问题.但是我不理解他们应用的各种解决方案(我是webpack的新手).
I have looked around stackoverflow, github and google. I saw that other people have faced similar problems. But I don't understand the various solutions they applied (I am new to webpack).
任何帮助将不胜感激.
推荐答案
这是我解决此问题的方法,但并不真正理解它解决了我的问题的原因(只是更改属性...).
This is the way I fixed this problem without really understanding why it solved my problem (just changing properties around...).
首先在我的webpack.config.json文件中,确保此publicPath是绝对('/')而不是相对('./')
First of all on my webpack.config.json file I made sure that this publicPath is absolute ('/') and not relative ('./')
{
output:{
publicPath: '/'
}
}
第二,在需要mystyle.css的index.tsx中,我确保它是相对路径:
Second, in my index.tsx where I require mystyle.css, I made sure that it is relative path:
require('./assets/css/mystyle.css')
最后,在mystyle.css中引用图像的位置,我确保它是绝对路径(... crazy ...).因此,例如,我将拥有:
And finally inside mystyle.css where I reference my images, I made sure that it is absolute path (...crazy...). So for example, I would have:
background: url("/patterns/header-profile.png")
它神奇地起作用了!
有人可以解释一下此配置背后的逻辑是什么,这将帮助我理解webpack的工作原理,尤其是涉及CSS文件,图像资源和绝对/相对路径时.
Could someone explain what is logic behind this configuration, it would help me a long way to understand how webpack works especially when it comes to css files, images resources and absolute/relative paths.
这篇关于webpack css-loader:无法在css文件中加载引用为url('path')的png文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!