Set the output for image-webpack-loader - npm

I'm trying to load images through the image-webpack-loader module, it works correctly, but instead of saving the image where the outputPath indicates, it saves it in the root directory of the project, does anyone know how to define the output of the images? I have read the entire documentation (webpack-image-loader) and I can not find any place where it says how to configure the output of files. Here I leave the my webpack.config.js
const autoprefixer = require('autoprefixer')
module.exports = {
entry: ['./assets/src/scss/index.scss', './assets/src/service/clanService.js', './assets/src/service/locationService',
'./assets/src/model/Clan.js', './assets/src/model/Location.js', './assets/src/model/Player.js',
'./assets/src/utils/material.js', './assets/src/utils/constants.js', './assets/src/utils/auxFunctions.js',
'./assets/src/fonts/Supercell-magic-webfont.generated.woff','./assets/src/images/Clash_Royale.png'],
output: {
filename: './dist/bundle.js',
},
module: {
rules: [
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack#1.x
disable: true, // webpack#2.x and newer
outputPath: '/dist/images/' // <-- Thats not work
},
},
],
},
{
test: /^(?!.*\.generated\.ttf$).*\.ttf$/,
use: ['css-loader', 'fontface-loader'],
}, {
test: /\.generated.(ttf|eot|woff|woff2)$/,
use: [{
loader: 'file-loader',
options: {
outputPath: '/dist/fonts/',
},
}],
},
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: './dist/bundle.css',
},
},
{loader: 'extract-loader'},
{loader: 'css-loader'},
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer()],
},
},
{
loader: 'sass-loader',
options: {
includePaths: ['./node_modules'],
},
}
],
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015'],
plugins: ['transform-object-assign']
},
}
],
},
}
As you can see I also have to enter the files one by one at the entry point, is it possible to add whole directories?

Answering a pretty old post here but I had the same issue today.
At least it may help someone else having the same problem.
Setting the outputPath for file-loader prior to image-webpack-loader solved it for me.
{
test: /\.(gif|png|jpe?g|svg)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: '/dist/images/'
}
},
{
loader: 'image-webpack-loader',
options: { ... }
}
]
}

Related

Debugging in Chrome Sources tab showing webpack folder with multiple version of vue files

Something happened recently as this was working fine. I am unable to debug vue files in the sources tab like I used to do.
Currently it shows different versions of each vue file, and all of them are compressed, and they don't show the file. Also extra folders like lang sync^\ that I did not created.
I tried to add a workspace in Filesystem Tab, it did not help.
I looked in Chrome, and Firefox and both have the same problem.
This is the webpack code.
module.exports = env => {
return {
mode: 'development',
entry: {
main: ['babel-polyfill', './src/js/main.js']
},
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: [
/node_modules/
]
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
outputPath: (url, resourcePath, context) => {
if (url.indexOf('logo') !== -1) {
return `./brand/${env}/${url}`;
}
return `./src/assets/cards/${url}`;
}
}
},
{
test: /\.(svg)$/,
loader: 'html-loader', // since svg is an inline image
options: {
name: '[name].[ext]',
outputPath: './src/assets'
}
},
{
test: /\.ico$/,
loader: 'file-loader',
options: {
name: '[name].ico',
outputPath: '/'
}
},
{
test: /\.(woff|woff2|eot|ttf)(\?v=[0-9]\.[0-9]\.[0-9])?$/, // todo include svg
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './fonts'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'scss': path.resolve(__dirname, './src/scss'),
'$assets': path.resolve(__dirname, './src/assets'),
'$brand': path.resolve(__dirname, './brand')
},
modules: [
'node_modules',
path.resolve(__dirname),
path.resolve(__dirname, 'src/assets')
],
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true,
allowedHosts: Object.values(BRANDS),
port: PORT,
publicPath: '/dist/',
before: function (app) {
console.log('listening on ' + Object.values(BRANDS).join(' and ') + ' on port ' + PORT);
}
},
performance: {
hints: false
},
devtool: '#inline-source-map',
plugins: [
new VueLoaderPlugin(),
new webpack.DefinePlugin(processEnv),
new HtmlWebpackPlugin({ //Generates an HTML file for your application by injecting automatically all your generated bundles.
title: '...',
template: 'template.html',
filename: 'index.html',
hash: true,
meta: {
viewport: 'width=device-width,initial-scale=1,user-scalable=no'
}
}),
],
}
};
Try to add this to vue.config.js
module.exports = {
//...
configureWebpack: config => {
if (process.env.NODE_ENV === 'development') {
config.devtool = 'eval-source-map';
config.output.devtoolModuleFilenameTemplate = info =>
info.resourcePath.match(/\.vue$/) && !info.identifier.match(/type=script/)
? `webpack-generated:///${info.resourcePath}?${info.hash}`
: `webpack-yourCode:///${info.resourcePath}`;
config.output.devtoolFallbackModuleFilenameTemplate = 'webpack:///[resource-path]?[hash]';
}
},
// ...
}
Read more

How to add less-loader to react-app-rewired config?

I am using react-app-rewired and I want to add the less-loader to the config-overides.js but it doesn't work,
I tried with
module.exports = {
module: {
rules: [
{
test: /\.less$/i,
loader: [
// compiles Less to CSS
"style-loader",
"css-loader",
"less-loader",
],
},
],
},
};
and
module.exports = function override(config, env) {
config.module.rules[1].oneOf.push({
test: /\.less$/,
use: [
{
loader: "style-loader",
},
{
loader: "css-loader", // translates CSS into CommonJS
},
{
loader: "less-loader", // compiles Less to CSS
},
],
})
but nothing works, any help please ?
Am using:
react-app-rewired: "^2.2.1"
less: "^4.1.2"
less-loader: "^10.2.0"
react-scripts: "^5.0.0"
it worked with:
module.exports = function override(config, env) {
config.module.rules[1].oneOf.splice(2, 0, {
test: /\.less$/i,
exclude: /\.module\.(less)$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
})
return config
}

Webpack.config.js doesn't want to use an appriopriate loader to handle PNG images

I use in my project two library:
"leaflet": "^1.0.3",
"react-leaflet": "^1.9.1",
I import following styles in my map.less file:
#import "../../../../../../node_modules/leaflet/dist/leaflet.css";
I see some errors related to png files included in leaflet package. One of them is below:
ERROR in ./node_modules/leaflet/dist/images/marker-icon.png
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
# ./node_modules/css-loader?{"minimize":true}!./node_modules/leaflet/dist/leaflet.css 7:6181-6216
# ./node_modules/css-loader??ref--5-2!./node_modules/postcss-loader/lib??ref--5-3!./node_modules/less-loader/dist/cjs.js??ref--5-4!./src/app/components/toolkit/monitor-management/monitor-management-settings/map.less
This is my webpack config:
const config = {
some aliases outputs, entries etc...
module: {
rules: [
{
test: /\.js$/,
exclude: [
path.resolve(__dirname, 'node_modules')
],
use: [{
loader: 'babel-loader',
options: {
presets: [
'#babel/preset-env',
'#babel/react'
],
plugins: [
'#babel/plugin-proposal-class-properties',
'#babel/plugin-proposal-export-default-from',
'babel-plugin-jsx-remove-data-test-id'
]
}
}]
},
{
test: /\.less$/,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'modules/src')
],
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true
}
},
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('autoprefixer')
];
}
}
},
{
loader: 'less-loader',
options: {}
}
]
})
},
{
test: /\.(ttf|eot|svg|woff|woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [
path.resolve(__dirname, 'public/img')
],
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: '/fonts/',
publicPath: '/login/fonts/'
}
},
{
test: /\.(svg|png|jp(e*)g)$/,
include: [
path.resolve(__dirname, 'public/img')
],
use: [
{
loader: 'url-loader',
options: {
limit: 10, // Convert images to base64 strings
name: '[name].[ext]',
outputPath: '/img/',
publicPath: '/login/img/'
}
}
]
},
],
},
some plugins...
}
In my opinion I would be able to load png thanks to url-loader.
Maybe the reason is in another place. Maybe sould I use file-loader to handle this case?
The problem here is about requiring an image from node_modules specifically in this case node_modules/leaflet/dist/images/marker-icon.png. However, the url-loader is set up just handling this only: path.resolve(__dirname, 'public/img') that's why all images in other places can't be handled.
Normally you can either add more paths into it or completely remove it (this is what I prefer):
{
test: /\.(svg|png|jp(e*)g)$/,
// Remove this
// include: [
// path.resolve(__dirname, 'public/img')
// ],
use: [
// ...
]
},

Webpack 5: how to disable copying static files to build folder?

I am used webpack 2 for project, and some days ago I have upgraded to version 5.6.
2-3 hours and project compiling OK, but I have just one problem.
All static files (images, fonts) I store in /static/ folder.
CSS file:
.link-yellow{
background-image: url("/static/wifi2.png");
}
.link-red{
background-image: url("/static/wifi4.png");
}
.link-grey{
background-image: url("/static/wifi3.png");
}
Webpack 2 after compiling JS bundle file don't touch css url(...) links but webpack 5 copying files to folder /build/ and finally I have this:
4e4b4524859364a45966.png
5b97c8a6a9b9aa05b4be.otf
7ed064d8178174fa9ce1.woff2
14d3902c59ccd98328c8.png
440e51cee01b3e78fed5.png
664550a92a1dbb39a80a.png
build.js
And this records (like 4e4b4524859364a45966.png) in build.js:
...3197:function(t,e,n){"use strict";t.exports=n.p+"4e4b4524859364a45966.png"}...
Build progress:
I tried to use file-loader with this params:
module: {
rules: [
....
{
test: /\.(png|svg|jpg|jpeg|gif|gif|woff|woff2|eot|ttf|otf)$/,
loader: 'file-loader',
options:{
emitFile: false,
name: '[name].[ext]'
}
}
....
]
}
but after that I have the same files in /build/ folder with texts inside like this:
export default __webpack_public_path__ + "wifi2.png";
export default __webpack_public_path__ + "exo2.otf";
export default __webpack_public_path__ + "wifi3.png";
I moved some images files to style="..." attributes in vue files:
<div id="mainFooter" style="background: transparent url('/static/wifi3.png') no-repeat 0 0">
and webpack don't touch this text and not generate copies of static files in /build/ folder and all works fine. It is that I need.
Full source of webpack.config.js:
var path = require('path');
var webpack = require('webpack');
const { VueLoaderPlugin } = require('vue-loader');
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
mode: 'production',
plugins: [
new VueLoaderPlugin()
],
module: {
rules: [
{
test: /\.styl$/,
use: ['style-loader', 'css-loader', 'stylus-loader']
},
{
test: /\.css$/,
use: ['vue-style-loader','css-loader']
},
{
test: /\.scss$/,
use: ['vue-style-loader','css-loader','sass-loader'],
},
{
test: /\.sass$/,
use: ['vue-style-loader','css-loader','sass-loader?indentedSyntax'],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|svg|jpg|jpeg|gif|gif|woff|woff2|eot|ttf|otf)$/,
loader: 'file-loader',
options:{
emitFile: false,
name: '[name].[ext]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
client: {
overlay: true,
},
static: './'
},
performance: {
hints: false
},
devtool: 'eval-source-map',
optimization: {
minimize: true,
portableRecords: true,
removeAvailableModules: true,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
format: {
comments: false,
},
},
extractComments: false
})
]
}
};
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = 'inline-nosources-cheap-source-map';//source-map
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
Maybe someone know how to disable generating static files in /build/ folder and leave files url's in css with no modifying paths?
UPDATE:
I have some progress. Code disabling touching urls in css files, but fonts still copying:
{
test: /\.css$/,
use: ['vue-style-loader']
},
{
test: /\.css$/,
loader: 'css-loader',
options: {
url: false,
},
},
UPDATE2: Done. Adding:
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
type: "asset",
}
finally disabling generate other files. At now generating only /build/build.js file

Vue+Webpack configuration

I try to use Vue2 with Webpack4. Command webpack-dev-server finished with success, but it shows me only folders:
My webpack.config.js is:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
module.exports = {
entry: './src/main.js',
output: {
publicPath: './',
path: path.join(__dirname, '/dist'),
filename: 'result.js',
},
devServer: {
compress: true,
port: 8532,
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.s[ac]ss$/i,
use: [
{ loader: 'vue-style-loader' },
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.(png|jpg|gif)$/,
exclude: [path.resolve(__dirname, "public/fonts")],
use: ['file-loader'],
},
],
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './public/index.html',
minify: false,
})
],
};
This is my first experience with Vue.
What am I doing wrong?
Before that I tried to use vue-cli, but as I understood I can't use scss and Pug without Webpack.
Am I right?