I'm using a node library that has a css file inside, that css file loads an image like this
background-image: url("image.png");
The file "image.png" is being requested at http://localhost:9000/ (the root) is dirty to have images all over the root of the project.
Also I have another problem, I don't know why the image file of the module is not being copied to the build folder automatically, only works if I copy the image file manually to the build folder. Playing with Webpack config parameters I got working the feature to copy the images to root, but it puts random names to them and I do not want them in root.
I want all the images loaded to be copied to a "img" subfolder inside the build folder with thier original file names, and to be requested there, so the root is not dirty with all the files there.
This is my webpack config file:
var webpack = require('webpack');
var WebpackNotifierPlugin = require('webpack-notifier');
"use strict";
module.exports = {
entry: "./source/typescript/Main.ts",
output: {
filename: "./js/bundle.js"
},
devtool: "inline-source-map",
devServer: {
contentBase: ".",
host: "localhost",
port: 9000,
open: true
},
module: {
rules: [
{
test:/\.(s*)css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
url: false
}
},
{
loader: "sass-loader"
}
]
},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
enforce: 'pre',
test: /\.js$/,
loader: "source-map-loader"
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|otf|woff|woff2|json|xml|ico)$/,
use: [{loader: 'file-loader'}]
},
{
test: /\.(csv|tsv)$/,
use: [{ loader: 'csv-loader' }]
},
{
test: /\.exec\.js$/,
use: [{ loader: 'script-loader' }]
},
{
test: require.resolve('jquery'),
use:
[
{
loader: 'expose-loader',
options: 'jQuery'
},
{
loader: 'expose-loader',
options: '$'
}
]
}
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ]
},
plugins: [
new WebpackNotifierPlugin({excludeWarnings: true}),
//new webpack.optimize.UglifyJsPlugin({compress: {warnings: false}}) // Uncoment to minify bundle
]
};
url-loader?url-loadersorryurl-loaderis likefile-loaderbut if the file is small enough, it can encode and inline it. So you don't have path problem anymore. For the path, problem, you can try to add anpathkey in yourouput: webpack.js.org/configuration/output/#output-path