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

dynamically import not working when build #24

Closed
zinken7 opened this issue Aug 25, 2021 · 13 comments
Closed

dynamically import not working when build #24

zinken7 opened this issue Aug 25, 2021 · 13 comments

Comments

@zinken7
Copy link

zinken7 commented Aug 25, 2021

@jpkleemans Hi, I used this but failed when build for production.
It's working in dev mode

<script lang="ts">
  import { computed, defineComponent, defineAsyncComponent } from 'vue'
  export default defineComponent({
    props: {
      name: {
        type: String,
        default: undefined,
      },
    },
    setup(props) {
      const currentIcon = computed(() => defineAsyncComponent(() => import(`../../icons/${props.name}.svg?component`))).value
      return {
        currentIcon
      }
    }
  })
</script>
@jpkleemans
Copy link
Owner

Hi, thanks for reporting. Could you share an example project so I can reproduce the problem, and investigate how to resolve it.

@zinken7
Copy link
Author

zinken7 commented Sep 14, 2021

I made an example here:
https://github.com/zinken7/vite_svg_inline
It only working on dev mode. It's not working when build.
Live example:
https://codesandbox.io/s/epic-williamson-sfmb5

@jd-solanki
Copy link

Hi @jpkleemans as I mentioned my another issue in #25 (comment) another thing I was trying to do is dynamically load SVG from a public directory(I ask for user input so I have to make it dynamic).

I write const Logo = () => import('../public/logo.svg'); I get error in console:
Screenshot 2021-10-27 at 11 44 53 PM

but if I turn it in to import Logo from '../public/logo.svg'; it works fine.

However, using ../public/logo.svg still gives a warning:
Screenshot 2021-10-27 at 11 46 45 PM

This is related to #25

If I turn url from ../public/logo.svg to /logo.svg I get error atttached in this comment #25 (comment)

BTW I am using nuxt + TS if you are going to test it.

@jpkleemans
Copy link
Owner

@zinken7 does it work when omitting the ?component param?

@silencerspirit
Copy link

@jpkleemans

Hi! All imports on server-side (raw,url,component) calling error.

500 Cannot read properties of undefined (reading 'stubModule')

On client-side - its working;

Code example:

<template>
<component :is="icon" />
</template>

<script>
let icon = await import(`../../assets/svg/${props.name.toLowerCase()}.svg?component`).then((_) => _.default);
</script>

@silencerspirit
Copy link

silencerspirit commented Jun 21, 2022

@jpkleemans

Hi! All imports on server-side (raw,url,component) calling error.

500 Cannot read properties of undefined (reading 'stubModule')

On client-side - its working;

Code example:

<template>
<component :is="icon" />
</template>

<script>
let icon = await import(`../../assets/svg/${props.name.toLowerCase()}.svg?component`).then((_) => _.default);
</script>

Resolve this problem, create a functional component:

import { h } from 'vue';

const CommonIcon = (props, context) => {
 const svg = require(`../../assets/svg/${props.name}.svg`).default;
 return h('span', {...context.attrs, class: 'common-icon', }, [
    h(svg, { class: 'h-[inherit] w-[inherit]' }, [h(svg)]),
  ]);
}

CommonIcon.props = {
  name: {
    type: String,
    required: true,
  },
  width: {
    type: String,
    default: '',
  },
  height: {
    type: String,
    default: '',
  },
  size: {
    type: String,
    default: '',
  },
};

export default CommonIcon;

@jpkleemans
Copy link
Owner

@silencerspirit thanks for posting your solution!

@cdwmhcc
Copy link

cdwmhcc commented Jun 22, 2022

import { h } from 'vue';

const CommonIcon = (props, context) => {
const svg = require(../../assets/svg/${props.name}.svg).default;
return h('span', {...context.attrs, class: 'common-icon', }, [
h(svg, { class: 'h-[inherit] w-[inherit]' }, [h(svg)]),
]);
}

CommonIcon.props = {
name: {
type: String,
required: true,
},
width: {
type: String,
default: '',
},
height: {
type: String,
default: '',
},
size: {
type: String,
default: '',
},
};

export default CommonIcon;

require is not defined

@silencerspirit
Copy link

@cdwmhcc you need install plugin vite-require;

In vite.config.ts:

import svgLoader from 'vite-svg-loader';
import { viteRequire } from 'vite-require';

export default {
  ...,
  plugins: [svgLoader(), viteRequire()]
};

and if you use vitest as test platform, in vitest.config.ts:

import { viteRequire } from 'vite-require';
import svgLoader from 'vite-svg-loader';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  ...,
  plugins: [vue(), viteRequire(), svgLoader()],
});

@cdwmhcc
Copy link

cdwmhcc commented Jul 3, 2022

@cdwmhcc you need install plugin vite-require;

In vite.config.ts:

import svgLoader from 'vite-svg-loader';
import { viteRequire } from 'vite-require';

export default {
  ...,
  plugins: [svgLoader(), viteRequire()]
};

and if you use vitest as test platform, in vitest.config.ts:

import { viteRequire } from 'vite-require';
import svgLoader from 'vite-svg-loader';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  ...,
  plugins: [vue(), viteRequire(), svgLoader()],
});

Not working in nuxt.js 3

@silencerspirit
Copy link

@cdwmhcc you need install plugin vite-require;
In vite.config.ts:

import svgLoader from 'vite-svg-loader';
import { viteRequire } from 'vite-require';

export default {
  ...,
  plugins: [svgLoader(), viteRequire()]
};

and if you use vitest as test platform, in vitest.config.ts:

import { viteRequire } from 'vite-require';
import svgLoader from 'vite-svg-loader';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  ...,
  plugins: [vue(), viteRequire(), svgLoader()],
});

Not working in nuxt.js 3

U welcome https://stackblitz.com/edit/github-1ck5ek?file=app.vue :)

@cdwmhcc
Copy link

cdwmhcc commented Jul 9, 2022

I know the reason.

Cannot use defineComponent:

export default defineComponent({
    async setup(props, { emit, attrs, slots }) {
        return () => {
            return ()
        };
    },
});

Must use:

import { h } from 'vue';

const CommonIcon = (props, context) => {
 const svg = require(`../../assets/svg/${props.name}.svg`).default;
 return h('span', {...context.attrs, class: 'common-icon', }, [
    h(svg, [h(svg)]),
  ]);
}

CommonIcon.props = {
  name: {
    type: String,
    required: true,
  },
};

export default CommonIcon;

@cdwmhcc
Copy link

cdwmhcc commented Jul 9, 2022

@silencerspirit Thank you for your reply. It will load all icons. I need to load on demand.

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

5 participants