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
Inlining SVGs #13
Comments
You can use SVGs straightaway in a standard HTML template e.g. <template>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
</template> Alternatively, if you want to save them as // Remove SVG from default rules
config.module.rules.forEach(rule => {
if (rule.test.toString() === "/.(svg|gif)$/i") {
rule.test = /\.(gif)$/i;
}
}); It might be fiddly though. Sorry this functionality isn't supported out-of-the-box with this module. |
@chrisjansky I made a modules: [
'nuxt-responsive-loader',
'@/modules/svgHtmlLoader', and the loader: const DEFAULT_SVG_LOADER_OPTIONS = {
minimize: true,
attrs: ['div:v-html'],
};
const setupSvgImageLoader = userOptions => config => {
// Find the existing webpack loader responsible for svgs (and other images)
const existingImageLoader = config.module.rules.find(
rule => rule.test.test('.svg')
);
/* If the image loader rule has been removed or edited then we cannot continue.
** It is not clear how to update the webpack rules.
** The user should define a custom webpack configuration.
*/
if (!existingImageLoader) {
throw new Error(
[
'svgHtmlLoader: Could not find the existing image loader rule.',
' The webpack config has been edited, perhaps by another Nuxt module.',
' To resolve this error try placing this module first in your Nuxt modules array',
' or use a custom webpack configuration instead.'
].join('')
)
}
/* Update the loader so it's no longer respo∏nsible for svg files */
if (existingImageLoader) {
existingImageLoader.test = /\.(gif)$/i;
}
/* Add the new loader rule */
config.module.rules.push({
test: /\.(svg)$/,
loader: 'html-loader',
options: userOptions || DEFAULT_SVG_LOADER_OPTIONS,
});
}
export default function svgHtmlLoader () {
const { svgHtmlLoader: userOptions } = this.options
this.extendBuild(setupSvgImageLoader(userOptions))
} |
Thank you both @geeogi and @tettoffensive! I've ended up importing .vue files per @geeogi suggestion, but might come to implement the latter if needed. |
Hey,
Thanks for a great plugin, getting it up and running with nuxt was a breeze. How do you inline SVGs without resorting to another loader (which causes a Webpack config conflict error, as stated in the README)?
Thanks!
The text was updated successfully, but these errors were encountered: