Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  143] [ 4]  / answers: 1 / hits: 8760  / 3 Years ago, sun, january 17, 2021, 12:00:00

Suppose I had the following webpack.config.js file:


module.exports = {
//...
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist"),
assetModuleFilename: "assets/[name][ext]",
},
module: {
rules: [
{
test: /.(woff|woff2|eot|ttf|otf)$/i,
type: "asset/resource",
},
{
test: /.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
],
},
plugins: [
//...
],
};

My question is how can I separate the fonts from the images? I want to put each one in a subdirectory called "fonts" and "images" under the assets folder. I know this is possible using the "file-loader". However, I want to use the asset module.


More From » webpack

 Answers
4

You can use the Rule.generator.filename property:


module.exports = {
// ...

module : {
rules : [
// ...

// Fonts
{
test : /.(woff2?|ttf|eot)(?v=w+)?$/,
type : 'asset/resource',
generator : {
filename : 'fonts/[name][ext][query]',
}
},
]
},

// ...
}

[#1940] Wednesday, January 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;