@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,274 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createVSlotCallExpression = exports.rewriteScopedSlotVForScope = exports.findCurrentPath = exports.findSlotName = exports.rewriteVSlot = exports.transformSlot = void 0;
4
+ const types_1 = require("@babel/types");
5
+ const shared_1 = require("@vue/shared");
6
+ const compiler_core_1 = require("@vue/compiler-core");
7
+ const uni_shared_1 = require("@dcloudio/uni-shared");
8
+ const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
9
+ const runtimeHelpers_1 = require("../runtimeHelpers");
10
+ const ast_1 = require("../ast");
11
+ const codegen_1 = require("../codegen");
12
+ const transform_1 = require("../transform");
13
+ const utils_1 = require("./utils");
14
+ const vFor_1 = require("./vFor");
15
+ const runtimeHelpers_2 = require("../runtimeHelpers");
16
+ const transformSlot = (node, context) => {
17
+ if (!(0, uni_cli_shared_1.isUserComponent)(node, context)) {
18
+ return;
19
+ }
20
+ const { tag, children } = node;
21
+ const slots = new Set();
22
+ const onComponentSlot = (0, compiler_core_1.findDir)(node, 'slot', true);
23
+ const implicitDefaultChildren = [];
24
+ const isMiniProgramComponent = context.isMiniProgramComponent(tag);
25
+ for (let i = 0; i < children.length; i++) {
26
+ const slotElement = children[i];
27
+ let slotDir;
28
+ if (!(0, compiler_core_1.isTemplateNode)(slotElement) ||
29
+ !(slotDir = (0, compiler_core_1.findDir)(slotElement, 'slot', true))) {
30
+ // not a <template v-slot>, skip.
31
+ if (slotElement.type !== 3 /* COMMENT */) {
32
+ implicitDefaultChildren.push(slotElement);
33
+ }
34
+ continue;
35
+ }
36
+ if (onComponentSlot) {
37
+ // already has on-component slot - this is incorrect usage.
38
+ context.onError((0, compiler_core_1.createCompilerError)(37 /* X_V_SLOT_MIXED_SLOT_USAGE */, slotDir.loc));
39
+ break;
40
+ }
41
+ if (!slotDir.arg) {
42
+ // v-slot => v-slot:default
43
+ slotDir.arg = (0, compiler_core_1.createSimpleExpression)('default', true);
44
+ }
45
+ const slotName = transformTemplateSlotElement(slotDir, slotElement, node, context);
46
+ // 小程序组件默认插槽,直接移除<template #default>节点
47
+ if (isMiniProgramComponent) {
48
+ if (slotName === 'default' && slotElement.children.length === 1) {
49
+ children.splice(i, 1, slotElement.children[0]);
50
+ }
51
+ continue;
52
+ }
53
+ if (slotName) {
54
+ slots.add(slotName);
55
+ }
56
+ }
57
+ if (isMiniProgramComponent) {
58
+ return;
59
+ }
60
+ if (implicitDefaultChildren.length) {
61
+ slots.add(uni_shared_1.SLOT_DEFAULT_NAME);
62
+ }
63
+ if (onComponentSlot) {
64
+ // <unicloud-db v-slot:default="{data, loading, error, options}"/>
65
+ // => <unicloud-db collection=""><template v-slot:default="{data, loading, error, options}"/></unicloud-db>
66
+ slots.add(uni_shared_1.SLOT_DEFAULT_NAME);
67
+ const templateNode = createTemplateNode(onComponentSlot, implicitDefaultChildren);
68
+ transformTemplateSlotElement(onComponentSlot, templateNode, node, context);
69
+ node.children = [templateNode];
70
+ }
71
+ // 不支持 $slots, 则自动补充 props
72
+ if (slots.size && !context.miniProgram.slot.$slots) {
73
+ const slotsArr = [...slots];
74
+ const hasDynamic = slotsArr.find((name) => !(0, shared_1.isString)(name));
75
+ let value;
76
+ if (hasDynamic) {
77
+ const children = [];
78
+ const len = slotsArr.length - 1;
79
+ slotsArr.forEach((name, index) => {
80
+ if ((0, shared_1.isString)(name)) {
81
+ children.push(`'${(0, uni_shared_1.dynamicSlotName)(name)}'`);
82
+ }
83
+ else {
84
+ children.push(name);
85
+ }
86
+ if (index < len) {
87
+ children.push(',');
88
+ }
89
+ });
90
+ value = (0, compiler_core_1.createCompoundExpression)([
91
+ context.helperString(runtimeHelpers_2.DYNAMIC_SLOT) + '([',
92
+ ...children,
93
+ '])',
94
+ ]);
95
+ }
96
+ else {
97
+ value = `[${slotsArr
98
+ .map((name) => `'${(0, uni_shared_1.dynamicSlotName)(name)}'`)
99
+ .join(',')}]`;
100
+ }
101
+ node.props.unshift((0, uni_cli_shared_1.createBindDirectiveNode)(utils_1.ATTR_VUE_SLOTS, value));
102
+ }
103
+ };
104
+ exports.transformSlot = transformSlot;
105
+ function rewriteVSlot(dir, context) {
106
+ dir.arg = (0, utils_1.rewriteExpression)((0, compiler_core_1.createCompoundExpression)([
107
+ context.helperString(runtimeHelpers_2.DYNAMIC_SLOT) + '(',
108
+ dir.arg,
109
+ ')',
110
+ ]), context);
111
+ }
112
+ exports.rewriteVSlot = rewriteVSlot;
113
+ function transformTemplateSlotElement(slotDir, slotTemplate, slotComponent, context) {
114
+ const slotName = findSlotName(slotDir);
115
+ if (!slotName) {
116
+ return;
117
+ }
118
+ const { exp } = slotDir;
119
+ // non scoped slots
120
+ if (!exp) {
121
+ return slotName;
122
+ }
123
+ // empty
124
+ if (exp.type === 4 /* SIMPLE_EXPRESSION */ && !exp.content.trim()) {
125
+ return slotName;
126
+ }
127
+ // 使用vFor来简单处理scoped slot作用域问题
128
+ slotTemplate.children = [
129
+ createVForTemplate(slotTemplate, { name: slotName, value: (0, codegen_1.genExpr)(exp), slotComponent }, context),
130
+ ];
131
+ if (context.miniProgram.slot.dynamicSlotNames) {
132
+ // 已经在 vFor 中补充 slot,故需要移除 slotTemplate 中的
133
+ const index = slotTemplate.props.indexOf(slotDir);
134
+ if (index > -1) {
135
+ slotTemplate.props.splice(index, 1);
136
+ }
137
+ }
138
+ // v-slot="slotProps" => v-slot 避免 transformIdentifier 生成 slotProps 的变量声明
139
+ slotDir.exp = undefined;
140
+ return slotName;
141
+ }
142
+ function createTemplateNode(slotDir, children) {
143
+ return {
144
+ type: 1 /* ELEMENT */,
145
+ tag: 'template',
146
+ tagType: 3 /* TEMPLATE */,
147
+ loc: compiler_core_1.locStub,
148
+ isSelfClosing: false,
149
+ codegenNode: undefined,
150
+ ns: 0,
151
+ props: [slotDir],
152
+ children,
153
+ };
154
+ }
155
+ function findSlotName(slotDir) {
156
+ if (!slotDir.arg) {
157
+ return uni_shared_1.SLOT_DEFAULT_NAME;
158
+ }
159
+ if ((0, compiler_core_1.isStaticExp)(slotDir.arg)) {
160
+ return slotDir.arg.content;
161
+ }
162
+ return slotDir.arg;
163
+ }
164
+ exports.findSlotName = findSlotName;
165
+ function findCurrentVForValueAlias(context) {
166
+ let scope = context.currentScope;
167
+ while (scope) {
168
+ if ((0, transform_1.isVForScope)(scope)) {
169
+ return scope.valueAlias;
170
+ }
171
+ scope = scope.parent;
172
+ }
173
+ return '';
174
+ }
175
+ function createVForTemplate(slotElement, { name, value, slotComponent, }, context) {
176
+ const slotName = 's' + context.scopes.vFor;
177
+ const keyProp = (0, uni_cli_shared_1.createBindDirectiveNode)('key', 'i' + context.scopes.vFor);
178
+ const source = (0, shared_1.isString)(name) ? `'${name}'` : (0, codegen_1.genExpr)(name);
179
+ const vForProp = {
180
+ type: 7 /* DIRECTIVE */,
181
+ name: 'for',
182
+ loc: compiler_core_1.locStub,
183
+ modifiers: [],
184
+ arg: undefined,
185
+ exp: (0, compiler_core_1.createSimpleExpression)(`(${value}, ${slotName}) in ${utils_1.SCOPED_SLOT_IDENTIFIER}(${source}, ${findCurrentVForValueAlias(context) || `''`})`),
186
+ };
187
+ const props = [vForProp, keyProp];
188
+ if (context.miniProgram.slot.dynamicSlotNames) {
189
+ props.push((0, uni_cli_shared_1.createBindDirectiveNode)('slot', slotName));
190
+ }
191
+ return {
192
+ loc: slotElement.loc,
193
+ ns: 0,
194
+ tag: 'template',
195
+ type: 1 /* ELEMENT */,
196
+ tagType: 3 /* TEMPLATE */,
197
+ props,
198
+ isSelfClosing: false,
199
+ codegenNode: undefined,
200
+ children: slotElement.children,
201
+ slotComponent,
202
+ };
203
+ }
204
+ const slotNameRE = /\('(.*)',/;
205
+ /**
206
+ * ('default','') => default
207
+ * @param source
208
+ * @returns
209
+ */
210
+ function findCurrentSlotName(source) {
211
+ return (0, types_1.stringLiteral)((0, uni_shared_1.dynamicSlotName)(source.children[1].match(slotNameRE)[1]));
212
+ }
213
+ function createPathBinaryExpr(scope, computed = true) {
214
+ return (0, types_1.binaryExpression)('+', (0, types_1.binaryExpression)('+', (0, types_1.stringLiteral)(parseVForPath(scope.sourceAlias) + (computed ? '[' : '.')), (0, types_1.identifier)(scope.indexAlias)), (0, types_1.stringLiteral)(computed ? '].' : '.'));
215
+ }
216
+ function findCurrentPath(id, scope) {
217
+ let parent = scope.parent;
218
+ let binaryExpr = null;
219
+ while (parent) {
220
+ if ((0, transform_1.isVForScope)(parent)) {
221
+ // const computed = !isScopedSlotVFor(parent)
222
+ if (!binaryExpr) {
223
+ binaryExpr = createPathBinaryExpr(parent);
224
+ }
225
+ else {
226
+ binaryExpr = (0, types_1.binaryExpression)('+', createPathBinaryExpr(parent), binaryExpr);
227
+ }
228
+ }
229
+ parent = parent.parent;
230
+ }
231
+ return ((binaryExpr && (0, types_1.binaryExpression)('+', binaryExpr, (0, types_1.stringLiteral)(id))) ||
232
+ (0, types_1.stringLiteral)(id));
233
+ }
234
+ exports.findCurrentPath = findCurrentPath;
235
+ function findCurrentVueIdExpr(node, context) {
236
+ if (!node) {
237
+ return (0, types_1.stringLiteral)('');
238
+ }
239
+ const vueIdProp = (0, compiler_core_1.findProp)(node, utils_1.ATTR_VUE_ID);
240
+ if (vueIdProp.type === 6 /* ATTRIBUTE */) {
241
+ return (0, types_1.stringLiteral)(vueIdProp.value.content);
242
+ }
243
+ return (0, ast_1.parseExpr)((0, codegen_1.genExpr)(vueIdProp.exp), context) || (0, types_1.stringLiteral)('');
244
+ }
245
+ /**
246
+ * 目前无用
247
+ * @param vForScope
248
+ * @param parentScope
249
+ * @param context
250
+ */
251
+ function rewriteScopedSlotVForScope(vForScope, parentScope, context) {
252
+ // 生成一个新的sourceAlias,用于scopedSlots
253
+ const { source, sourceExpr } = vForScope;
254
+ vForScope.sourceAlias = (0, utils_1.rewriteExpressionWithoutProperty)(source, context, sourceExpr, parentScope).content;
255
+ }
256
+ exports.rewriteScopedSlotVForScope = rewriteScopedSlotVForScope;
257
+ function parseVForPath(id) {
258
+ return id.includes('.') ? id.split('.')[1] : id;
259
+ }
260
+ function createVSlotCallExpression(slotComponent, vForScope, context) {
261
+ const { source, sourceAlias } = vForScope;
262
+ const id = parseVForPath(sourceAlias);
263
+ return (0, types_1.callExpression)((0, types_1.identifier)(context.helperString(runtimeHelpers_1.WITH_SCOPED_SLOT)), [
264
+ (0, vFor_1.createVForArrowFunctionExpression)(vForScope),
265
+ (0, types_1.objectExpression)([
266
+ // 插槽名称,数据更新 path,vueId
267
+ (0, types_1.objectProperty)((0, types_1.identifier)('name'), findCurrentSlotName(source)),
268
+ // 暂不生成 path
269
+ (0, types_1.objectProperty)((0, types_1.identifier)('path'), findCurrentPath(id, vForScope)),
270
+ (0, types_1.objectProperty)((0, types_1.identifier)('vueId'), findCurrentVueIdExpr(slotComponent, context)),
271
+ ]),
272
+ ]);
273
+ }
274
+ exports.createVSlotCallExpression = createVSlotCallExpression;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@dcloudio/uni-mp-compiler",
3
+ "version": "0.0.1-nvue3.3030820220125001",
4
+ "description": "uni-mp-compiler",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "lib"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/dcloudio/uni-app.git",
14
+ "directory": "packages/uni-mp-compiler"
15
+ },
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1"
18
+ },
19
+ "license": "Apache-2.0",
20
+ "gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da",
21
+ "dependencies": {
22
+ "@babel/generator": "^7.16.0",
23
+ "@babel/parser": "^7.16.4",
24
+ "@babel/types": "^7.16.0",
25
+ "@dcloudio/uni-cli-shared": "0.0.1-nvue3.3030820220125001",
26
+ "@dcloudio/uni-shared": "0.0.1-nvue3.3030820220125001",
27
+ "@vue/compiler-core": "3.2.29",
28
+ "@vue/compiler-dom": "3.2.29",
29
+ "@vue/shared": "3.2.29",
30
+ "estree-walker": "^2.0.2"
31
+ },
32
+ "devDependencies": {
33
+ "@vue/compiler-sfc": "3.2.29",
34
+ "source-map": "^0.6.1"
35
+ }
36
+ }