-
Hi! We can use asset loader options for images: https://webpack.js.org/guides/asset-modules/ My webpack config have some loader rules for SVG: module: {
rules: [
{
test: /\.svg/i,
type: "asset/inline",
generator: {
dataUrl: (/** @type {Buffer} */ content) => {
console.log(content.toString());
return svgToMiniDataURI(content.toString());
},
},
},
{
test: /\.(jpe?g|ico|png|svg|webp)/i,
type: "asset",
parser: {
dataUrlCondition(source, { filename }) {
console.log(filename);
// inline only svg
if (/\.svg/i.test(filename)) {
return Buffer.byteLength(source) <= 0 * 1024; // =maxSize: 2.5kb
}
return false;
},
},
},
{
test: /\.s?css/i,
use: [
{
loader: "css-loader",
options: {
importLoaders: 1,
sourceMap: true,
},
},
{
loader: "sass-loader",
options: {
implementation: "sass-embedded",
api: "modern-compiler", // 'modern-compiler' since sass-loader v14.2.0
warnRuleAsWarning: true, // Treats the @warn rule as a webpack warning.
sourceMap: true,
sassOptions: {
style: "expanded",
quietDeps: true,
silenceDeprecations: ["import"],
},
},
},
],
},
],
}, Important rules for images: {
test: /\.svg/i,
type: "asset/inline",
generator: {
dataUrl: (/** @type {Buffer} */ content) => {
console.log(content.toString());
return svgToMiniDataURI(content.toString());
},
},
},
{
test: /\.(jpe?g|ico|png|svg|webp)/i,
type: "asset",
parser: {
dataUrlCondition(source, { filename }) {
console.log(filename);
// inline only svg
if (/\.svg/i.test(filename)) {
return Buffer.byteLength(source) <= 0;
}
return false;
},
},
},
I can try to import several SVG files into SCSS: .i-whatsapp::before {
background-image: url("@src/img/icons/messengers/whatsapp.svg");
} As a result of compilation I get an data-URI with SVG image in CSS file. But: I intentionally left console commands in the example configuration. The console output does not contain any image files imported into SCCS. I found out that the Any ideas to find out the reason why the loader rules are ignored? Now I don't even understand how to find out which loader is doing this. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 19 replies
-
All works fine. module: {
rules: [
{
test: /\.(css)$/,
loader: 'css-loader',
},
{
test: /\.svg/i,
type: 'asset', // only by `asset` will be called the `dataUrlCondition()` function
generator: {
dataUrl: (content) => {
console.log('SVG: ', content.toString());
return 'data:image/svg+xml,' + content.toString();
},
},
parser: {
dataUrlCondition(source, { filename }) {
console.log('parser: ', filename);
return true; // IMPORTANT: only by return `true` will be called the `generator.dataUrl`
},
},
},
},
],
}, In this example, I see outputs in console:
Check your images rules, you have two rules for |
Beta Was this translation helpful? Give feedback.
-
@webdiscus, I've tried all) Import from HTML and JS works perfect. But import SVG in SCSS lives its own uncontrolled life) |
Beta Was this translation helpful? Give feedback.
-
https://github.com/vralle/svg-data-uri-in-css
|
Beta Was this translation helpful? Give feedback.
-
I've caught that. In my configuration the loaders use |
Beta Was this translation helpful? Give feedback.
-
https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data
I always strive to follow standards. Latest Bootstrap does the same for SVGs: And I'm convinced from my own experience that modern browsers are what developers have on their computers. If you look at user devices in the wild, you will quickly come across history, such as IE on corporate computers or Android 4 on someone's phone. |
Beta Was this translation helpful? Give feedback.
-
If we are discussing the import of SVG, then I believe that the decision should be made by the developer based on the specific task. I would like to know the reason why Webpack options are ignored. |
Beta Was this translation helpful? Give feedback.
-
@webdiscus, I have created two issues related to SVG import. I'm currently experiencing some overload, so I would like to have more time to find the right solution. |
Beta Was this translation helpful? Give feedback.
-
Closed due to changes in v4.19 |
Beta Was this translation helpful? Give feedback.
Closed due to changes in v4.19