@dcloudio/uni-mp-compiler 0.0.1-nvue3.3030820220125001

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +202 -0
  2. package/dist/ast.d.ts +16 -0
  3. package/dist/ast.js +91 -0
  4. package/dist/codegen.d.ts +14 -0
  5. package/dist/codegen.js +322 -0
  6. package/dist/compile.d.ts +13 -0
  7. package/dist/compile.js +108 -0
  8. package/dist/decodeHtml.d.ts +2 -0
  9. package/dist/decodeHtml.js +131 -0
  10. package/dist/errors.d.ts +16 -0
  11. package/dist/errors.js +19 -0
  12. package/dist/identifier.d.ts +7 -0
  13. package/dist/identifier.js +35 -0
  14. package/dist/index.d.ts +12 -0
  15. package/dist/index.js +40 -0
  16. package/dist/namedChars.json +2233 -0
  17. package/dist/options.d.ts +88 -0
  18. package/dist/options.js +2 -0
  19. package/dist/parserOptions.d.ts +7 -0
  20. package/dist/parserOptions.js +63 -0
  21. package/dist/runtimeHelpers.d.ts +13 -0
  22. package/dist/runtimeHelpers.js +32 -0
  23. package/dist/template/codegen.d.ts +19 -0
  24. package/dist/template/codegen.js +381 -0
  25. package/dist/transform.d.ts +73 -0
  26. package/dist/transform.js +518 -0
  27. package/dist/transforms/transformClass.d.ts +5 -0
  28. package/dist/transforms/transformClass.js +162 -0
  29. package/dist/transforms/transformComponent.d.ts +11 -0
  30. package/dist/transforms/transformComponent.js +179 -0
  31. package/dist/transforms/transformElement.d.ts +9 -0
  32. package/dist/transforms/transformElement.js +191 -0
  33. package/dist/transforms/transformExpression.d.ts +5 -0
  34. package/dist/transforms/transformExpression.js +283 -0
  35. package/dist/transforms/transformIdentifier.d.ts +2 -0
  36. package/dist/transforms/transformIdentifier.js +95 -0
  37. package/dist/transforms/transformKey.d.ts +4 -0
  38. package/dist/transforms/transformKey.js +18 -0
  39. package/dist/transforms/transformRef.d.ts +3 -0
  40. package/dist/transforms/transformRef.js +126 -0
  41. package/dist/transforms/transformRoot.d.ts +2 -0
  42. package/dist/transforms/transformRoot.js +42 -0
  43. package/dist/transforms/transformSlot.d.ts +3 -0
  44. package/dist/transforms/transformSlot.js +144 -0
  45. package/dist/transforms/transformStyle.d.ts +5 -0
  46. package/dist/transforms/transformStyle.js +156 -0
  47. package/dist/transforms/transformTag.d.ts +2 -0
  48. package/dist/transforms/transformTag.js +15 -0
  49. package/dist/transforms/utils.d.ts +25 -0
  50. package/dist/transforms/utils.js +133 -0
  51. package/dist/transforms/vBind.d.ts +2 -0
  52. package/dist/transforms/vBind.js +49 -0
  53. package/dist/transforms/vFor.d.ts +35 -0
  54. package/dist/transforms/vFor.js +299 -0
  55. package/dist/transforms/vHtml.d.ts +2 -0
  56. package/dist/transforms/vHtml.js +35 -0
  57. package/dist/transforms/vIf.d.ts +13 -0
  58. package/dist/transforms/vIf.js +139 -0
  59. package/dist/transforms/vModel.d.ts +2 -0
  60. package/dist/transforms/vModel.js +178 -0
  61. package/dist/transforms/vOn.d.ts +8 -0
  62. package/dist/transforms/vOn.js +166 -0
  63. package/dist/transforms/vSlot.d.ts +16 -0
  64. package/dist/transforms/vSlot.js +274 -0
  65. package/package.json +36 -0
@@ -0,0 +1,88 @@
1
+ import { ParserPlugin } from '@babel/parser';
2
+ import { GeneratorOptions } from '@babel/generator';
3
+ import { CallExpression, Expression, ObjectExpression, ObjectProperty, SpreadElement } from '@babel/types';
4
+ import { NodeTransform as VueNodeTransform, DirectiveTransform as VueDirectiveTransform } from '@vue/compiler-core';
5
+ import { MiniProgramCompilerOptions } from '@dcloudio/uni-cli-shared';
6
+ import { BindingMetadata, CompilerError, RootNode } from '@vue/compiler-core';
7
+ import IdentifierGenerator from './identifier';
8
+ import { TransformContext } from './transform';
9
+ import { VForOptions } from './transforms/vFor';
10
+ export interface CodegenRootNode extends RootNode {
11
+ renderData: ObjectExpression | CallExpression;
12
+ bindingComponents: TransformContext['bindingComponents'];
13
+ }
14
+ export interface ErrorHandlingOptions {
15
+ onWarn?: (warning: CompilerError) => void;
16
+ onError?: (error: CompilerError) => void;
17
+ }
18
+ interface ParserOptions {
19
+ /**
20
+ * e.g. platform native elements, e.g. `<div>` for browsers
21
+ */
22
+ isNativeTag?: (tag: string) => boolean;
23
+ /**
24
+ * e.g. native elements that can self-close, e.g. `<img>`, `<br>`, `<hr>`
25
+ */
26
+ isVoidTag?: (tag: string) => boolean;
27
+ /**
28
+ * Separate option for end users to extend the native elements list
29
+ */
30
+ isCustomElement?: (tag: string) => boolean | void;
31
+ }
32
+ interface SharedTransformCodegenOptions {
33
+ inline?: boolean;
34
+ isTS?: boolean;
35
+ root?: string;
36
+ filename?: string;
37
+ bindingMetadata?: BindingMetadata;
38
+ prefixIdentifiers?: boolean;
39
+ miniProgram?: MiniProgramCompilerOptions;
40
+ }
41
+ export interface TransformOptions extends SharedTransformCodegenOptions, ErrorHandlingOptions {
42
+ hashId?: string | null;
43
+ scopeId?: string | null;
44
+ filters?: string[];
45
+ renderDataSpread?: boolean;
46
+ cacheHandlers?: boolean;
47
+ nodeTransforms?: VueNodeTransform[];
48
+ directiveTransforms?: Record<string, VueDirectiveTransform | undefined>;
49
+ isBuiltInComponent?: (tag: string) => symbol | void;
50
+ isCustomElement?: (tag: string) => boolean | void;
51
+ expressionPlugins?: ParserPlugin[];
52
+ skipTransformIdentifier?: boolean;
53
+ bindingCssVars?: string[];
54
+ }
55
+ export interface CodegenRootScope {
56
+ id: IdentifierGenerator;
57
+ identifiers: string[];
58
+ properties: (ObjectProperty | SpreadElement)[];
59
+ parent: CodegenScope | null;
60
+ }
61
+ export interface CodegenVIfScopeInit {
62
+ name: 'if' | 'else-if' | 'else' | string;
63
+ condition?: Expression;
64
+ }
65
+ export declare type CodegenVForScopeInit = VForOptions & {
66
+ locals: string[];
67
+ };
68
+ export interface CodegenVIfScope extends CodegenRootScope, CodegenVIfScopeInit {
69
+ parentScope: CodegenRootScope | CodegenVForScope;
70
+ }
71
+ export interface CodegenVForScope extends CodegenRootScope, CodegenVForScopeInit {
72
+ }
73
+ export declare type CodegenScope = CodegenRootScope | CodegenVIfScope | CodegenVForScope;
74
+ export interface CodegenOptions extends SharedTransformCodegenOptions {
75
+ mode?: 'module' | 'function';
76
+ scopeId?: string | null;
77
+ sourceMap?: boolean;
78
+ runtimeModuleName?: string;
79
+ runtimeGlobalName?: string;
80
+ generatorOpts?: GeneratorOptions;
81
+ }
82
+ export interface TemplateCodegenOptions extends Omit<MiniProgramCompilerOptions, 'filter'> {
83
+ scopeId?: string | null;
84
+ filename: string;
85
+ isBuiltInComponent: Required<TransformOptions>['isBuiltInComponent'];
86
+ }
87
+ export declare type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions;
88
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { ParserOptions } from '@vue/compiler-core';
2
+ export declare const enum DOMNamespaces {
3
+ HTML = 0,
4
+ SVG = 1,
5
+ MATH_ML = 2
6
+ }
7
+ export declare const parserOptions: ParserOptions;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parserOptions = void 0;
4
+ const shared_1 = require("@vue/shared");
5
+ const decodeHtml_1 = require("./decodeHtml");
6
+ const isRawTextContainer = /*#__PURE__*/ (0, shared_1.makeMap)('style,iframe,script,noscript', true);
7
+ exports.parserOptions = {
8
+ isVoidTag: shared_1.isVoidTag,
9
+ isNativeTag: (tag) => (0, shared_1.isHTMLTag)(tag) || (0, shared_1.isSVGTag)(tag),
10
+ isPreTag: (tag) => tag === 'pre',
11
+ decodeEntities: decodeHtml_1.decodeHtml,
12
+ // https://html.spec.whatwg.org/multipage/parsing.html#tree-construction-dispatcher
13
+ getNamespace(tag, parent) {
14
+ let ns = parent ? parent.ns : 0 /* HTML */;
15
+ if (parent && ns === 2 /* MATH_ML */) {
16
+ if (parent.tag === 'annotation-xml') {
17
+ if (tag === 'svg') {
18
+ return 1 /* SVG */;
19
+ }
20
+ if (parent.props.some((a) => a.type === 6 /* ATTRIBUTE */ &&
21
+ a.name === 'encoding' &&
22
+ a.value != null &&
23
+ (a.value.content === 'text/html' ||
24
+ a.value.content === 'application/xhtml+xml'))) {
25
+ ns = 0 /* HTML */;
26
+ }
27
+ }
28
+ else if (/^m(?:[ions]|text)$/.test(parent.tag) &&
29
+ tag !== 'mglyph' &&
30
+ tag !== 'malignmark') {
31
+ ns = 0 /* HTML */;
32
+ }
33
+ }
34
+ else if (parent && ns === 1 /* SVG */) {
35
+ if (parent.tag === 'foreignObject' ||
36
+ parent.tag === 'desc' ||
37
+ parent.tag === 'title') {
38
+ ns = 0 /* HTML */;
39
+ }
40
+ }
41
+ if (ns === 0 /* HTML */) {
42
+ if (tag === 'svg') {
43
+ return 1 /* SVG */;
44
+ }
45
+ if (tag === 'math') {
46
+ return 2 /* MATH_ML */;
47
+ }
48
+ }
49
+ return ns;
50
+ },
51
+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments
52
+ getTextMode({ tag, ns }) {
53
+ if (ns === 0 /* HTML */) {
54
+ if (tag === 'textarea' || tag === 'title') {
55
+ return 1 /* RCDATA */;
56
+ }
57
+ if (isRawTextContainer(tag)) {
58
+ return 2 /* RAWTEXT */;
59
+ }
60
+ }
61
+ return 0 /* DATA */;
62
+ },
63
+ };
@@ -0,0 +1,13 @@
1
+ export declare const V_ON: unique symbol;
2
+ export declare const V_FOR: unique symbol;
3
+ export declare const EXTEND: unique symbol;
4
+ export declare const SET_REF: unique symbol;
5
+ export declare const CAMELIZE: unique symbol;
6
+ export declare const HYPHENATE: unique symbol;
7
+ export declare const RENDER_PROPS: unique symbol;
8
+ export declare const RENDER_SLOT: unique symbol;
9
+ export declare const DYNAMIC_SLOT: unique symbol;
10
+ export declare const WITH_SCOPED_SLOT: unique symbol;
11
+ export declare const STRINGIFY_STYLE: unique symbol;
12
+ export declare const NORMALIZE_CLASS: unique symbol;
13
+ export declare const TO_DISPLAY_STRING: unique symbol;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TO_DISPLAY_STRING = exports.NORMALIZE_CLASS = exports.STRINGIFY_STYLE = exports.WITH_SCOPED_SLOT = exports.DYNAMIC_SLOT = exports.RENDER_SLOT = exports.RENDER_PROPS = exports.HYPHENATE = exports.CAMELIZE = exports.SET_REF = exports.EXTEND = exports.V_FOR = exports.V_ON = void 0;
4
+ const compiler_core_1 = require("@vue/compiler-core");
5
+ exports.V_ON = Symbol(`vOn`);
6
+ exports.V_FOR = Symbol(`vFor`);
7
+ exports.EXTEND = Symbol(`extend`);
8
+ exports.SET_REF = Symbol(`setRef`);
9
+ exports.CAMELIZE = Symbol(`camelize`);
10
+ exports.HYPHENATE = Symbol(`hyphenate`);
11
+ exports.RENDER_PROPS = Symbol(`renderProps`);
12
+ exports.RENDER_SLOT = Symbol(`renderSlot`);
13
+ exports.DYNAMIC_SLOT = Symbol(`dynamicSlot`);
14
+ exports.WITH_SCOPED_SLOT = Symbol(`withScopedSlot`);
15
+ exports.STRINGIFY_STYLE = Symbol(`stringifyStyle`);
16
+ exports.NORMALIZE_CLASS = Symbol(`normalizeClass`);
17
+ exports.TO_DISPLAY_STRING = Symbol(`toDisplayString`);
18
+ (0, compiler_core_1.registerRuntimeHelpers)({
19
+ [exports.V_ON]: 'o',
20
+ [exports.V_FOR]: 'f',
21
+ [exports.EXTEND]: 'e',
22
+ [exports.SET_REF]: 'sr',
23
+ [exports.CAMELIZE]: 'c',
24
+ [exports.HYPHENATE]: 'h',
25
+ [exports.RENDER_PROPS]: 'p',
26
+ [exports.RENDER_SLOT]: 'r',
27
+ [exports.DYNAMIC_SLOT]: 'd',
28
+ [exports.WITH_SCOPED_SLOT]: 'w',
29
+ [exports.STRINGIFY_STYLE]: 's',
30
+ [exports.NORMALIZE_CLASS]: 'n',
31
+ [exports.TO_DISPLAY_STRING]: `t`,
32
+ });
@@ -0,0 +1,19 @@
1
+ import { MiniProgramCompilerOptions } from '@dcloudio/uni-cli-shared';
2
+ import { ElementNode, RootNode, TemplateChildNode } from '@vue/compiler-core';
3
+ import { TemplateCodegenOptions } from '../options';
4
+ import { TransformContext } from '../transform';
5
+ interface TemplateCodegenContext {
6
+ code: string;
7
+ directive: string;
8
+ scopeId?: string | null;
9
+ event: MiniProgramCompilerOptions['event'];
10
+ slot: MiniProgramCompilerOptions['slot'];
11
+ lazyElement: MiniProgramCompilerOptions['lazyElement'];
12
+ component: MiniProgramCompilerOptions['component'];
13
+ isBuiltInComponent: TransformContext['isBuiltInComponent'];
14
+ push(code: string): void;
15
+ }
16
+ export declare function generate({ children }: RootNode, { slot, event, scopeId, emitFile, filename, directive, lazyElement, isBuiltInComponent, component, }: TemplateCodegenOptions): void;
17
+ export declare function genNode(node: TemplateChildNode, context: TemplateCodegenContext): void;
18
+ export declare function genElementProps(node: ElementNode, context: TemplateCodegenContext): void;
19
+ export {};
@@ -0,0 +1,381 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.genElementProps = exports.genNode = exports.generate = void 0;
4
+ const shared_1 = require("@vue/shared");
5
+ const uni_shared_1 = require("@dcloudio/uni-shared");
6
+ const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
7
+ const compiler_core_1 = require("@vue/compiler-core");
8
+ const codegen_1 = require("../codegen");
9
+ const vFor_1 = require("../transforms/vFor");
10
+ const vIf_1 = require("../transforms/vIf");
11
+ const vSlot_1 = require("../transforms/vSlot");
12
+ const utils_1 = require("../transforms/utils");
13
+ function generate({ children }, { slot, event, scopeId, emitFile, filename, directive, lazyElement, isBuiltInComponent, component, }) {
14
+ const context = {
15
+ slot,
16
+ event,
17
+ code: '',
18
+ scopeId,
19
+ directive,
20
+ lazyElement,
21
+ component,
22
+ isBuiltInComponent,
23
+ push(code) {
24
+ context.code += code;
25
+ },
26
+ };
27
+ children.forEach((node) => {
28
+ genNode(node, context);
29
+ });
30
+ emitFile({ type: 'asset', fileName: filename, source: context.code });
31
+ }
32
+ exports.generate = generate;
33
+ function genNode(node, context) {
34
+ switch (node.type) {
35
+ case 9 /* IF */:
36
+ return node.branches.forEach((node) => {
37
+ genNode(node, context);
38
+ });
39
+ case 2 /* TEXT */:
40
+ return genText(node, context);
41
+ case 5 /* INTERPOLATION */:
42
+ return genExpression(node.content, context);
43
+ case 1 /* ELEMENT */:
44
+ if (node.tagType === 2 /* SLOT */) {
45
+ return genSlot(node, context);
46
+ }
47
+ else if (node.tagType === 1 /* COMPONENT */) {
48
+ return genComponent(node, context);
49
+ }
50
+ else if (node.tagType === 3 /* TEMPLATE */) {
51
+ return genTemplate(node, context);
52
+ }
53
+ else if (isLazyElement(node, context)) {
54
+ return genLazyElement(node, context);
55
+ }
56
+ return genElement(node, context);
57
+ }
58
+ }
59
+ exports.genNode = genNode;
60
+ function genText(node, { push }) {
61
+ push(node.content);
62
+ }
63
+ function genExpression(node, { push }) {
64
+ push(`{{${(0, codegen_1.genExpr)(node)}}}`);
65
+ }
66
+ function genVIf(exp, { push, directive }) {
67
+ push(` ${directive}if="{{${exp}}}"`);
68
+ }
69
+ function genVElseIf(exp, { push, directive }) {
70
+ push(` ${directive}elif="{{${exp}}}"`);
71
+ }
72
+ function genVElse({ push, directive }) {
73
+ push(` ${directive}else`);
74
+ }
75
+ function genVFor(node, { push, directive }) {
76
+ const { sourceCode, valueAlias, indexAlias } = node.vFor;
77
+ push(` ${directive}for="${sourceCode}"`);
78
+ if (valueAlias) {
79
+ push(` ${directive}for-item="${valueAlias}"`);
80
+ }
81
+ if (valueAlias === 'index') {
82
+ push(` ${directive}for-index="${indexAlias}"`);
83
+ }
84
+ const keyProp = (0, compiler_core_1.findProp)(node, 'key', true);
85
+ if (keyProp) {
86
+ const key = keyProp.exp.content;
87
+ push(` ${directive}key="${key.includes('.') ? key.split('.')[1] : key}"`);
88
+ node.props.splice(node.props.indexOf(keyProp), 1);
89
+ }
90
+ }
91
+ function genSlot(node, context) {
92
+ var _a;
93
+ // 移除掉所有非name属性,即移除作用域插槽的绑定指令
94
+ node.props = node.props.filter((prop) => {
95
+ var _a;
96
+ if (prop.type === 6 /* ATTRIBUTE */) {
97
+ return prop.name === 'name';
98
+ }
99
+ else if (((_a = prop.arg) === null || _a === void 0 ? void 0 : _a.type) === 4 /* SIMPLE_EXPRESSION */) {
100
+ return prop.arg.content === 'name';
101
+ }
102
+ });
103
+ if (!node.children.length || context.slot.fallbackContent) {
104
+ // 无后备内容或支持后备内容
105
+ return genElement(node, context);
106
+ }
107
+ const { push } = context;
108
+ const isVIfSlot = (0, vIf_1.isIfElementNode)(node);
109
+ if (isVIfSlot) {
110
+ push(`<block`);
111
+ genVIfCode(node, context);
112
+ push(`>`);
113
+ delete node.vIf;
114
+ }
115
+ const children = node.children.slice();
116
+ node.children.length = 0;
117
+ push(`<block`);
118
+ const nameProp = (0, compiler_core_1.findProp)(node, 'name');
119
+ genVIf(`$slots.` +
120
+ ((nameProp === null || nameProp === void 0 ? void 0 : nameProp.type) === 6 /* ATTRIBUTE */ && ((_a = nameProp.value) === null || _a === void 0 ? void 0 : _a.content)
121
+ ? nameProp.value.content
122
+ : uni_shared_1.SLOT_DEFAULT_NAME), context);
123
+ push(`>`);
124
+ genElement(node, context);
125
+ push(`</block>`);
126
+ push(`<block`);
127
+ genVElse(context);
128
+ push(`>`);
129
+ children.forEach((node) => {
130
+ genNode(node, context);
131
+ });
132
+ push(`</block>`);
133
+ if (isVIfSlot) {
134
+ push(`</block>`);
135
+ }
136
+ }
137
+ function genTemplate(node, context) {
138
+ const slotProp = node.props.find((prop) => {
139
+ var _a;
140
+ return prop.type === 7 /* DIRECTIVE */ &&
141
+ (prop.name === 'slot' ||
142
+ (prop.name === 'bind' &&
143
+ ((_a = prop.arg) === null || _a === void 0 ? void 0 : _a.type) === 4 /* SIMPLE_EXPRESSION */ &&
144
+ prop.arg.content === 'slot'));
145
+ });
146
+ // 为 bind 时,通常是作用域插槽生成的 vSlot.ts:197 createBindDirectiveNode('slot',...)
147
+ if (slotProp && (slotProp.name === 'bind' || (0, vSlot_1.findSlotName)(slotProp))) {
148
+ /**
149
+ * 仅百度、字节支持使用 block 作为命名插槽根节点
150
+ * 此处为了统一仅默认替换为view
151
+ * <template v-slot/> => <view slot="">
152
+ */
153
+ node.tag = 'view';
154
+ }
155
+ else {
156
+ // <template/> => <block/>
157
+ node.tag = 'block';
158
+ }
159
+ // @ts-ignore
160
+ node.tagType = 0 /* ELEMENT */;
161
+ // 仅单个子节点的命名插槽(非作用域),直接使用子节点作为插槽使用,避免多增加的 view 节点影响 flex 排版
162
+ if (slotProp &&
163
+ node.tag === 'view' &&
164
+ !(0, vFor_1.isForElementNode)(node) &&
165
+ node.children.length === 1) {
166
+ const child = node.children[0];
167
+ if ((0, uni_cli_shared_1.isElementNode)(child) &&
168
+ !(0, vFor_1.isForElementNode)(child) &&
169
+ !(0, compiler_core_1.isSlotOutlet)(child)) {
170
+ child.props.push(slotProp);
171
+ return genElement(child, context);
172
+ }
173
+ }
174
+ return genElement(node, context);
175
+ }
176
+ function genComponent(node, context) {
177
+ var _a;
178
+ if ((_a = context.component) === null || _a === void 0 ? void 0 : _a.getPropertySync) {
179
+ return genElement(node, context);
180
+ }
181
+ if ((0, vIf_1.isIfElementNode)(node) || (0, vFor_1.isForElementNode)(node)) {
182
+ return genElement(node, context);
183
+ }
184
+ const prop = (0, compiler_core_1.findProp)(node, utils_1.ATTR_VUE_PROPS);
185
+ if (!prop) {
186
+ return genElement(node, context);
187
+ }
188
+ ;
189
+ node.vIf = {
190
+ name: 'if',
191
+ condition: prop.exp.content,
192
+ };
193
+ return genElement(node, context);
194
+ }
195
+ function isLazyElement(node, context) {
196
+ if (!context.lazyElement) {
197
+ return false;
198
+ }
199
+ const lazyProps = context.lazyElement[node.tag];
200
+ if (!lazyProps) {
201
+ return;
202
+ }
203
+ return node.props.some((prop) => prop.type === 7 /* DIRECTIVE */ &&
204
+ lazyProps.find((lazyProp) => {
205
+ var _a;
206
+ return (prop.name === lazyProp.name &&
207
+ ((_a = prop.arg) === null || _a === void 0 ? void 0 : _a.type) === 4 /* SIMPLE_EXPRESSION */ &&
208
+ lazyProp.arg.includes(prop.arg.content));
209
+ }));
210
+ }
211
+ /**
212
+ * 部分内置组件的部分事件在初始化时会立刻触发,但标准事件需要等首次渲染才能确认事件函数,故增加wx:if="{{r0}}"
213
+ * @param node
214
+ * @param context
215
+ */
216
+ function genLazyElement(node, context) {
217
+ const { push } = context;
218
+ if (!(0, vIf_1.isIfElementNode)(node)) {
219
+ push(`<block`);
220
+ // r0 => ready 首次渲染
221
+ genVIf(`r0`, context);
222
+ push(`>`);
223
+ genElement(node, context);
224
+ push(`</block>`);
225
+ return;
226
+ }
227
+ // v-if,v-else-if 无需处理
228
+ if (node.vIf.name !== 'else') {
229
+ return genElement(node, context);
230
+ }
231
+ push(`<block`);
232
+ genVElse(context);
233
+ push(`>`);
234
+ node.vIf.name = 'if';
235
+ node.vIf.condition = 'r0';
236
+ genElement(node, context);
237
+ push(`</block>`);
238
+ }
239
+ function genVIfCode(node, context) {
240
+ const { name, condition } = node.vIf;
241
+ if (name === 'if') {
242
+ genVIf(condition, context);
243
+ }
244
+ else if (name === 'else-if') {
245
+ genVElseIf(condition, context);
246
+ }
247
+ else if (name === 'else') {
248
+ genVElse(context);
249
+ }
250
+ }
251
+ function genElement(node, context) {
252
+ const { children, isSelfClosing, props } = node;
253
+ let tag = node.tag;
254
+ // <template slot="left"/> => <block slot="left"/>
255
+ if (tag === 'template') {
256
+ if ((0, compiler_core_1.findProp)(node, 'slot')) {
257
+ tag = 'view';
258
+ }
259
+ else {
260
+ tag = 'block';
261
+ }
262
+ }
263
+ // 无用的 block
264
+ if (tag === 'block' &&
265
+ props.length === 0 &&
266
+ !(0, vIf_1.isIfElementNode)(node) &&
267
+ !(0, vFor_1.isForElementNode)(node)) {
268
+ return children.forEach((node) => {
269
+ genNode(node, context);
270
+ });
271
+ }
272
+ if ((0, uni_cli_shared_1.isUserComponent)(node, context)) {
273
+ tag = (0, shared_1.hyphenate)(tag);
274
+ }
275
+ const { push } = context;
276
+ const hasVIf = (0, vIf_1.isIfElementNode)(node);
277
+ const hasVFor = (0, vFor_1.isForElementNode)(node);
278
+ const hasVIfAndVFor = hasVIf && hasVFor;
279
+ // 小程序中 wx:else wx:elif 不支持与 wx:for 同时使用
280
+ // 故 if 需要补充一层 block
281
+ if (hasVIfAndVFor) {
282
+ push(`<block`);
283
+ genVIfCode(node, context);
284
+ push(`>`);
285
+ }
286
+ push(`<${tag}`);
287
+ if (!hasVIfAndVFor && hasVIf) {
288
+ genVIfCode(node, context);
289
+ }
290
+ if (hasVFor) {
291
+ genVFor(node, context);
292
+ }
293
+ if (props.length) {
294
+ genElementProps(node, context);
295
+ }
296
+ if (isSelfClosing) {
297
+ push(`/>`);
298
+ }
299
+ else {
300
+ push(`>`);
301
+ children.forEach((node) => {
302
+ genNode(node, context);
303
+ });
304
+ push(`</${tag}>`);
305
+ }
306
+ if (hasVIfAndVFor) {
307
+ push(`</block>`);
308
+ }
309
+ }
310
+ function genElementProps(node, context) {
311
+ node.props.forEach((prop) => {
312
+ if (prop.type === 6 /* ATTRIBUTE */) {
313
+ const { value } = prop;
314
+ if (value) {
315
+ context.push(` ${prop.name}="${value.content}"`);
316
+ }
317
+ else {
318
+ context.push(` ${prop.name}`);
319
+ }
320
+ }
321
+ else {
322
+ const { name } = prop;
323
+ if (name === 'on') {
324
+ genOn(prop, node, context);
325
+ }
326
+ else {
327
+ genDirectiveNode(prop, node, context);
328
+ }
329
+ }
330
+ });
331
+ }
332
+ exports.genElementProps = genElementProps;
333
+ function genOn(prop, node, { push, event, isBuiltInComponent }) {
334
+ const arg = prop.arg.content;
335
+ const exp = prop.exp;
336
+ const modifiers = prop.modifiers;
337
+ const name = ((event === null || event === void 0 ? void 0 : event.format) || uni_cli_shared_1.formatMiniProgramEvent)(arg, {
338
+ isCatch: modifiers.includes('stop') || modifiers.includes('prevent'),
339
+ isCapture: modifiers.includes('capture'),
340
+ isComponent: (0, uni_cli_shared_1.isUserComponent)(node, { isBuiltInComponent }),
341
+ });
342
+ if (exp.isStatic) {
343
+ push(` ${name}="${exp.content}"`);
344
+ }
345
+ else {
346
+ push(` ${name}="{{${exp.content}}}"`);
347
+ }
348
+ }
349
+ function genDirectiveNode(prop, node, context) {
350
+ const { push, component } = context;
351
+ if (prop.name === 'slot') {
352
+ if (prop.arg) {
353
+ const arg = prop.arg;
354
+ if (arg.isStatic) {
355
+ const slotName = (0, uni_shared_1.dynamicSlotName)(arg.content);
356
+ // 非作用域默认插槽不生成 slot 属性
357
+ if (slotName !== uni_shared_1.SLOT_DEFAULT_NAME) {
358
+ push(` slot="${slotName}"`);
359
+ }
360
+ }
361
+ else {
362
+ push(` slot="{{${arg.content}}}"`);
363
+ }
364
+ }
365
+ }
366
+ else if (prop.name === 'show') {
367
+ let hiddenPropName = 'hidden';
368
+ if ((0, uni_cli_shared_1.isUserComponent)(node, context) && component && component.vShow) {
369
+ hiddenPropName = component.vShow;
370
+ }
371
+ push(` ${hiddenPropName}="{{!${prop.exp.content}}}"`);
372
+ }
373
+ else if (prop.arg && prop.exp) {
374
+ const arg = prop.arg.content;
375
+ const exp = prop.exp.content;
376
+ push(` ${arg}="{{${exp}}}"`);
377
+ }
378
+ else {
379
+ throw new Error(`unknown directive` + JSON.stringify(prop));
380
+ }
381
+ }
@@ -0,0 +1,73 @@
1
+ import { DirectiveNode, ElementNode, Property, RootNode, ParentNode, TemplateChildNode, CompilerError, ExpressionNode, JSChildNode, CacheExpression } from '@vue/compiler-core';
2
+ import { CodegenRootNode, CodegenRootScope, CodegenScope, CodegenVForScope, CodegenVForScopeInit, CodegenVIfScope, CodegenVIfScopeInit, TransformOptions } from './options';
3
+ export interface ImportItem {
4
+ exp: string | ExpressionNode;
5
+ path: string;
6
+ }
7
+ export declare type NodeTransform = (node: RootNode | TemplateChildNode, context: TransformContext) => void | (() => void) | (() => void)[];
8
+ export declare type DirectiveTransform = (dir: DirectiveNode, node: ElementNode, context: TransformContext, augmentor?: (ret: DirectiveTransformResult) => DirectiveTransformResult) => DirectiveTransformResult;
9
+ interface DirectiveTransformResult {
10
+ props: Property[];
11
+ needRuntime?: boolean | symbol;
12
+ }
13
+ export interface ErrorHandlingOptions {
14
+ onWarn?: (warning: CompilerError) => void;
15
+ onError?: (error: CompilerError) => void;
16
+ }
17
+ export declare const enum BindingComponentTypes {
18
+ SELF = "self",
19
+ SETUP = "setup",
20
+ UNKNOWN = "unknown"
21
+ }
22
+ export interface TransformContext extends Required<Omit<TransformOptions, 'filename' | 'root'>> {
23
+ selfName: string | null;
24
+ currentNode: RootNode | TemplateChildNode | null;
25
+ parent: ParentNode | null;
26
+ childIndex: number;
27
+ helpers: Map<symbol, number>;
28
+ components: Set<string>;
29
+ imports: ImportItem[];
30
+ bindingComponents: Record<string, {
31
+ type: BindingComponentTypes;
32
+ name: string;
33
+ }>;
34
+ identifiers: {
35
+ [name: string]: number | undefined;
36
+ };
37
+ cached: number;
38
+ scopes: {
39
+ vFor: number;
40
+ vueId: number;
41
+ };
42
+ scope: CodegenRootScope;
43
+ currentScope: CodegenScope;
44
+ currentVueId: string;
45
+ vueIds: string[];
46
+ inVOnce: boolean;
47
+ inVFor: boolean;
48
+ helper<T extends symbol>(name: T): T;
49
+ removeHelper<T extends symbol>(name: T): void;
50
+ helperString(name: symbol): string;
51
+ replaceNode(node: TemplateChildNode): void;
52
+ removeNode(node?: TemplateChildNode): void;
53
+ onNodeRemoved(): void;
54
+ addIdentifiers(exp: ExpressionNode | string): void;
55
+ removeIdentifiers(exp: ExpressionNode | string): void;
56
+ popScope(): CodegenScope | undefined;
57
+ getScopeIndex(scope: CodegenScope): number;
58
+ addVIfScope(initScope: CodegenVIfScopeInit): CodegenVIfScope;
59
+ addVForScope(initScope: CodegenVForScopeInit): CodegenVForScope;
60
+ cache<T extends JSChildNode>(exp: T, isVNode?: boolean): CacheExpression | T;
61
+ isMiniProgramComponent(name: string): boolean;
62
+ }
63
+ export declare function isRootScope(scope: CodegenScope): scope is CodegenRootScope;
64
+ export declare function isVIfScope(scope: CodegenScope): scope is CodegenVIfScope;
65
+ export declare function isVForScope(scope: CodegenScope): scope is CodegenVForScope;
66
+ export declare function isScopedSlotVFor({ source }: CodegenVForScope): boolean;
67
+ export declare function transform(root: CodegenRootNode, options: TransformOptions): TransformContext;
68
+ export declare function traverseNode(node: RootNode | TemplateChildNode, context: TransformContext): void;
69
+ export declare function traverseChildren(parent: ParentNode, context: TransformContext): void;
70
+ export declare function createTransformContext(rootNode: RootNode, { root, filename, isTS, inline, hashId, scopeId, filters, bindingCssVars, bindingMetadata, cacheHandlers, prefixIdentifiers, skipTransformIdentifier, renderDataSpread, nodeTransforms, directiveTransforms, miniProgram, isBuiltInComponent, isCustomElement, expressionPlugins, onError, onWarn, }: TransformOptions): TransformContext;
71
+ export declare type StructuralDirectiveTransform = (node: ElementNode, dir: DirectiveNode, context: TransformContext) => void | (() => void);
72
+ export declare function createStructuralDirectiveTransform(name: string | RegExp, fn: StructuralDirectiveTransform): NodeTransform;
73
+ export {};