无法从node_modules动态导入

时间:2019-03-21 05:10:40

标签: vue.js webpack node-modules dynamic-import

我制作了一个vue组件包my-custom-comp,其中包含动态导入: my-custom-comp.vue

<template>
  <component v-if="id && data" :is="`list-${id}`" :data="data" />
</template>

<script>
export default {
  props: ['id','data'],
  components: {
    'list-1': () => import(/* webpackChunkName: "MyTaglist0" */'./list-1.vue'),
    'list-2': () => import(/* webpackChunkName: "MyTaglist0" */'./list-2.vue'),
    'list-3': () => import(/* webpackChunkName: "MyTaglist1" */'./list-3.vue'),
    'list-4': () => import(/* webpackChunkName: "MyTaglist1" */'./list-4.vue')
  }
}
</script>

lib中的输出文件:

  my-custom-comp.common.js
  my-custom-comp.common.MyTaglist0.js
  my-custom-comp.common.MyTaglist1.js

我的应用中安装了my-custom-comp软件包,并将软件包路径添加到resolve.moduleswebpack.config.js

module.exports = {
  ...
  resolve: {
    modules: [path.join(__dirname, '../node_modules'), path.join(__dirname, '../node_modules/my-custom-comp/lib')],
  }
  ...
}

但是MyTaglist0.js无法解决:

GET http://localhost:8882/my-custom-comp.common.MyTaglist0.js net::ERR_ABORTED 404 (Not Found)

如何解决从node_modules动态导入?

测试仓库: https://github.com/Miaoxingren/webpack-issue-8934

0 个答案:

没有答案