webpack4动态import无法拿到chunkname

webpack.base.config.js:

    entry: {
        app: './src/index.js'
    },
    output: {
        path: config.build.assetsRoot,  // '/'
        publicPath: config.dev.assetsPublicPath,    // 'static'
        filename: '[name].bundle.js',
        chunkFilename: '[name].chunk.js'
    },

router(使用require.ensure()能正确拿到name):

const routes = [
  {
    path: '/',
    component: r => {
      require.ensure([], () => r(require('./view/index/index.vue')), 'index')
    }
  },
  {
    path: '/errorPage',
    component: r => {
      require.ensure([], () => r(require('./view/errorPage/errorPage.vue')), 'errorPage')
    }
  },
  {
    path: '/page',
    component: r => {
      require.ensure([], () => r(require('./view/page/page.vue')), 'page')
    }
  }
];

结果(正确拿到name):

clipboard.png

但是!!!!!使用import:

const routes = [
  {
    path: '/',
    component: () => import(/* webpackChunkName: "index" */ './view/index/index.vue')
  },
  {
    path: '/errorPage',
    component: () => import(/* webpackChunkName: "errorPage" */ './view/errorPage/errorPage.vue')
  },
  {
    path: '/page',
    component: () => import(/* webpackChunkName: "page" */ './view/page/page.vue')
  }
];

结果不能拿到name:

clipboard.png

有人说跟babel设置有关,但我尝试了也没作用:
.babelrc:

{
    "env": {
        "production": {
            "presets": [
                ["env", {
                    "targets": {
                        "browsers": ["last 2 version", "ie 10"]
                    },
                    "modules": false,
                    "debug": true
                }]
            ],
            "plugins": [
                "transform-runtime",
                "transform-object-rest-spread",
                "dynamic-import-webpack"
            ]
        },
        "development": {
            "presets": [
                ["env", {
                    "targets": {
                        "chrome": 60
                    },
                    "modules": false,
                    "debug": true
                }]
            ],
            "plugins": [
                "transform-object-rest-spread",
                "dynamic-import-webpack"
            ]
        }
    }
}

向各位大佬求解

回答:

安装插件 babel-plugin-syntax-dynamic-import
.babelrc中修改:

"plugins": [
  "syntax-dynamic-import"
]

参考链接

回答:

正解,忘记结帖了。赞

回答:

我的vue-cli升级webpack4之后,路由页动态加载就无效了,试了上面的方法,编译时,还是都编译到了一个js文件里,请问你有遇到这个问题么

回答:

因为菜单都是后台的,路由是根据菜单动态组的,所以chunk name能不能是一个变量,如何写?

暂无评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注