@vue-jsx-vapor/macros 2.3.0 → 2.3.2
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.
- package/dist/api.cjs +6 -13
- package/dist/api.d.cts +8 -4
- package/dist/api.d.ts +8 -4
- package/dist/api.js +3 -14
- package/dist/astro.cjs +14 -18
- package/dist/astro.d.cts +4 -3
- package/dist/astro.d.ts +4 -3
- package/dist/astro.js +13 -17
- package/dist/chunk-BCwAaXi7.cjs +31 -0
- package/dist/core-B1Tt-ieP.cjs +438 -0
- package/dist/core-cHgZBgtA.js +377 -0
- package/dist/esbuild.cjs +12 -14
- package/dist/esbuild.d.cts +6 -5
- package/dist/esbuild.d.ts +6 -5
- package/dist/esbuild.js +9 -11
- package/dist/index.cjs +8 -12
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +6 -12
- package/dist/nuxt.cjs +23 -26
- package/dist/nuxt.d.cts +6 -5
- package/dist/nuxt.d.ts +6 -5
- package/dist/nuxt.js +19 -23
- package/dist/options-CNIhAQHD.cjs +27 -0
- package/dist/options-pbSvpKdV.js +20 -0
- package/dist/options.cjs +2 -5
- package/dist/options.d-B3WOiT_L.d.cts +26 -0
- package/dist/options.d-DtyRfe-9.d.ts +26 -0
- package/dist/options.d.cts +2 -23
- package/dist/options.d.ts +2 -23
- package/dist/options.js +3 -6
- package/dist/raw-3ZelnSQR.cjs +62 -0
- package/dist/raw-9SXDNxBH.js +55 -0
- package/dist/raw.cjs +5 -9
- package/dist/raw.d.cts +5 -4
- package/dist/raw.d.ts +5 -4
- package/dist/raw.js +5 -8
- package/dist/rolldown.cjs +12 -14
- package/dist/rolldown.d.cts +4 -3
- package/dist/rolldown.d.ts +4 -3
- package/dist/rolldown.js +9 -11
- package/dist/rollup.cjs +12 -14
- package/dist/rollup.d.cts +6 -5
- package/dist/rollup.d.ts +6 -5
- package/dist/rollup.js +9 -11
- package/dist/rspack.cjs +12 -14
- package/dist/rspack.d.cts +4 -3
- package/dist/rspack.d.ts +4 -3
- package/dist/rspack.js +9 -11
- package/dist/src-BfZoaRPY.js +9 -0
- package/dist/src-DwQLomPw.cjs +16 -0
- package/dist/vite-C5QxH0B9.cjs +13 -0
- package/dist/vite-DPLCEtsm.js +7 -0
- package/dist/vite.cjs +8 -12
- package/dist/vite.d.cts +6 -5
- package/dist/vite.d.ts +6 -5
- package/dist/vite.js +7 -10
- package/dist/volar.cjs +221 -372
- package/dist/volar.d.cts +5 -4
- package/dist/volar.d.ts +5 -4
- package/dist/volar.js +218 -370
- package/dist/webpack-1cYmRdvx.js +7 -0
- package/dist/webpack-B-EPKx0G.cjs +13 -0
- package/dist/webpack.cjs +8 -12
- package/dist/webpack.d.cts +6 -5
- package/dist/webpack.d.ts +6 -5
- package/dist/webpack.js +7 -10
- package/package.json +7 -7
- package/dist/chunk-53TNKVYD.cjs +0 -29
- package/dist/chunk-AUE5EAQA.js +0 -12
- package/dist/chunk-DANY2WQD.js +0 -29
- package/dist/chunk-E5WGUCGE.js +0 -68
- package/dist/chunk-HBVQYNJJ.js +0 -10
- package/dist/chunk-KUKYWPEK.cjs +0 -10
- package/dist/chunk-L26STXZK.cjs +0 -12
- package/dist/chunk-PWA6BBMQ.cjs +0 -10
- package/dist/chunk-SMWNQXL7.js +0 -585
- package/dist/chunk-UAWFIGPX.cjs +0 -68
- package/dist/chunk-ZQ4GVJKX.cjs +0 -585
- package/dist/chunk-ZXJQCFZZ.js +0 -10
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_chunk = require('./chunk-BCwAaXi7.cjs');
|
|
3
|
+
const __vue_macros_common = require_chunk.__toESM(require("@vue-macros/common"));
|
|
4
|
+
const __vue_compiler_sfc = require_chunk.__toESM(require("@vue/compiler-sfc"));
|
|
5
|
+
const hash_sum = require_chunk.__toESM(require("hash-sum"));
|
|
6
|
+
|
|
7
|
+
//#region src/core/helper/use-model.ts?raw
|
|
8
|
+
var use_model_default = "import { customRef, watchSyncEffect } from \"vue\";\nexport function useModel(props, name, options = {}) {\n const res = customRef((track, trigger) => {\n let localValue = options && options.default;\n let prevEmittedValue;\n watchSyncEffect(() => {\n const propValue = props[name];\n if (!Object.is(prevEmittedValue, propValue)) {\n localValue = propValue;\n trigger();\n }\n });\n return {\n get() {\n track();\n return options.get ? options.get(localValue) : localValue;\n },\n set(value) {\n if (Object.is(value, localValue)) return;\n localValue = value;\n trigger();\n const emittedValue = prevEmittedValue = options.set ? options.set(value) : value;\n for (const emit of [props[`onUpdate:${name}`]].flat()) {\n if (typeof emit === \"function\") emit(emittedValue);\n }\n }\n };\n });\n res[Symbol.iterator] = () => {\n let i = 0;\n return {\n next() {\n if (i < 2) {\n return {\n value: i++ ? props[`${name}Modifiers`] || {} : res,\n done: false\n };\n } else {\n return { done: true };\n }\n }\n };\n };\n return res;\n}\n";
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/core/helper/with-defaults.ts?raw
|
|
12
|
+
var with_defaults_default = "function resolveDefaultProps(paths) {\n const result = {};\n for (const path of Object.keys(paths)) {\n const segments = path.split(/[.?[\\]]/).filter(Boolean);\n let current = result;\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i];\n if (i === segments.length - 1) {\n current[segment] = paths[path];\n } else {\n if (!current[segment]) {\n current[segment] = Number.isNaN(Number(segments[i + 1])) ? {} : [];\n }\n current = current[segment];\n }\n }\n }\n return result;\n}\nexport function createPropsDefaultProxy(props, defaults) {\n const defaultProps = resolveDefaultProps(defaults);\n const result = {};\n for (const key of [\n .../* @__PURE__ */ new Set([...Object.keys(props), ...Object.keys(defaultProps)])\n ]) {\n Object.defineProperty(result, key, {\n enumerable: true,\n get: () => props[key] === void 0 ? defaultProps[key] : props[key]\n });\n }\n return result;\n}\n";
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/core/helper/index.ts
|
|
16
|
+
const helperPrefix = "vue-jsx-vapor/macros";
|
|
17
|
+
const useModelHelperId = `${helperPrefix}/use-model`;
|
|
18
|
+
const withDefaultsHelperId = `${helperPrefix}/with-defaults`;
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/core/restructure.ts
|
|
22
|
+
function restructure(s, node, options = {}) {
|
|
23
|
+
let index = 0;
|
|
24
|
+
const propList = [];
|
|
25
|
+
for (const param of node.params) {
|
|
26
|
+
const path = `${__vue_macros_common.HELPER_PREFIX}props${index++ || ""}`;
|
|
27
|
+
const props = getProps(param, path, s, [], options);
|
|
28
|
+
if (props) {
|
|
29
|
+
const hasDefaultValue = props.some((i) => i.defaultValue);
|
|
30
|
+
s.overwrite(param.start, param.end, path);
|
|
31
|
+
propList.push(...hasDefaultValue ? props.map((i) => ({
|
|
32
|
+
...i,
|
|
33
|
+
path: i.path.replace(__vue_macros_common.HELPER_PREFIX, `${__vue_macros_common.HELPER_PREFIX}default_`)
|
|
34
|
+
})) : props);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (propList.length) {
|
|
38
|
+
const defaultValues = {};
|
|
39
|
+
const rests = [];
|
|
40
|
+
for (const prop of propList) {
|
|
41
|
+
if (prop.defaultValue) {
|
|
42
|
+
const basePath = prop.path.split(/\.|\[/)[0];
|
|
43
|
+
(defaultValues[basePath] ??= []).push(prop);
|
|
44
|
+
}
|
|
45
|
+
if (prop.isRest) rests.push(prop);
|
|
46
|
+
}
|
|
47
|
+
for (const [path, values] of Object.entries(defaultValues)) {
|
|
48
|
+
const createPropsDefaultProxy = (0, __vue_macros_common.importHelperFn)(s, 0, "createPropsDefaultProxy", void 0, options.withDefaultsFrom ?? withDefaultsHelperId);
|
|
49
|
+
const resolvedPath = path.replace(`${__vue_macros_common.HELPER_PREFIX}default_`, __vue_macros_common.HELPER_PREFIX);
|
|
50
|
+
const resolvedValues = values.map((i) => `'${i.path.replace(path, "")}${i.value}': ${i.defaultValue}`).join(", ");
|
|
51
|
+
prependFunctionalNode(node, s, `\nconst ${path} = ${createPropsDefaultProxy}(${resolvedPath}, {${resolvedValues}})`);
|
|
52
|
+
}
|
|
53
|
+
for (const [index$1, rest] of rests.entries()) prependFunctionalNode(node, s, options.generateRestProps?.(rest.name, index$1, rests) ?? `\nconst ${rest.name} = ${(0, __vue_macros_common.importHelperFn)(s, 0, "createPropsRestProxy")}(${rest.path}, [${rest.value}])`);
|
|
54
|
+
(0, __vue_compiler_sfc.walkIdentifiers)(node.body, (id, parent) => {
|
|
55
|
+
const prop = propList.find((i) => i.name === id.name);
|
|
56
|
+
if (prop && !prop.isRest) s.overwrite(id.start, id.end, `${parent?.type === "ObjectProperty" && parent.shorthand ? `${id.name}: ` : ""}${prop.path}${prop.value}`);
|
|
57
|
+
}, false);
|
|
58
|
+
}
|
|
59
|
+
return propList;
|
|
60
|
+
}
|
|
61
|
+
function getProps(node, path = "", s, props = [], options) {
|
|
62
|
+
const properties = node.type === "ObjectPattern" ? node.properties : node.type === "ArrayPattern" ? node.elements : [];
|
|
63
|
+
if (!properties.length) return;
|
|
64
|
+
const propNames = [];
|
|
65
|
+
properties.forEach((prop, index) => {
|
|
66
|
+
if (prop?.type === "Identifier") {
|
|
67
|
+
props.push({
|
|
68
|
+
name: prop.name,
|
|
69
|
+
path,
|
|
70
|
+
value: `[${index}]`
|
|
71
|
+
});
|
|
72
|
+
propNames.push(`'${prop.name}'`);
|
|
73
|
+
} else if (prop?.type === "AssignmentPattern" && prop.left.type === "Identifier") {
|
|
74
|
+
props.push({
|
|
75
|
+
path,
|
|
76
|
+
name: prop.left.name,
|
|
77
|
+
value: `[${index}]`,
|
|
78
|
+
defaultValue: s.slice(prop.right.start, prop.right.end)
|
|
79
|
+
});
|
|
80
|
+
propNames.push(`'${prop.left.name}'`);
|
|
81
|
+
} else if (prop?.type === "ObjectProperty" && prop.key.type === "Identifier") {
|
|
82
|
+
if (prop.value.type === "AssignmentPattern" && prop.value.left.type === "Identifier") props.push({
|
|
83
|
+
path,
|
|
84
|
+
name: prop.value.left.name,
|
|
85
|
+
value: `.${prop.key.name}`,
|
|
86
|
+
defaultValue: s.slice(prop.value.right.start, prop.value.right.end),
|
|
87
|
+
isRequired: prop.value.right.type === "TSNonNullExpression"
|
|
88
|
+
});
|
|
89
|
+
else if (!getProps(prop.value, `${path}.${prop.key.name}`, s, props, options)) props.push({
|
|
90
|
+
path,
|
|
91
|
+
name: prop.value.type === "Identifier" ? prop.value.name : prop.key.name,
|
|
92
|
+
value: `.${prop.key.name}`
|
|
93
|
+
});
|
|
94
|
+
propNames.push(`'${prop.key.name}'`);
|
|
95
|
+
} else if (prop?.type === "RestElement" && prop.argument.type === "Identifier" && !prop.argument.name.startsWith(`${__vue_macros_common.HELPER_PREFIX}props`)) props.push({
|
|
96
|
+
path,
|
|
97
|
+
name: prop.argument.name,
|
|
98
|
+
value: propNames.join(", "),
|
|
99
|
+
isRest: true
|
|
100
|
+
});
|
|
101
|
+
else if (prop) getProps(prop, `${path}[${index}]`, s, props, options);
|
|
102
|
+
});
|
|
103
|
+
return props.length ? props : void 0;
|
|
104
|
+
}
|
|
105
|
+
function prependFunctionalNode(node, s, result) {
|
|
106
|
+
const isBlockStatement = node.body.type === "BlockStatement";
|
|
107
|
+
const start = node.body.extra?.parenthesized ? node.body.extra.parenStart : node.body.start;
|
|
108
|
+
s.appendRight(start + (isBlockStatement ? 1 : 0), `${result};${!isBlockStatement ? "return " : ""}`);
|
|
109
|
+
if (!isBlockStatement) {
|
|
110
|
+
s.appendLeft(start, "{");
|
|
111
|
+
s.appendRight(node.end, "}");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/core/define-component/await.ts
|
|
117
|
+
const isFunctionType = (node) => {
|
|
118
|
+
return /Function(?:Expression|Declaration)$|Method$/.test(node.type);
|
|
119
|
+
};
|
|
120
|
+
function transformAwait(root, s) {
|
|
121
|
+
if (root.body.type !== "BlockStatement") return;
|
|
122
|
+
let hasAwait = false;
|
|
123
|
+
for (const node of root.body.body) if (node.type === "VariableDeclaration" && !node.declare || node.type.endsWith("Statement")) {
|
|
124
|
+
const scope = [root.body.body];
|
|
125
|
+
(0, __vue_macros_common.walkAST)(node, {
|
|
126
|
+
enter(child, parent) {
|
|
127
|
+
if (isFunctionType(child)) this.skip();
|
|
128
|
+
if (child.type === "BlockStatement") scope.push(child.body);
|
|
129
|
+
if (child.type === "AwaitExpression") {
|
|
130
|
+
hasAwait = true;
|
|
131
|
+
const currentScope = scope.at(-1);
|
|
132
|
+
const needsSemi = !!currentScope?.some((n, i) => {
|
|
133
|
+
return (scope.length === 1 || i > 0) && n.type === "ExpressionStatement" && n.start === child.start;
|
|
134
|
+
});
|
|
135
|
+
processAwait(s, child, needsSemi, parent.type === "ExpressionStatement");
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
leave(node$1) {
|
|
139
|
+
if (node$1.type === "BlockStatement") scope.pop();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (hasAwait) s.prependLeft(root.body.start + 1, `\nlet __temp, __restore\n`);
|
|
144
|
+
}
|
|
145
|
+
function processAwait(s, node, needSemi, isStatement) {
|
|
146
|
+
const argumentStart = node.argument.extra && node.argument.extra.parenthesized ? node.argument.extra.parenStart : node.argument.start;
|
|
147
|
+
const argumentStr = s.slice(argumentStart, node.argument.end);
|
|
148
|
+
const containsNestedAwait = /\bawait\b/.test(argumentStr);
|
|
149
|
+
s.overwrite(node.start, argumentStart, `${needSemi ? `;` : ``}(\n ([__temp,__restore] = ${(0, __vue_macros_common.importHelperFn)(s, 0, `withAsyncContext`)}(${containsNestedAwait ? `async ` : ``}() => `);
|
|
150
|
+
s.appendLeft(node.end, `)),\n ${isStatement ? `` : `__temp = `}await __temp,\n __restore()${isStatement ? `` : `,\n __temp`}\n)`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/core/define-component/return.ts
|
|
155
|
+
function transformReturn(root, s) {
|
|
156
|
+
const node = root.body.type === "BlockStatement" ? root.body.body.find((node$1) => node$1.type === "ReturnStatement")?.argument : root.body;
|
|
157
|
+
if (!node || isFunctionalNode(node)) return;
|
|
158
|
+
s.appendRight(node.extra?.parenthesized ? node.extra.parenStart : node.start, "() => ");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/core/define-component/index.ts
|
|
163
|
+
function transformDefineComponent(root, propsName, map, s, autoReturnFunction = false) {
|
|
164
|
+
if (!map.defineComponent) return;
|
|
165
|
+
const defineComponentName = s.sliceNode(map.defineComponent.callee);
|
|
166
|
+
if (defineComponentName && !["defineComponent", "defineVaporComponent"].includes(defineComponentName)) (0, __vue_macros_common.importHelperFn)(s, 0, "defineComponent", defineComponentName);
|
|
167
|
+
let hasRestProp = false;
|
|
168
|
+
const props = {};
|
|
169
|
+
if (root.params[0]) if (root.params[0].type === "Identifier") getWalkedIds(root, propsName).forEach((id) => props[id] = null);
|
|
170
|
+
else {
|
|
171
|
+
const restructuredProps = restructure(s, root, { generateRestProps: (restPropsName, index, list) => {
|
|
172
|
+
if (index === list.length - 1) {
|
|
173
|
+
hasRestProp = true;
|
|
174
|
+
const useAttrs = (0, __vue_macros_common.importHelperFn)(s, 0, "useAttrs");
|
|
175
|
+
return `const ${restPropsName} = ${useAttrs}()`;
|
|
176
|
+
}
|
|
177
|
+
} });
|
|
178
|
+
for (const prop of restructuredProps) if (prop.path.endsWith("props") && !prop.isRest) props[prop.name] = prop.isRequired ? "{ required: true }" : null;
|
|
179
|
+
}
|
|
180
|
+
for (const { expression, isRequired } of map.defineModel || []) {
|
|
181
|
+
const modelOptions = expression.arguments[0]?.type === "ObjectExpression" ? expression.arguments[0] : expression.arguments[1]?.type === "ObjectExpression" ? expression.arguments[1] : void 0;
|
|
182
|
+
const options = {};
|
|
183
|
+
if (isRequired) options.required = true;
|
|
184
|
+
for (const prop of modelOptions?.properties || []) if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && [
|
|
185
|
+
"validator",
|
|
186
|
+
"type",
|
|
187
|
+
"required"
|
|
188
|
+
].includes(prop.key.name)) options[prop.key.name] = s.sliceNode(prop.value);
|
|
189
|
+
const propName = expression.arguments[0]?.type === "StringLiteral" ? expression.arguments[0].value : "modelValue";
|
|
190
|
+
props[propName] = Object.keys(options).length ? `{ ${Object.entries(options).map(([key, value]) => `${key}: ${value}`).join(", ")} }` : null;
|
|
191
|
+
props[`onUpdate:${propName}`] = null;
|
|
192
|
+
props[`${propName === "modelValue" ? "model" : propName}Modifiers`] = null;
|
|
193
|
+
}
|
|
194
|
+
const propsString = Object.entries(props).map(([key, value]) => `'${key}': ${value}`).join(", ");
|
|
195
|
+
if (propsString) {
|
|
196
|
+
const argument = map.defineComponent.arguments[1];
|
|
197
|
+
if (!argument) s.appendRight(root.end, `, {${hasRestProp ? "inheritAttrs: false," : ""} props: { ${propsString} } }`);
|
|
198
|
+
else if (argument.type === "ObjectExpression") {
|
|
199
|
+
prependObjectExpression(argument, "props", `{ ${propsString} }`, s);
|
|
200
|
+
if (hasRestProp) prependObjectExpression(argument, "inheritAttrs", "false", s);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
transformAwait(root, s);
|
|
204
|
+
if (autoReturnFunction) transformReturn(root, s);
|
|
205
|
+
}
|
|
206
|
+
function prependObjectExpression(argument, name, value, s) {
|
|
207
|
+
if (!argument.properties?.find((prop) => prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === name)) s.appendRight(argument.start + 1, `${name}: ${value},`);
|
|
208
|
+
}
|
|
209
|
+
function getWalkedIds(root, propsName) {
|
|
210
|
+
const walkedIds = new Set();
|
|
211
|
+
(0, __vue_compiler_sfc.walkIdentifiers)(root.body, (id, parent) => {
|
|
212
|
+
if (id.name === propsName && (parent?.type === "MemberExpression" || parent?.type === "OptionalMemberExpression")) {
|
|
213
|
+
const prop = parent.property.type === "Identifier" ? parent.property.name : parent.property.type === "StringLiteral" ? parent.property.value : "";
|
|
214
|
+
if (prop) walkedIds.add(prop);
|
|
215
|
+
}
|
|
216
|
+
}, false);
|
|
217
|
+
return walkedIds;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/core/define-expose.ts
|
|
222
|
+
function transformDefineExpose(node, s, version) {
|
|
223
|
+
s.overwriteNode(node.callee, ";");
|
|
224
|
+
s.appendRight(node.arguments[0]?.start || node.end - 1, version >= 3.6 ? `${(0, __vue_macros_common.importHelperFn)(s, 0, "currentInstance")}.exposed = ` : `${(0, __vue_macros_common.importHelperFn)(s, 0, "getCurrentInstance")}().exposed = `);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/core/define-model.ts
|
|
229
|
+
function transformDefineModel(node, propsName, s) {
|
|
230
|
+
s.overwriteNode(node.callee, (0, __vue_macros_common.importHelperFn)(s, 0, "useModel", void 0, useModelHelperId));
|
|
231
|
+
s.appendRight(node.arguments[0]?.start || node.end - 1, `${propsName}, ${node.arguments[0]?.type !== "StringLiteral" ? `'modelValue',` : ""}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/core/define-slots.ts
|
|
236
|
+
function transformDefineSlots(node, s) {
|
|
237
|
+
s.overwrite(node.start, node.arguments[0]?.start && node.arguments[0].start - 1 || node.typeArguments?.end || node.callee.end, `Object.assign`);
|
|
238
|
+
const slots = `${(0, __vue_macros_common.importHelperFn)(s, 0, "useSlots")}()`;
|
|
239
|
+
s.appendLeft(node.end - 1, `${node.arguments[0] ? "," : "{}, "}${slots}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/core/define-style.ts
|
|
244
|
+
function transformDefineStyle(defineStyle, index, root, s, importMap) {
|
|
245
|
+
const { expression, lang, isDeclaration } = defineStyle;
|
|
246
|
+
if (expression.arguments[0]?.type !== "TemplateLiteral") return;
|
|
247
|
+
let css = s.sliceNode(expression.arguments[0]).slice(1, -1);
|
|
248
|
+
const scopeId = (0, hash_sum.default)(css);
|
|
249
|
+
const vars = new Map();
|
|
250
|
+
expression.arguments[0].expressions.forEach((exp) => {
|
|
251
|
+
const cssVar = s.sliceNode(exp);
|
|
252
|
+
const cssVarId = toCssVarId(cssVar, `--${scopeId}-`);
|
|
253
|
+
s.overwrite(exp.start - 2, exp.end + 1, `var(${cssVarId})`);
|
|
254
|
+
vars.set(cssVarId, cssVar);
|
|
255
|
+
});
|
|
256
|
+
let returnExpression = root && getReturnStatement(root);
|
|
257
|
+
if (isFunctionalNode(returnExpression)) returnExpression = getReturnStatement(returnExpression);
|
|
258
|
+
if (vars.size && returnExpression) {
|
|
259
|
+
const children = returnExpression.type === "JSXElement" ? [returnExpression] : returnExpression.type === "JSXFragment" ? returnExpression.children : [];
|
|
260
|
+
const varString = Array.from(vars.entries()).map(([key, value]) => `'${key}': ${value}`).join(", ");
|
|
261
|
+
for (const child of children) if (child.type === "JSXElement") s.appendRight(child.openingElement.name.end, ` {...{style:{${varString}}}}`);
|
|
262
|
+
}
|
|
263
|
+
let scoped = !!root;
|
|
264
|
+
if (expression.arguments[1]?.type === "ObjectExpression") {
|
|
265
|
+
for (const prop of expression.arguments[1].properties) if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "scoped" && prop.value.type === "BooleanLiteral") scoped = prop.value.value;
|
|
266
|
+
}
|
|
267
|
+
if (scoped && returnExpression) (0, __vue_macros_common.walkAST)(returnExpression, { enter(node) {
|
|
268
|
+
if (node.type === "JSXElement" && s.sliceNode(node.openingElement.name) !== "template") s.appendRight(node.openingElement.name.end, ` data-v-${scopeId}=""`);
|
|
269
|
+
} });
|
|
270
|
+
css = s.sliceNode(expression.arguments[0]).slice(1, -1).replaceAll(/\/\/(.*)(?=\n)/g, "/*$1*/");
|
|
271
|
+
const module$1 = isDeclaration ? "module." : "";
|
|
272
|
+
const importId = `${helperPrefix}/define-style/${index}?scopeId=${scopeId}&scoped=${scoped}&lang.${module$1}${lang}`;
|
|
273
|
+
importMap.set(importId, css);
|
|
274
|
+
s.appendLeft(0, isDeclaration ? `import style${index} from "${importId}";` : `import "${importId}";`);
|
|
275
|
+
s.overwriteNode(expression, isDeclaration ? `style${index}` : "");
|
|
276
|
+
}
|
|
277
|
+
function getReturnStatement(root) {
|
|
278
|
+
if (root.body.type === "BlockStatement") {
|
|
279
|
+
const returnStatement = root.body.body.find((node) => node.type === "ReturnStatement");
|
|
280
|
+
if (returnStatement) return returnStatement.argument;
|
|
281
|
+
} else return root.body;
|
|
282
|
+
}
|
|
283
|
+
function toCssVarId(name, prefix = "") {
|
|
284
|
+
return prefix + name.replaceAll(/\W/g, (searchValue, replaceValue) => {
|
|
285
|
+
return searchValue === "." ? "-" : name.charCodeAt(replaceValue).toString();
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/core/index.ts
|
|
291
|
+
function transformJsxMacros(code, id, importMap, options) {
|
|
292
|
+
const s = new __vue_macros_common.MagicStringAST(code);
|
|
293
|
+
const ast = (0, __vue_macros_common.babelParse)(s.original, (0, __vue_macros_common.getLang)(id));
|
|
294
|
+
const rootMap = getRootMap(ast, s, options);
|
|
295
|
+
let defineStyleIndex = 0;
|
|
296
|
+
for (const [root, map] of rootMap) {
|
|
297
|
+
map.defineStyle?.forEach((defineStyle) => {
|
|
298
|
+
transformDefineStyle(defineStyle, defineStyleIndex++, root, s, importMap);
|
|
299
|
+
});
|
|
300
|
+
if (root === void 0) continue;
|
|
301
|
+
let propsName = `${__vue_macros_common.HELPER_PREFIX}props`;
|
|
302
|
+
if (root.params[0]) {
|
|
303
|
+
if (root.params[0].type === "Identifier") propsName = root.params[0].name;
|
|
304
|
+
else if (root.params[0].type === "ObjectPattern") {
|
|
305
|
+
const lastProp = root.params[0].properties.at(-1);
|
|
306
|
+
if (!map.defineComponent && lastProp?.type === "RestElement" && lastProp.argument.type === "Identifier") propsName = lastProp.argument.name;
|
|
307
|
+
else s.appendRight(root.params[0].extra?.trailingComma ? root.params[0].extra?.trailingComma + 1 : lastProp?.end || root.params[0].end - 1, `${!root.params[0].extra?.trailingComma && root.params[0].properties.length ? "," : ""} ...${__vue_macros_common.HELPER_PREFIX}props`);
|
|
308
|
+
}
|
|
309
|
+
} else s.appendRight(getParamsStart(root, s.original), propsName);
|
|
310
|
+
if (map.defineComponent) transformDefineComponent(root, propsName, map, s, options.defineComponent?.autoReturnFunction);
|
|
311
|
+
if (map.defineModel?.length) map.defineModel.forEach(({ expression }) => {
|
|
312
|
+
transformDefineModel(expression, propsName, s);
|
|
313
|
+
});
|
|
314
|
+
if (map.defineSlots) transformDefineSlots(map.defineSlots, s);
|
|
315
|
+
if (map.defineExpose) transformDefineExpose(map.defineExpose, s, options.version);
|
|
316
|
+
}
|
|
317
|
+
return (0, __vue_macros_common.generateTransform)(s, id);
|
|
318
|
+
}
|
|
319
|
+
function getRootMap(ast, s, options) {
|
|
320
|
+
const parents = [];
|
|
321
|
+
const rootMap = new Map();
|
|
322
|
+
(0, __vue_macros_common.walkAST)(ast, {
|
|
323
|
+
enter(node, parent) {
|
|
324
|
+
parents.unshift(parent);
|
|
325
|
+
const root = isFunctionalNode(parents[1]) ? parents[1] : void 0;
|
|
326
|
+
if (root && parents[2]?.type === "CallExpression" && options.defineComponent.alias.includes(s.sliceNode(parents[2].callee))) {
|
|
327
|
+
if (!rootMap.has(root)) rootMap.set(root, {});
|
|
328
|
+
if (!rootMap.get(root).defineComponent) rootMap.get(root).defineComponent = parents[2];
|
|
329
|
+
}
|
|
330
|
+
const expression = node.type === "VariableDeclaration" ? node.declarations[0].init?.type === "CallExpression" && s.sliceNode(node.declarations[0].init.callee) === "$" ? node.declarations[0].init.arguments[0] : node.declarations[0].init : node.type === "ExpressionStatement" ? node.expression : void 0;
|
|
331
|
+
if (!expression) return;
|
|
332
|
+
const macroExpression = getMacroExpression(expression, options);
|
|
333
|
+
if (!macroExpression) return;
|
|
334
|
+
if (!rootMap.has(root)) rootMap.set(root, {});
|
|
335
|
+
const macroName = s.sliceNode(macroExpression.callee.type === "MemberExpression" ? macroExpression.callee.object : macroExpression.callee);
|
|
336
|
+
if (macroName) {
|
|
337
|
+
if (options.defineModel.alias.includes(macroName)) (rootMap.get(root).defineModel ??= []).push({
|
|
338
|
+
expression: macroExpression,
|
|
339
|
+
isRequired: expression.type === "TSNonNullExpression"
|
|
340
|
+
});
|
|
341
|
+
else if (options.defineStyle.alias.includes(macroName)) {
|
|
342
|
+
const lang = macroExpression.callee.type === "MemberExpression" && macroExpression.callee.property.type === "Identifier" ? macroExpression.callee.property.name : "css";
|
|
343
|
+
(rootMap.get(root).defineStyle ??= []).push({
|
|
344
|
+
expression: macroExpression,
|
|
345
|
+
isDeclaration: node.type === "VariableDeclaration",
|
|
346
|
+
lang
|
|
347
|
+
});
|
|
348
|
+
} else if (options.defineSlots.alias.includes(macroName)) rootMap.get(root).defineSlots = macroExpression;
|
|
349
|
+
else if (options.defineExpose.alias.includes(macroName)) rootMap.get(root).defineExpose = macroExpression;
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
leave() {
|
|
353
|
+
parents.shift();
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
return rootMap;
|
|
357
|
+
}
|
|
358
|
+
function isFunctionalNode(node) {
|
|
359
|
+
return !!(node && (node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression"));
|
|
360
|
+
}
|
|
361
|
+
function getMacroExpression(node, options) {
|
|
362
|
+
if (node.type === "TSNonNullExpression") node = node.expression;
|
|
363
|
+
if (node.type === "CallExpression") {
|
|
364
|
+
if (node.callee.type === "MemberExpression" && node.callee.object.type === "Identifier" && node.callee.object.name === "defineStyle") return node;
|
|
365
|
+
else if (node.callee.type === "Identifier" && [
|
|
366
|
+
...options.defineComponent.alias,
|
|
367
|
+
...options.defineSlots.alias,
|
|
368
|
+
...options.defineModel.alias,
|
|
369
|
+
...options.defineExpose.alias,
|
|
370
|
+
...options.defineStyle.alias
|
|
371
|
+
].includes(node.callee.name)) return node;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
function getParamsStart(node, code) {
|
|
375
|
+
return node.params[0] ? node.params[0].start : node.start + (code.slice(node.start, node.body.start).match(/\(\s*\)/)?.index || 0) + 1;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
//#endregion
|
|
379
|
+
Object.defineProperty(exports, 'getMacroExpression', {
|
|
380
|
+
enumerable: true,
|
|
381
|
+
get: function () {
|
|
382
|
+
return getMacroExpression;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
Object.defineProperty(exports, 'getParamsStart', {
|
|
386
|
+
enumerable: true,
|
|
387
|
+
get: function () {
|
|
388
|
+
return getParamsStart;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
Object.defineProperty(exports, 'helperPrefix', {
|
|
392
|
+
enumerable: true,
|
|
393
|
+
get: function () {
|
|
394
|
+
return helperPrefix;
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
Object.defineProperty(exports, 'isFunctionalNode', {
|
|
398
|
+
enumerable: true,
|
|
399
|
+
get: function () {
|
|
400
|
+
return isFunctionalNode;
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
Object.defineProperty(exports, 'restructure', {
|
|
404
|
+
enumerable: true,
|
|
405
|
+
get: function () {
|
|
406
|
+
return restructure;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
Object.defineProperty(exports, 'transformJsxMacros', {
|
|
410
|
+
enumerable: true,
|
|
411
|
+
get: function () {
|
|
412
|
+
return transformJsxMacros;
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
Object.defineProperty(exports, 'useModelHelperId', {
|
|
416
|
+
enumerable: true,
|
|
417
|
+
get: function () {
|
|
418
|
+
return useModelHelperId;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
Object.defineProperty(exports, 'use_model_default', {
|
|
422
|
+
enumerable: true,
|
|
423
|
+
get: function () {
|
|
424
|
+
return use_model_default;
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
Object.defineProperty(exports, 'withDefaultsHelperId', {
|
|
428
|
+
enumerable: true,
|
|
429
|
+
get: function () {
|
|
430
|
+
return withDefaultsHelperId;
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
Object.defineProperty(exports, 'with_defaults_default', {
|
|
434
|
+
enumerable: true,
|
|
435
|
+
get: function () {
|
|
436
|
+
return with_defaults_default;
|
|
437
|
+
}
|
|
438
|
+
});
|