webpackChunkName 魔法注释失效解决,只能显示 id 的解决方案

问题
路由懒加载时,采用ES6的按需加载方式,却无法显示webpackChunkName

const BackHome = () => import(/* webpackChunkName: 'backHome' */ '@/views/back/back_home');
const ArticleMgt = () => import(/* webpackChunkName: 'articleMgt'*/ '@/views/back/article_mgt');
const CommentMgt = () => import(/* webpackChunkName: 'commentMgt' */ '@/views/back/comment_mgt');

打包结果:
webpackChunkName 魔法注释失效解决,只能显示 id 的解决方案_第1张图片
解决方案

改用Webpack的按需加载方式:

require.ensure(dependencies: String[], callback: function(require), chunkName: String)

修改如下:

const BackHome =  resolve => require.ensure([], () => resolve(require('@/views/back/back_home')), 'backHome');
const ArticleMgt = resolve => require.ensure([], () => resolve(require('@/views/back/article_mgt')), 'articleMgt');
const CommentMgt = resolve => require.ensure([], () => resolve(require('@/views/back/comment_mgt')), 'commentMgt');

打包结果:
在这里插入图片描述
原因初步鉴定——Babel配置文件

采用ES6的import()方式,删除了Babel的配置文件,打包结果能正确显示webpackChunkName。添加上则会显示id。尝试了@babel/plugin-syntax-dynamic-import 或 babel-plugin-syntax-dynamic-import 均无效。若有知道原因的大佬,望告知…

你可能感兴趣的:(前端,webpack,webpack,魔法注释,路由懒加载)