storybook: Load svg using vue-svg-loader not working
.storybook/webpack.config.js
const path = require('path');
const fs = require('fs');
// Export a function. Accept the base config as the only param.
module.exports = async ({ config, mode }) => {
// `mode` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.
// Make whatever fine-grained changes you need
config.module.rules.push({
test: /\.scss$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
});
config.module.rules.push({
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {name: 'assets/[name].[hash:8].[ext]'},
},
],
});
config.module.rules.push({
test: /\.vue$/,
use: [
{
loader: "vue-svg-inline-loader",
options: { /* ... */ }
}
]
});
config.module.rules.push({
test: /\.svg$/,
loader: 'vue-svg-loader',
options: {name: 'assets/[name].[hash:8].[ext]'},
});
config.resolve.alias['@'] = path.resolve('src')
// Return the altered config
return { ...config, node: { fs: 'empty' } };
};
index.vue
<template>
<div>
<IconTest/>
</div>
</template>
<script>
import IconTest from "@/assets/svg/test-icon.svg"
export default {
components:{
IconTest
}
}
</script>
error:
[Vue warn]: Invalid Component definition: static/media/test-icon.d9ed9e17.svg
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 19 (5 by maintainers)
@devtoni’s workaround worked for me too, with one additional change.
As of Storybook
v6.9.0, I had to addpdfto the test regex that was being replaced. The updated code that worked for me is:https://github.com/storybookjs/vue-cli-plugin-storybook/issues/69#issuecomment-668171815
This config solved the issue for me: Using vite
3.0.0, vite-svg-loader3.4.0and storybook6.5.9@ecarrera solution isn’t working for me - I get an error when doing that saying “You may need an additional loader to handle the result of these loaders.”
It however looks like the svg’s are correctly loaded as vue components, but storybook still fails…
I have also tried this approach from the documentation:
But since I’m using vue-cli this isn’t working either because webpack is configured in
vue.config.jswhich doesnt have a ‘rules’ object exposedThanks @devtoni ! I was running into this trying to use
svg-inline-loader.I had a similar problem, the reason as shilman commented is because storybook by default in its webpack configuration is using file-loader for the svgs. So it founds a conflict with the vue-svg-loader.
What I did in my case and it works is to remove from the regex the svg extension and then as you did push the new rule of svg with the vue-svg-loader.
friends i need your help here about Vue Storybook import SVGs components https://stackoverflow.com/questions/75432897/load-svgs-components-in-storybook-vue-not-working
Thanks @ecarrera that worked for me as well. Been struggling with this all day!
Thank you @devtoni ! Works!