关于babel:elementui部分引入失效问题追踪
背景我的项目应用vue2与element-ui;最近发现我的项目的局部引入生效了 // babel.config.jsmodule.exports = api => { return { "presets": ['@vue/cli-plugin-babel/preset'], "plugins": [ [ "component", { "libraryName": "element-ui", "styleLibraryName": "theme-chalk" } ] ] };};babel-plugin-component 负责将element-ui的引入代码转换为局部引入代码; 全量的element-ui是被谁引入的这里咱们应用Vue.component这个函数作为切入点;发现Vue.component对于同一个组件(如ElTooltip)调用了2次;看到全量引入的element-ui来自于node_modules中的包;查看了下logic-tree这个包的代码 // 打包前源码import { Button } from 'element-ui';// 打包后var _ks_kwai_ui__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! element-ui */ \"../../node_modules/element-ui/lib/element-ui.common.js\");能够看出包自身代码没有问题,是咱们在打包时babel-plugin-component没能在包文件上失效,导致import语句被webpack翻译为应用了main下的index.js 为什么babel-plugin-component没能失效?能够看出babel-loader没有去编译node_modules下的文件。很好了解,如果所有node_modules代码都要编译,会大大增加打包与热更新工夫;在babel-plugin-component源码中退出log,发现的确没有调用 ImportDeclaration: function ImportDeclaration(path, _ref2) { var opts = _ref2.opts; var node = path.node; var value = node.source.value; var result = {}; if (Array.isArray(opts)) { result = opts.find(function (option) { return option.libraryName === value; }) || {}; } console.log('hy', value); ...通过文档浏览后,发现参数transpileDependencies能够管制node_modules哪些包须要通过babel ...