full-esm
2023年07月21日
一、认识
Full
: 是一个全量的包,包含编译器(compiler
)和运行时(runtime
)。ES Module
: 现代 JavaScript
规范,ES Module
规范的包是给像 webpack 2
和 rollup
这样的现代打包器使用的。这些打包器默认使用仅包含运行时的 vue.runtime.esm.js
文件。因此, 我们可以通过 full-esm
的产物进行开发调试
二、编译配置
scripts/config.js
const builds = {
// Runtime only (CommonJS). Used by bundlers e.g. Webpack & Browserify
'runtime-cjs-dev': {
entry: resolve('web/entry-runtime.ts'),
dest: resolve('dist/vue.runtime.common.dev.js'),
format: 'cjs',
env: 'development',
banner
},
'runtime-cjs-prod': {
entry: resolve('web/entry-runtime.ts'),
dest: resolve('dist/vue.runtime.common.prod.js'),
format: 'cjs',
env: 'production',
banner
},
// Runtime+compiler CommonJS build (CommonJS)
'full-cjs-dev': {
entry: resolve('web/entry-runtime-with-compiler.ts'),
dest: resolve('dist/vue.common.dev.js'),
format: 'cjs',
env: 'development',
alias: { he: './entity-decoder' },
banner
},
'full-cjs-prod': {
entry: resolve('web/entry-runtime-with-compiler.ts'),
dest: resolve('dist/vue.common.prod.js'),
format: 'cjs',
env: 'production',
alias: { he: './entity-decoder' },
banner
},
// Runtime only ES modules build (for bundlers)
'runtime-esm': {
entry: resolve('web/entry-runtime-esm.ts'),
dest: resolve('dist/vue.runtime.esm.js'),
format: 'es',
banner
},
// Runtime+compiler ES modules build (for bundlers)
'full-esm': {
entry: resolve('web/entry-runtime-with-compiler-esm.ts'),
dest: resolve('dist/vue.esm.js'),
format: 'es',
alias: { he: './entity-decoder' },
banner
},
// Runtime+compiler ES modules build (for direct import in browser)
'full-esm-browser-dev': {
entry: resolve('web/entry-runtime-with-compiler-esm.ts'),
dest: resolve('dist/vue.esm.browser.js'),
format: 'es',
transpile: false,
env: 'development',
alias: { he: './entity-decoder' },
banner
},
// Runtime+compiler ES modules build (for direct import in browser)
'full-esm-browser-prod': {
entry: resolve('web/entry-runtime-with-compiler-esm.ts'),
dest: resolve('dist/vue.esm.browser.min.js'),
format: 'es',
transpile: false,
env: 'production',
alias: { he: './entity-decoder' },
banner
},
// runtime-only build (Browser)
'runtime-dev': {
entry: resolve('web/entry-runtime.ts'),
dest: resolve('dist/vue.runtime.js'),
format: 'umd',
env: 'development',
banner
},
// runtime-only production build (Browser)
'runtime-prod': {
entry: resolve('web/entry-runtime.ts'),
dest: resolve('dist/vue.runtime.min.js'),
format: 'umd',
env: 'production',
banner
},
// Runtime+compiler development build (Browser)
'full-dev': {
entry: resolve('web/entry-runtime-with-compiler.ts'),
dest: resolve('dist/vue.js'),
format: 'umd',
env: 'development',
alias: { he: './entity-decoder' },
banner
},
// Runtime+compiler production build (Browser)
'full-prod': {
entry: resolve('web/entry-runtime-with-compiler.ts'),
dest: resolve('dist/vue.min.js'),
format: 'umd',
env: 'production',
alias: { he: './entity-decoder' },
banner
},
// Web compiler (CommonJS).
compiler: {
entry: resolve('web/entry-compiler.ts'),
dest: resolve('packages/template-compiler/build.js'),
format: 'cjs',
external: Object.keys(
require('../packages/template-compiler/package.json').dependencies
)
},
// Web compiler (UMD for in-browser use).
'compiler-browser': {
entry: resolve('web/entry-compiler.ts'),
dest: resolve('packages/template-compiler/browser.js'),
format: 'umd',
env: 'development',
moduleName: 'VueTemplateCompiler',
plugins: [node(), cjs()]
},
// Web server renderer (CommonJS).
'server-renderer-dev': {
entry: resolve('packages/server-renderer/src/index.ts'),
dest: resolve('packages/server-renderer/build.dev.js'),
format: 'cjs',
env: 'development',
external: [
'stream',
...Object.keys(
require('../packages/server-renderer/package.json').dependencies
)
]
},
'server-renderer-prod': {
entry: resolve('server/index.ts'),
dest: resolve('packages/server-renderer/build.prod.js'),
format: 'cjs',
env: 'production',
external: [
'stream',
...Object.keys(
require('../packages/server-renderer/package.json').dependencies
)
]
},
'server-renderer-basic': {
entry: resolve('server/index-basic.ts'),
dest: resolve('packages/server-renderer/basic.js'),
format: 'umd',
env: 'development',
moduleName: 'renderVueComponentToString',
plugins: [node(), cjs()]
},
'server-renderer-webpack-server-plugin': {
entry: resolve('server/webpack-plugin/server.ts'),
dest: resolve('packages/server-renderer/server-plugin.js'),
format: 'cjs',
external: Object.keys(
require('../packages/server-renderer/package.json').dependencies
)
},
'server-renderer-webpack-client-plugin': {
entry: resolve('server/webpack-plugin/client.ts'),
dest: resolve('packages/server-renderer/client-plugin.js'),
format: 'cjs',
external: Object.keys(
require('../packages/server-renderer/package.json').dependencies
)
},
'compiler-sfc': {
entry: resolve('packages/compiler-sfc/src/index.ts'),
dest: resolve('packages/compiler-sfc/dist/compiler-sfc.js'),
format: 'cjs',
external: Object.keys(
require('../packages/compiler-sfc/package.json').dependencies
),
plugins: [
node({ preferBuiltins: true }),
cjs({
ignore: [
...Object.keys(require(consolidatePath).devDependencies),
'vm',
'crypto',
'react-dom/server',
'teacup/lib/express',
'arc-templates/dist/es5',
'then-pug',
'then-jade'
]
})
]
}
}
三、命令管理
3.1 配置编译产物
package.json
添加命令 rollup -w -c scripts/config.js --environment TARGET:full-esm
"scripts": {
"dev": "rollup -w -c scripts/config.js --environment TARGET:full-dev",
"dev:cjs": "rollup -w -c scripts/config.js --environment TARGET:runtime-cjs-dev",
"dev:esm": "rollup -w -c scripts/config.js --environment TARGET:runtime-esm",
"dev:ssr": "rollup -w -c scripts/config.js --environment TARGET:server-renderer",
"dev:compiler": "rollup -w -c scripts/config.js --environment TARGET:compiler ",
"build": "node scripts/build.js",
"build:ssr": "npm run build -- runtime-cjs,server-renderer",
"build:types": "rimraf temp && tsc --declaration --emitDeclarationOnly --outDir temp && api-extractor run && api-extractor run -c packages/compiler-sfc/api-extractor.json",
"test": "npm run ts-check && npm run test:types && npm run test:unit && npm run test:e2e && npm run test:ssr && npm run test:sfc",
"test:unit": "vitest run test/unit",
"test:ssr": "npm run build:ssr && vitest run server-renderer",
"test:sfc": "vitest run compiler-sfc",
"test:e2e": "npm run build -- full-prod,server-renderer-basic && vitest run test/e2e",
"test:transition": "karma start test/transition/karma.conf.js",
"test:types": "npm run build:types && tsc -p ./types/tsconfig.json",
"format": "prettier --write --parser typescript \"(src|test|packages|types)/**/*.ts\"",
"ts-check": "tsc -p tsconfig.json --noEmit",
"ts-check:test": "tsc -p test/tsconfig.json --noEmit",
"bench:ssr": "npm run build:ssr && node benchmarks/ssr/renderToString.js && node benchmarks/ssr/renderToStream.js",
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"dev:full-esm": "rollup -w -c scripts/config.js --environment TARGET:full-esm"
}
3.2 开启 SourceMap
在 package.json -> scripts
中的 dev
命令中添加 --sourcemap
,这样就可以在浏览器中调试源码时查看当前代码在源码中的位置。
"scripts": {
"dev": "rollup -w -c scripts/config.js --environment TARGET:full-dev",
"dev:cjs": "rollup -w -c scripts/config.js --environment TARGET:runtime-cjs-dev",
"dev:esm": "rollup -w -c scripts/config.js --environment TARGET:runtime-esm",
"dev:ssr": "rollup -w -c scripts/config.js --environment TARGET:server-renderer",
"dev:compiler": "rollup -w -c scripts/config.js --environment TARGET:compiler ",
"build": "node scripts/build.js",
"build:ssr": "npm run build -- runtime-cjs,server-renderer",
"build:types": "rimraf temp && tsc --declaration --emitDeclarationOnly --outDir temp && api-extractor run && api-extractor run -c packages/compiler-sfc/api-extractor.json",
"test": "npm run ts-check && npm run test:types && npm run test:unit && npm run test:e2e && npm run test:ssr && npm run test:sfc",
"test:unit": "vitest run test/unit",
"test:ssr": "npm run build:ssr && vitest run server-renderer",
"test:sfc": "vitest run compiler-sfc",
"test:e2e": "npm run build -- full-prod,server-renderer-basic && vitest run test/e2e",
"test:transition": "karma start test/transition/karma.conf.js",
"test:types": "npm run build:types && tsc -p ./types/tsconfig.json",
"format": "prettier --write --parser typescript \"(src|test|packages|types)/**/*.ts\"",
"ts-check": "tsc -p tsconfig.json --noEmit",
"ts-check:test": "tsc -p test/tsconfig.json --noEmit",
"bench:ssr": "npm run build:ssr && node benchmarks/ssr/renderToString.js && node benchmarks/ssr/renderToStream.js",
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"dev:full-esm": "rollup -w -c scripts/config.js --sourcemap --environment TARGET:full-esm"
}
3.3 运行命令,编译产物
执行 dev:full-esm
生成 Vue
产物。 产物路径为: dist/vue.esm.js
四、运行调试
在 examples
创建 html
文件调试。引入 Vue
产物时, 我们可以使用导入映射表 (Import Maps
)来告诉浏览器如何定位到导入的 vue
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vue 2.0</title>
<script type="importmap">
{
"imports": {
"vue": "../dist/vue.esm.js"
}
}
</script>
<script>
var process = {
env: {
NODE_ENV: 'development'
}
}
</script>
</head>
<body>
<div id="app">{{ msg }}</div>
<script type="module">
import Vue from 'vue'
const app = new Vue({
el: '#app',
data: {
msg: 'Hello World'
},
mounted() {
setTimeout(() => {
this.msg = 'Hello World Update'
}, 2000)
}
})
</script>
</body>
</html>