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
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'
}
};
And finally this is the way I require mystyle.css
in the index js file:
require('/assets/css/inspinia.css');
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).
Any help would be greatly appreciated.
This is the way I fixed this problem without really understanding why it solved my problem (just changing properties around...).
First of all on my webpack.config.json file I made sure that this publicPath is absolute ('/') and not relative ('./')
Second, in my index.tsx where I require mystyle.css, I made sure that it is relative path:
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:
And it magically works!
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.