Skip to content
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

Loading svgs from different packages #186

Open
Gwalchmei opened this issue Mar 23, 2022 · 0 comments
Open

Loading svgs from different packages #186

Gwalchmei opened this issue Mar 23, 2022 · 0 comments

Comments

@Gwalchmei
Copy link

I want to dynamically load svg from consumer projects and from a "core" dependency project but I manage to only load files located in my consumer projects. I don't know if I'm missing something in my config or if it is a bug.

Core project

- src/
  - assets/
    - svg/
      - foo.svg
  - components/
    - Icon.vue

Icon.vue

<template>
  <div :is="svg" class="..." />
</template>

<script>
export default {
  name: 'Icon',
  props: { core: Boolean, name: String },
  computed: {
    svg() {
      return this.core
        ? require('@core/assets/svg/'+this.name+'.svg')
        : require('@/assets/svg/'+this.name+'.svg');
    },
};
</script>

Consumer projets

- src/
  - assets/
    - svg/
      - bar.svg
  - vue.config.js

vue.config.js

module.exports = {
  chainWebpack: (config) => {
    const svgRule = config.module.rule('svg');

    svgRule.uses.clear();

    svgRule
      .test(/\/svg\/.*\.svg$/)
      .use('babel-loader')
      .loader('babel-loader')
      .end()
      .use('vue-svg-loader')
      .loader('vue-svg-loader')
      .options({
        svgo: {
          plugins: [{ cleanupIDs: false }],
        },
      })
      .end();
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@core': 'package-name/src',
      },
    },
  },
}

And then in my components

<Icon core name="foo" /> <!-- Does not work -->
<Icon name="bar" /> <!-- Works -->

I have the following error

Error: Cannot find module './svg/foo.svg'
    at webpackContextResolve (eval at ./node_modules/package-name/src/assets sync recursive ^\.\/.*\.svg$ 
    at webpackContext (eval at ./node_modules/package-name/src/assets sync recursive ^\.\/.*\.svg$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant