Answer the question
In order to leave comments, you need to log in
How to save relative paths for images in Webpack?
How to save relative paths for images in the final project in Webpack?
Example:
/img/1.jpg
/img/folder/2.jpg
/dist/img/1.jpg
/dist/img/folder/2.jpg
test: /\.(gif|png|jpe?g|svg)$/i,
use: [{
loader: 'file-loader',
options: {
// перебор вариантов
name: '[name].[ext]' // -> /dist/1.jpg и /dist/2.jpg все в корень папки
name: '[folder]/[name].[ext]' // -> /dist/img/1.jpg и /dist/folder/2.jpg создает папки без вложенности
name: '[path]/[name].[ext]' // -> /dist/src/img/1.jpg и /dist/folder/2.jpg берет лишний src
path: path.resolve(__dirname, 'dist/[folder]'), // -> /dist/1.jpg и /dist/2.jpg все в корень папки
}
}
Answer the question
In order to leave comments, you need to log in
Faced a similar problem.
Solved it like this:
output: {
assetModuleFilename: (path) => {
if(typeof path.filename !== "undefined" && path.filename.match(/(img)/)) {
path.filename = path.filename.replace('src/', '');
return `${path.filename}/[name].[ext]`;
}
return '[name].[ext]';
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question