question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamically import not working when build

See original GitHub issue

@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>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
silencerspiritcommented, Jul 4, 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

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

2reactions
silencerspiritcommented, 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;
Read more comments on GitHub >

github_iconTop Results From Across the Web

[SOLVED] - Dynamic import not working - help - Meteor.js forums
Hey there! I'm trying to implement dynamic loading to split the code per page to minimise the main bundle size cause the app...
Read more >
Dynamic imports not working on built project - only on the dev ...
When I run parcel build index.html (build static files for browser) the dynamic imports are not working. This is the dynamic import I...
Read more >
import() in ts is not dynamic? what's the cause? - Stack Overflow
"dynamic" means you can do import("someFile" + someVariable) which you cannot do with a regular import statement. It's static, thus it would not...
Read more >
Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >
Dynamic imports and code splitting with Next.js
Dynamic imports, also known as code splitting, refers to the practice of dividing bundles of JavaScript code into smaller chunks, which are then ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found