gitee https://gitee.com/xiaoxfa/source-vue2
Rollup 仿真环境
为啥是 Rollup ,因为这是个 库Lib
准备一个项目,安装依赖,配置 rollup.config.js .babelrc ,初始化项目。
配置不提,简单看下 rollup 编译空function 得到的结果:
(function (global, factory) {// 判断 exports 和 module 若满足啧 commonJStypeof exports === "object" && typeof module !== "undefined"? (module.exports = factory()) // 继续判断是否有 define 判断 amd: typeof define === "function" && define.amd? define(factory) // 再没有视为浏览器里的全局变量,挂 Vue 到全局。: ((global =typeof globalThis !== "undefined" ? globalThis : global || self),(global.Vue = factory()));})(this, function () {"use strict";function Vue() {}return Vue;});//# sourceMappingURL=vue.js.map
具体细节看提交就可以了。
rollup配置一览:
import babel from "rollup-plugin-babel";import serve from "rollup-plugin-serve";export default {input: "./src/index.js",output: {format: "umd", // eefe esm cjs umd 模块化类型file: "dist/umd/vue.js",name: "Vue", // 打包后的全局变量的名字sourcemap: true,},plugins: [babel({exclude: "node_modules/**",}),process.env.ENV === "development"? serve({open: true,openPage: "/public/index.html",port: 3000,contentBase: "",}): null,],};
new Vue 初始化
基于 Prototype 原型来拓展 Vue,具体看代码。
