Vue3 功能拆解⑧ script setup 来龙去脉
文章目录
诗号:六道同坠,魔劫万千,引渡如来。
本文将从源码,编译前后让你明白 <script setup> 标签内如何正确的书写代码,及每个语 法背后的原理又是什么?
本文涉及的源码包: compiler-sfc, compiler-core。
SFC <script setup> 的编译相关代码在 packages/compiler-sfc/src/compileScript.ts
使用到的第三方插件 : @babel/parser->parse, magic-string, estree-walker
script setup
插件使用测试
@babel/parser:
|
|
>>> foo 变量声明 AST Node { type: 'VariableDeclarator', start: 7, end: 16, loc: SourceLocation { start: Position { line: 2, column: 6 }, end: Position { line: 2, column: 15 }, filename: undefined, identifierName: undefined }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: undefined, id: Node { type: 'Identifier', start: 7, end: 10, loc: SourceLocation { start: [Position], end: [Position], filename: undefined, identifierName: 'foo' }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: undefined, name: 'foo' }, init: Node { type: 'NumericLiteral', start: 13, end: 16, loc: SourceLocation { start: [Position], end: [Position], filename: undefined, identifierName: undefined }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: { rawValue: 100, raw: '100' }, value: 100 } } >>> baz 函数声明 AST Node { type: 'FunctionDeclaration', start: 29, end: 72, loc: SourceLocation { start: Position { line: 3, column: 0 }, end: Position { line: 5, column: 1 }, filename: undefined, identifierName: undefined }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: undefined, id: Node { type: 'Identifier', start: 38, end: 41, loc: SourceLocation { start: [Position], end: [Position], filename: undefined, identifierName: 'baz' }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: undefined, name: 'baz' }, generator: false, async: false, params: [], body: Node { type: 'BlockStatement', start: 44, end: 72, loc: SourceLocation { start: [Position], end: [Position], filename: undefined, identifierName: undefined }, range: undefined, leadingComments: undefined, trailingComments: undefined, innerComments: undefined, extra: undefined, body: [ [Node] ], directives: [] } } 0
magic-string
对字符串进行增删改查的各种操作, vue3 中使用该插件来替换编译之后的 ast code。
|
|
替换变量名 -> answer = 99 替换变量值 -> answer = 42 前后加内容 -> var answer = 42; 生产 source map -> {"version":3,"file":"converted.js.map","sources":["source.js"],"sourcesContent":["problems = 99"],"names":[],"mappings":"IAAA,MAAQ,GAAG"} undefined
estree-walker
一个遍历 ast 的插件。
|
|
#+RESULTS: