最近使用ElementUI作项目的时候用Babel的插件babel-plugin-component作按需加载,使得组件打包的JS和CSS包体积大大缩小,加载速度也大大提高,全部想模仿作一个组件库也来作下按需加载。css
vue create bes-ui
复制代码
注意的是cli3的脚手架用的Babel7的配置,只有babel.config.js文件,因此要本身添加.babelrc文件。vue
新建项目以后,能够按照本身的想法建文件结构,也能够按照babel-plugin-component官方文档的目录构建: node
这里组件库写了两个样例:component1,component2 。 每一个组件都添加了本身的初始化install方法(用于按需加载时候独立使用),install方面里面加个日志,方便后面看看组件加载记录。 git
if (styleLibrary && _typeof(styleLibrary) === 'object') {//这个是样式的一些配置
styleLibraryName = styleLibrary.name;
isBaseStyle = styleLibrary.base;
modulePathTpl = styleLibrary.path;
mixin = styleLibrary.mixin;
styleRoot = styleLibrary.root;
}
if (styleLibraryName) {//是否在.babelrc配置了styleLibraryName
if (!cachePath[libraryName]) {//是否存在配置好的样式获取路径
var themeName = styleLibraryName.replace(/^~/, '');
cachePath[libraryName] = styleLibraryName.indexOf('~') === 0 ?//路径是否相对于element-ui/lib
resolve(process.cwd(), themeName) :
"".concat(libraryName, "/").concat(libDir, "/").concat(themeName);
}//若是是相对于lib 组合路径---element-ui/lib/theme-chalk/ 这个目录下是75个css文件
//这里将这一段路径保存在了cachePath[libraryName] 后续会用到
if (libraryObjs[methodName]) {//做者也没搞清楚这里是什么 不过不要紧,事实证实这里走了false
/* istanbul ingore next */
if (cache[libraryName] === 2) {
throw Error('[babel-plugin-component] If you are using both' + 'on-demand and importing all, make sure to invoke the' + ' importing all first.');
}
if (styleRoot) {//这里默认是没有配置的 全部走false
path = "".concat(cachePath[libraryName]).concat(styleRoot).concat(ext);
} else {
path = "".concat(cachePath[libraryName]).concat(_root || '/index').concat(ext);
}//这里会默认先加载index.css 由于ext没设置就会默认css
cache[libraryName] = 1;
} else {//走了else
if (cache[libraryName] !== 1) {//这里确定是不等于1,由于上面一行才会赋值1
/* if set styleLibrary.path(format: [module]/module.css) */
var parsedMethodName = parseName(methodName, camel2Dash);
if (modulePathTpl) {
var modulePath = modulePathTpl.replace(/\[module]/ig, parsedMethodName);
path = "".concat(cachePath[libraryName], "/").concat(modulePath);
} else {//这里走了else 也就是样式路径后续为模块名.[ext]
path = "".concat(cachePath[libraryName], "/").concat(parsedMethodName).concat(ext);
}//全部这里的路径就是element-ui/lib/
if (mixin && !isExist(path)) {
path = style === true ? "".concat(_path, "/style").concat(ext) : "".concat(_path, "/").concat(style);
}
if (isBaseStyle) {
addSideEffect(file.path, "".concat(cachePath[libraryName], "/base").concat(ext));
}
cache[libraryName] = 2;
}
}
addDefault(file.path, path, {
nameHint: methodName
});
} else {
if (style === true) {
addSideEffect(file.path, "".concat(path, "/style").concat(ext));
} else if (style) {
addSideEffect(file.path, "".concat(path, "/").concat(style));
}
}
}
复制代码
转回正题,文件建立好了以后,就能够发包到npm了,这里提供了两种方式:1是将组件库打包压缩成css和js,暴露出去(这种方式没法作按需,由于因此代码压缩在一块儿了);2是将lib下的index暴露出去(这种方式能够作按需加载);以下(package文件): github
组件放上去后,就能够在项目里面使用了,这里用vue create添加了一个example项目: npm
最后能够打个测试项目包,看下里面确实没有其余组件的代码和样式,这里就不贴图了,你们能够本身尝试,总结下这里的bes-ui其实就是模仿ElementUI的一个简易的组件库(虽然目前只加了按需加载,后续能够把国际化和主题加上),项目里面的一些配置和文件都大同小异,若是有兴趣构建本身的vue组件库的时候,bes-ui是个不错的模版。element-ui
GitHub项目地址:github.com/BothEyes199…api