New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"module export" instead of svg directly #524
Comments
Hi, are you using webpack 5 ? I am asking because we have a PR #478 which has test failures, which makes me think that something in the code is not compatible with webpack 5. |
Yes, i do. If webpack 5 is the problem, then that's curious. Because it worked with webpack 5 up until recently. I try to find out what changed. |
I got good and bad news. Svg-url-loader was responsible for "model export" within the data uri. The "type: 'asset/inline'" triggers the builtin asset module from webpack 5, making the conversion happening twice. Removing svg-url-loader resulting in
gets it working again. Unfortunately i have not been able to get it to work that svg-url-loader is solely responsible. It is a pity and i hope that svg-url-loader (imho the more comfortable solutioni) will be available again under webpack 5. Unfortunately my js knowledge is not enough to support the project more deeply here. |
You can use {
test: /\.svg$/,
type: 'asset/inline',
generator: {
dataUrl(content) {
const REGEX_XML_DECLARATION = /^\s*<\?xml [^>]*>\s*/i;
const REGEX_MULTIPLE_SPACES = /\s+/g;
const REGEX_DOUBLE_QUOTE = /"/g;
const REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g;
const transformedSvgSource = content
.toString('utf8')
.replace(REGEX_XML_DECLARATION, '')
.replace(REGEX_MULTIPLE_SPACES, ' ')
.replace(REGEX_DOUBLE_QUOTE, "'")
.replace(REGEX_UNSAFE_CHARS, uriEncodeMatch)
.trim();
const data = 'data:image/svg+xml,' + transformedSvgSource;
function uriEncodeMatch(match) {
return '%' + match[0].charCodeAt(0).toString(16).toLowerCase();
}
return data;
},
},
} Or you can simply use const miniSVGDataURI = require('mini-svg-data-uri');
...
{
test: /\.svg$/,
type: 'asset/inline',
generator: {
dataUrl: (content) => miniSVGDataURI(content.toString()),
},
} |
From Webpack 5 documentation:
{
test: /\.svg(\?.*)?$/,
use: [
'svg-url-loader',
],
type: 'javascript/auto',
}, Works perfect for me. |
Thank you guys! LambdaZed's solution got me learning something again and lada's hint for the documentation is absolutely right. I somehow read over this paragraph. |
Hi Folks,
in css there will be generated
background-image: url(data:image/svg+xml;base64,bW9kdWxlLmV4cG9ydHMgPSAiZGF0YTpp...
the base64-encoded part is
module.exports = "data:image/svg+xml,%3csvg viewBox='0 0 32.72 90.57' xmlns='http://www.w3.org/2000/svg'%3e%3cg fill='none'...
My webpack config:
I cannot find the cause of this. There is no other svg-loader.
My packages:
Does anybody got a hint for me?
The text was updated successfully, but these errors were encountered: