第三方插件分包 — 篇幅2
技术路线
- 编写获取第三方插件的工具类ThirdPartyUtils — 实现懒加载
- umd模式下加载文件 动态require引入文件
- script标签引入下 动态创建script标签引入第三方插件
多说无益,上代码,ThirdPartyUtils.js
const ThirdPartyType = {
axios: "axios.min.js",
echarts: "echarts.min.js",
html2canvas: "html2canvas.min.js",
};
// 初始化存储BuildOutput.js所在路径
const currentScriptSrc = document.currentScript.src;
/**
* @private
*/
class ThirdPartyUtils {
/**
* 异步获取第三方插件
* @param {string} thirdParty 第三方插件名
* @returns {Object} 返回插件对象
*/
static async getThirdParty(thirdParty) {
if (window[thirdParty]) {
return window[thirdParty];
}
// 判断是否在处于哪个环境
if (("object"==typeof exports&&"object"==typeof module)