vue.js构建
vue2.6源码下载
github地址:https://github.com/vuejs/vue
vue2.6安装
运行npm i
安装所需的包,或者使用npm i -g cnpm
之后使用cnpm i
安装所需的包。具体查看package.json。
vue2.6打包
使用npm run
加上相应的指令得到相应的打包文件
"dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev",
"dev:cjs": "rollup -w -c scripts/config.js --environment TARGET:web-runtime-cjs-dev",
"dev:esm": "rollup -w -c scripts/config.js --environment TARGET:web-runtime-esm",
"dev:test": "karma start test/unit/karma.dev.config.js",
"dev:ssr": "rollup -w -c scripts/config.js --environment TARGET:web-server-renderer",
"dev:compiler": "rollup -w -c scripts/config.js --environment TARGET:web-compiler ",
"dev:weex": "rollup -w -c scripts/config.js --environment TARGET:weex-framework",
"dev:weex:factory": "rollup -w -c scripts/config.js --environment TARGET:weex-factory",
"dev:weex:compiler": "rollup -w -c scripts/config.js --environment TARGET:weex-compiler ",
"build": "node scripts/build.js",
"build:ssr": "npm run build -- web-runtime-cjs,web-server-renderer",
"build:weex": "npm run build -- weex",
"test": "npm run lint && flow check && npm run test:types && npm run test:cover && npm run test:e2e -- --env phantomjs && npm run test:ssr && npm run test:weex",
"test:unit": "karma start test/unit/karma.unit.config.js",
"test:cover": "karma start test/unit/karma.cover.config.js",
"test:e2e": "npm run build -- web-full-prod,web-server-basic-renderer && node test/e2e/runner.js",
"test:weex": "npm run build:weex && jasmine JASMINE_CONFIG_PATH=test/weex/jasmine.js",
"test:ssr": "npm run build:ssr && jasmine JASMINE_CONFIG_PATH=test/ssr/jasmine.js",
"test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2",
"test:types": "tsc -p ./types/test/tsconfig.json",
"lint": "eslint src scripts test",
"flow": "flow check",
"sauce": "karma start test/unit/karma.sauce.config.js",
"bench:ssr": "npm run build:ssr && node benchmarks/ssr/renderToString.js && node benchmarks/ssr/renderToStream.js",
"release": "bash scripts/release.sh",
"release:weex": "bash scripts/release-weex.sh",
"release:note": "node scripts/gen-release-note.js",
"commit": "git-cz"
使用npm run构建js文件
以npm run dev
为例:
执行npm run dev
会执行rollup -w -c scripts/config.js --environment TARGET:web-full-dev
指令。
解释:运行scripts/config.js文件,并传入变量TARGET。
在scripts/config.js下执行代码:
if (process.env.TARGET) {
module.exports = genConfig(process.env.TARGET)
} else {
exports.getBuild = genConfig
exports.getAllBuilds = () => Object.keys(builds).map(genConfig)
}
这里传入的TARGET,所以执行genConfig(‘web-full-dev’)函数。之后会获得vue.js文件,在html文件里面引用这个文件就能运行项目了。
运行原理
1.传入name(这里接收的值是web-full-dev)值给genConfig()函数;
2.genConfig()函数通过name值去获得builds对象里面的配置;
3.将获得的配置转换为rollup的配置;
4.打包成vue.js文件
resolve()补充说明
resolve()源码:
const resolve = p => {
const base = p.split('/')[0]
if (aliases[base]) {
return path.resolve(aliases[base], p.slice(base.length + 1))
} else {
return path.resolve(__dirname, '../', p)
}
}
解释:
假如p = 'web/entry-runtime-with-compiler.js'
:
1.获取“/”前面的“web”;
2.aliases对象:
{
vue: resolve('src/platforms/web/entry-runtime-with-compiler'),
compiler: resolve('src/compiler'),
core: resolve('src/core'),
shared: resolve('src/shared'),
web: resolve('src/platforms/web'),
weex: resolve('src/platforms/weex'),
server: resolve('src/server'),
entries: resolve('src/entries'),
sfc: resolve('src/sfc')
}
在aliases对象中返回vue构建目录,“web”对应的应该是“src/platforms/web”;
3.将目录与文件名拼接得到完整的路径并返回。
上一篇: VUE2.0中监听对象属性的方法
下一篇: vue路由监听_v1.0.0