@vue/compiler-sfc 3.3.0-alpha.4 → 3.3.0-alpha.6
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/compiler-sfc.cjs.js +527 -154
- package/dist/compiler-sfc.d.ts +31 -30
- package/dist/compiler-sfc.esm-browser.js +2515 -1988
- package/package.json +13 -13
package/dist/compiler-sfc.cjs.js
CHANGED
|
@@ -226,7 +226,7 @@ function genCssVarsCode(vars, bindings, id, isProd) {
|
|
|
226
226
|
}).join("");
|
|
227
227
|
return `_${CSS_VARS_HELPER}(_ctx => (${transformedString}))`;
|
|
228
228
|
}
|
|
229
|
-
function genNormalScriptCssVarsCode(cssVars, bindings, id, isProd) {
|
|
229
|
+
function genNormalScriptCssVarsCode(cssVars, bindings, id, isProd, defaultVar) {
|
|
230
230
|
return `
|
|
231
231
|
import { ${CSS_VARS_HELPER} as _${CSS_VARS_HELPER} } from 'vue'
|
|
232
232
|
const __injectCSSVars__ = () => {
|
|
@@ -236,8 +236,8 @@ ${genCssVarsCode(
|
|
|
236
236
|
id,
|
|
237
237
|
isProd
|
|
238
238
|
)}}
|
|
239
|
-
const __setup__ =
|
|
240
|
-
|
|
239
|
+
const __setup__ = ${defaultVar}.setup
|
|
240
|
+
${defaultVar}.setup = __setup__
|
|
241
241
|
? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }
|
|
242
242
|
: __injectCSSVars__
|
|
243
243
|
`;
|
|
@@ -3292,118 +3292,252 @@ function patchErrors(errors, source, inMap) {
|
|
|
3292
3292
|
});
|
|
3293
3293
|
}
|
|
3294
3294
|
|
|
3295
|
-
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/;
|
|
3296
|
-
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)(?:as)?(\s*)default/s;
|
|
3297
|
-
const exportDefaultClassRE = /((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
3298
3295
|
function rewriteDefault(input, as, parserPlugins) {
|
|
3299
|
-
if (!hasDefaultExport(input)) {
|
|
3300
|
-
return input + `
|
|
3301
|
-
const ${as} = {}`;
|
|
3302
|
-
}
|
|
3303
|
-
let replaced;
|
|
3304
|
-
const classMatch = input.match(exportDefaultClassRE);
|
|
3305
|
-
if (classMatch) {
|
|
3306
|
-
replaced = input.replace(exportDefaultClassRE, "$1class $2") + `
|
|
3307
|
-
const ${as} = ${classMatch[2]}`;
|
|
3308
|
-
} else {
|
|
3309
|
-
replaced = input.replace(defaultExportRE, `$1const ${as} =`);
|
|
3310
|
-
}
|
|
3311
|
-
if (!hasDefaultExport(replaced)) {
|
|
3312
|
-
return replaced;
|
|
3313
|
-
}
|
|
3314
|
-
const s = new MagicString(input);
|
|
3315
3296
|
const ast = parser$2.parse(input, {
|
|
3316
3297
|
sourceType: "module",
|
|
3317
3298
|
plugins: parserPlugins
|
|
3318
3299
|
}).program.body;
|
|
3300
|
+
const s = new MagicString(input);
|
|
3301
|
+
rewriteDefaultAST(ast, s, as);
|
|
3302
|
+
return s.toString();
|
|
3303
|
+
}
|
|
3304
|
+
function rewriteDefaultAST(ast, s, as) {
|
|
3305
|
+
if (!hasDefaultExport(ast)) {
|
|
3306
|
+
s.append(`
|
|
3307
|
+
const ${as} = {}`);
|
|
3308
|
+
return;
|
|
3309
|
+
}
|
|
3319
3310
|
ast.forEach((node) => {
|
|
3320
3311
|
if (node.type === "ExportDefaultDeclaration") {
|
|
3321
3312
|
if (node.declaration.type === "ClassDeclaration") {
|
|
3322
|
-
|
|
3313
|
+
let start = node.declaration.decorators && node.declaration.decorators.length > 0 ? node.declaration.decorators[node.declaration.decorators.length - 1].end : node.start;
|
|
3314
|
+
s.overwrite(start, node.declaration.id.start, ` class `);
|
|
3323
3315
|
s.append(`
|
|
3324
3316
|
const ${as} = ${node.declaration.id.name}`);
|
|
3325
3317
|
} else {
|
|
3326
3318
|
s.overwrite(node.start, node.declaration.start, `const ${as} = `);
|
|
3327
3319
|
}
|
|
3328
|
-
}
|
|
3329
|
-
if (node.type === "ExportNamedDeclaration") {
|
|
3320
|
+
} else if (node.type === "ExportNamedDeclaration") {
|
|
3330
3321
|
for (const specifier of node.specifiers) {
|
|
3331
3322
|
if (specifier.type === "ExportSpecifier" && specifier.exported.type === "Identifier" && specifier.exported.name === "default") {
|
|
3332
3323
|
if (node.source) {
|
|
3333
3324
|
if (specifier.local.name === "default") {
|
|
3334
|
-
const end2 = specifierEnd(input, specifier.local.end, node.end);
|
|
3335
3325
|
s.prepend(
|
|
3336
3326
|
`import { default as __VUE_DEFAULT__ } from '${node.source.value}'
|
|
3337
3327
|
`
|
|
3338
3328
|
);
|
|
3339
|
-
s
|
|
3329
|
+
const end2 = specifierEnd(s, specifier.local.end, node.end);
|
|
3330
|
+
s.remove(specifier.start, end2);
|
|
3340
3331
|
s.append(`
|
|
3341
3332
|
const ${as} = __VUE_DEFAULT__`);
|
|
3342
3333
|
continue;
|
|
3343
3334
|
} else {
|
|
3344
|
-
const end2 = specifierEnd(
|
|
3345
|
-
input,
|
|
3346
|
-
specifier.exported.end,
|
|
3347
|
-
node.end
|
|
3348
|
-
);
|
|
3349
3335
|
s.prepend(
|
|
3350
|
-
`import { ${
|
|
3336
|
+
`import { ${s.slice(
|
|
3351
3337
|
specifier.local.start,
|
|
3352
3338
|
specifier.local.end
|
|
3353
|
-
)} } from '${node.source.value}'
|
|
3339
|
+
)} as __VUE_DEFAULT__ } from '${node.source.value}'
|
|
3354
3340
|
`
|
|
3355
3341
|
);
|
|
3356
|
-
s
|
|
3342
|
+
const end2 = specifierEnd(s, specifier.exported.end, node.end);
|
|
3343
|
+
s.remove(specifier.start, end2);
|
|
3357
3344
|
s.append(`
|
|
3358
|
-
const ${as} =
|
|
3345
|
+
const ${as} = __VUE_DEFAULT__`);
|
|
3359
3346
|
continue;
|
|
3360
3347
|
}
|
|
3361
3348
|
}
|
|
3362
|
-
const end = specifierEnd(
|
|
3363
|
-
s.
|
|
3349
|
+
const end = specifierEnd(s, specifier.end, node.end);
|
|
3350
|
+
s.remove(specifier.start, end);
|
|
3364
3351
|
s.append(`
|
|
3365
3352
|
const ${as} = ${specifier.local.name}`);
|
|
3366
3353
|
}
|
|
3367
3354
|
}
|
|
3368
3355
|
}
|
|
3369
3356
|
});
|
|
3370
|
-
return s.toString();
|
|
3371
3357
|
}
|
|
3372
|
-
function hasDefaultExport(
|
|
3373
|
-
|
|
3358
|
+
function hasDefaultExport(ast) {
|
|
3359
|
+
for (const stmt of ast) {
|
|
3360
|
+
if (stmt.type === "ExportDefaultDeclaration") {
|
|
3361
|
+
return true;
|
|
3362
|
+
} else if (stmt.type === "ExportNamedDeclaration" && stmt.specifiers.some(
|
|
3363
|
+
(spec) => spec.exported.name === "default"
|
|
3364
|
+
)) {
|
|
3365
|
+
return true;
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
return false;
|
|
3374
3369
|
}
|
|
3375
|
-
function specifierEnd(
|
|
3370
|
+
function specifierEnd(s, end, nodeEnd) {
|
|
3376
3371
|
let hasCommas = false;
|
|
3377
3372
|
let oldEnd = end;
|
|
3378
3373
|
while (end < nodeEnd) {
|
|
3379
|
-
if (/\s/.test(
|
|
3374
|
+
if (/\s/.test(s.slice(end, end + 1))) {
|
|
3380
3375
|
end++;
|
|
3381
|
-
} else if (
|
|
3376
|
+
} else if (s.slice(end, end + 1) === ",") {
|
|
3382
3377
|
end++;
|
|
3383
3378
|
hasCommas = true;
|
|
3384
3379
|
break;
|
|
3385
|
-
} else if (
|
|
3380
|
+
} else if (s.slice(end, end + 1) === "}") {
|
|
3386
3381
|
break;
|
|
3387
3382
|
}
|
|
3388
3383
|
}
|
|
3389
3384
|
return hasCommas ? end : oldEnd;
|
|
3390
3385
|
}
|
|
3391
3386
|
|
|
3387
|
+
function transformDestructuredProps(ast, s, offset = 0, knownProps, error, watchMethodName = "watch") {
|
|
3388
|
+
const rootScope = {};
|
|
3389
|
+
const scopeStack = [rootScope];
|
|
3390
|
+
let currentScope = rootScope;
|
|
3391
|
+
const excludedIds = /* @__PURE__ */ new WeakSet();
|
|
3392
|
+
const parentStack = [];
|
|
3393
|
+
const propsLocalToPublicMap = /* @__PURE__ */ Object.create(null);
|
|
3394
|
+
for (const key in knownProps) {
|
|
3395
|
+
const { local } = knownProps[key];
|
|
3396
|
+
rootScope[local] = true;
|
|
3397
|
+
propsLocalToPublicMap[local] = key;
|
|
3398
|
+
}
|
|
3399
|
+
function registerLocalBinding(id) {
|
|
3400
|
+
excludedIds.add(id);
|
|
3401
|
+
if (currentScope) {
|
|
3402
|
+
currentScope[id.name] = false;
|
|
3403
|
+
} else {
|
|
3404
|
+
error(
|
|
3405
|
+
"registerBinding called without active scope, something is wrong.",
|
|
3406
|
+
id
|
|
3407
|
+
);
|
|
3408
|
+
}
|
|
3409
|
+
}
|
|
3410
|
+
function walkScope(node, isRoot = false) {
|
|
3411
|
+
for (const stmt of node.body) {
|
|
3412
|
+
if (stmt.type === "VariableDeclaration") {
|
|
3413
|
+
walkVariableDeclaration(stmt, isRoot);
|
|
3414
|
+
} else if (stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") {
|
|
3415
|
+
if (stmt.declare || !stmt.id)
|
|
3416
|
+
continue;
|
|
3417
|
+
registerLocalBinding(stmt.id);
|
|
3418
|
+
} else if ((stmt.type === "ForOfStatement" || stmt.type === "ForInStatement") && stmt.left.type === "VariableDeclaration") {
|
|
3419
|
+
walkVariableDeclaration(stmt.left);
|
|
3420
|
+
} else if (stmt.type === "ExportNamedDeclaration" && stmt.declaration && stmt.declaration.type === "VariableDeclaration") {
|
|
3421
|
+
walkVariableDeclaration(stmt.declaration, isRoot);
|
|
3422
|
+
} else if (stmt.type === "LabeledStatement" && stmt.body.type === "VariableDeclaration") {
|
|
3423
|
+
walkVariableDeclaration(stmt.body, isRoot);
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
function walkVariableDeclaration(stmt, isRoot = false) {
|
|
3428
|
+
if (stmt.declare) {
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
for (const decl of stmt.declarations) {
|
|
3432
|
+
const isDefineProps = isRoot && decl.init && compilerCore.isCallOf(compilerCore.unwrapTSNode(decl.init), "defineProps");
|
|
3433
|
+
for (const id of compilerCore.extractIdentifiers(decl.id)) {
|
|
3434
|
+
if (isDefineProps) {
|
|
3435
|
+
excludedIds.add(id);
|
|
3436
|
+
} else {
|
|
3437
|
+
registerLocalBinding(id);
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
function rewriteId(scope, id, parent, parentStack2) {
|
|
3443
|
+
if (shared.hasOwn(scope, id.name)) {
|
|
3444
|
+
const binding = scope[id.name];
|
|
3445
|
+
if (binding) {
|
|
3446
|
+
if (parent.type === "AssignmentExpression" && id === parent.left || parent.type === "UpdateExpression") {
|
|
3447
|
+
error(`Cannot assign to destructured props as they are readonly.`, id);
|
|
3448
|
+
}
|
|
3449
|
+
if (compilerCore.isStaticProperty(parent) && parent.shorthand) {
|
|
3450
|
+
if (!parent.inPattern || compilerCore.isInDestructureAssignment(parent, parentStack2)) {
|
|
3451
|
+
s.appendLeft(
|
|
3452
|
+
id.end + offset,
|
|
3453
|
+
`: ${shared.genPropsAccessExp(propsLocalToPublicMap[id.name])}`
|
|
3454
|
+
);
|
|
3455
|
+
}
|
|
3456
|
+
} else {
|
|
3457
|
+
s.overwrite(
|
|
3458
|
+
id.start + offset,
|
|
3459
|
+
id.end + offset,
|
|
3460
|
+
shared.genPropsAccessExp(propsLocalToPublicMap[id.name])
|
|
3461
|
+
);
|
|
3462
|
+
}
|
|
3463
|
+
}
|
|
3464
|
+
return true;
|
|
3465
|
+
}
|
|
3466
|
+
return false;
|
|
3467
|
+
}
|
|
3468
|
+
walkScope(ast, true);
|
|
3469
|
+
estreeWalker.walk(ast, {
|
|
3470
|
+
enter(node, parent) {
|
|
3471
|
+
parent && parentStack.push(parent);
|
|
3472
|
+
if (parent && parent.type.startsWith("TS") && parent.type !== "TSAsExpression" && parent.type !== "TSNonNullExpression" && parent.type !== "TSTypeAssertion") {
|
|
3473
|
+
return this.skip();
|
|
3474
|
+
}
|
|
3475
|
+
if (compilerCore.isCallOf(node, watchMethodName)) {
|
|
3476
|
+
const arg = compilerCore.unwrapTSNode(node.arguments[0]);
|
|
3477
|
+
if (arg.type === "Identifier") {
|
|
3478
|
+
error(
|
|
3479
|
+
`"${arg.name}" is a destructured prop and cannot be directly watched. Use a getter () => ${arg.name} instead.`,
|
|
3480
|
+
arg
|
|
3481
|
+
);
|
|
3482
|
+
}
|
|
3483
|
+
}
|
|
3484
|
+
if (compilerCore.isFunctionType(node)) {
|
|
3485
|
+
scopeStack.push(currentScope = {});
|
|
3486
|
+
compilerCore.walkFunctionParams(node, registerLocalBinding);
|
|
3487
|
+
if (node.body.type === "BlockStatement") {
|
|
3488
|
+
walkScope(node.body);
|
|
3489
|
+
}
|
|
3490
|
+
return;
|
|
3491
|
+
}
|
|
3492
|
+
if (node.type === "CatchClause") {
|
|
3493
|
+
scopeStack.push(currentScope = {});
|
|
3494
|
+
if (node.param && node.param.type === "Identifier") {
|
|
3495
|
+
registerLocalBinding(node.param);
|
|
3496
|
+
}
|
|
3497
|
+
walkScope(node.body);
|
|
3498
|
+
return;
|
|
3499
|
+
}
|
|
3500
|
+
if (node.type === "BlockStatement" && !compilerCore.isFunctionType(parent)) {
|
|
3501
|
+
scopeStack.push(currentScope = {});
|
|
3502
|
+
walkScope(node);
|
|
3503
|
+
return;
|
|
3504
|
+
}
|
|
3505
|
+
if (node.type === "Identifier") {
|
|
3506
|
+
if (compilerCore.isReferencedIdentifier(node, parent, parentStack) && !excludedIds.has(node)) {
|
|
3507
|
+
let i = scopeStack.length;
|
|
3508
|
+
while (i--) {
|
|
3509
|
+
if (rewriteId(scopeStack[i], node, parent, parentStack)) {
|
|
3510
|
+
return;
|
|
3511
|
+
}
|
|
3512
|
+
}
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
},
|
|
3516
|
+
leave(node, parent) {
|
|
3517
|
+
parent && parentStack.pop();
|
|
3518
|
+
if (node.type === "BlockStatement" && !compilerCore.isFunctionType(parent) || compilerCore.isFunctionType(node)) {
|
|
3519
|
+
scopeStack.pop();
|
|
3520
|
+
currentScope = scopeStack[scopeStack.length - 1] || null;
|
|
3521
|
+
}
|
|
3522
|
+
}
|
|
3523
|
+
});
|
|
3524
|
+
}
|
|
3525
|
+
|
|
3392
3526
|
const DEFINE_PROPS = "defineProps";
|
|
3393
3527
|
const DEFINE_EMITS = "defineEmits";
|
|
3394
3528
|
const DEFINE_EXPOSE = "defineExpose";
|
|
3395
3529
|
const WITH_DEFAULTS = "withDefaults";
|
|
3396
|
-
const
|
|
3530
|
+
const DEFINE_OPTIONS = "defineOptions";
|
|
3397
3531
|
const isBuiltInDir = shared.makeMap(
|
|
3398
3532
|
`once,memo,if,for,else,else-if,slot,text,html,on,bind,model,show,cloak,is`
|
|
3399
3533
|
);
|
|
3400
3534
|
function compileScript(sfc, options) {
|
|
3401
3535
|
var _a;
|
|
3402
3536
|
let { script, scriptSetup, source, filename } = sfc;
|
|
3403
|
-
const enableReactivityTransform = !!options.reactivityTransform
|
|
3404
|
-
const enablePropsTransform = !!options.reactivityTransform || !!options.propsDestructureTransform;
|
|
3537
|
+
const enableReactivityTransform = !!options.reactivityTransform;
|
|
3405
3538
|
const isProd = !!options.isProd;
|
|
3406
3539
|
const genSourceMap = options.sourceMap !== false;
|
|
3540
|
+
const hoistStatic = options.hoistStatic !== false && !script;
|
|
3407
3541
|
let refBindings;
|
|
3408
3542
|
if (!options.id) {
|
|
3409
3543
|
warnOnce(
|
|
@@ -3415,6 +3549,9 @@ Upgrade your vite or vue-loader version for compatibility with the latest experi
|
|
|
3415
3549
|
const cssVars = sfc.cssVars;
|
|
3416
3550
|
const scriptLang = script && script.lang;
|
|
3417
3551
|
const scriptSetupLang = scriptSetup && scriptSetup.lang;
|
|
3552
|
+
const genDefaultAs = options.genDefaultAs ? `const ${options.genDefaultAs} =` : `export default`;
|
|
3553
|
+
const normalScriptDefaultVar = `__default__`;
|
|
3554
|
+
const isJS = scriptLang === "js" || scriptLang === "jsx" || scriptSetupLang === "js" || scriptSetupLang === "jsx";
|
|
3418
3555
|
const isTS = scriptLang === "ts" || scriptLang === "tsx" || scriptSetupLang === "ts" || scriptSetupLang === "tsx";
|
|
3419
3556
|
const plugins = [];
|
|
3420
3557
|
if (!isTS || scriptLang === "tsx" || scriptSetupLang === "tsx") {
|
|
@@ -3437,7 +3574,7 @@ Upgrade your vite or vue-loader version for compatibility with the latest experi
|
|
|
3437
3574
|
if (!script) {
|
|
3438
3575
|
throw new Error(`[@vue/compiler-sfc] SFC contains no <script> tags.`);
|
|
3439
3576
|
}
|
|
3440
|
-
if (scriptLang && !
|
|
3577
|
+
if (scriptLang && !isJS && !isTS) {
|
|
3441
3578
|
return script;
|
|
3442
3579
|
}
|
|
3443
3580
|
try {
|
|
@@ -3470,16 +3607,24 @@ Upgrade your vite or vue-loader version for compatibility with the latest experi
|
|
|
3470
3607
|
});
|
|
3471
3608
|
}
|
|
3472
3609
|
}
|
|
3473
|
-
if (cssVars.length) {
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3610
|
+
if (cssVars.length || options.genDefaultAs) {
|
|
3611
|
+
const defaultVar = options.genDefaultAs || normalScriptDefaultVar;
|
|
3612
|
+
const s2 = new MagicString(content);
|
|
3613
|
+
rewriteDefaultAST(scriptAst2.body, s2, defaultVar);
|
|
3614
|
+
content = s2.toString();
|
|
3615
|
+
if (cssVars.length) {
|
|
3616
|
+
content += genNormalScriptCssVarsCode(
|
|
3617
|
+
cssVars,
|
|
3618
|
+
bindings,
|
|
3619
|
+
scopeId,
|
|
3620
|
+
isProd,
|
|
3621
|
+
defaultVar
|
|
3622
|
+
);
|
|
3623
|
+
}
|
|
3624
|
+
if (!options.genDefaultAs) {
|
|
3625
|
+
content += `
|
|
3626
|
+
export default ${defaultVar}`;
|
|
3627
|
+
}
|
|
3483
3628
|
}
|
|
3484
3629
|
return {
|
|
3485
3630
|
...script,
|
|
@@ -3497,7 +3642,7 @@ export default ${DEFAULT_VAR}`;
|
|
|
3497
3642
|
`[@vue/compiler-sfc] <script> and <script setup> must have the same language type.`
|
|
3498
3643
|
);
|
|
3499
3644
|
}
|
|
3500
|
-
if (scriptSetupLang && !
|
|
3645
|
+
if (scriptSetupLang && !isJS && !isTS) {
|
|
3501
3646
|
return scriptSetup;
|
|
3502
3647
|
}
|
|
3503
3648
|
const bindingMetadata = {};
|
|
@@ -3511,6 +3656,7 @@ export default ${DEFAULT_VAR}`;
|
|
|
3511
3656
|
let hasDefineExposeCall = false;
|
|
3512
3657
|
let hasDefaultExportName = false;
|
|
3513
3658
|
let hasDefaultExportRender = false;
|
|
3659
|
+
let hasDefineOptionsCall = false;
|
|
3514
3660
|
let propsRuntimeDecl;
|
|
3515
3661
|
let propsRuntimeDefaults;
|
|
3516
3662
|
let propsDestructureDecl;
|
|
@@ -3522,6 +3668,7 @@ export default ${DEFAULT_VAR}`;
|
|
|
3522
3668
|
let emitsTypeDecl;
|
|
3523
3669
|
let emitsTypeDeclRaw;
|
|
3524
3670
|
let emitIdentifier;
|
|
3671
|
+
let optionsRuntimeDecl;
|
|
3525
3672
|
let hasAwait = false;
|
|
3526
3673
|
let hasInlinedSsrRenderFn = false;
|
|
3527
3674
|
const typeDeclaredProps = {};
|
|
@@ -3582,15 +3729,15 @@ ${shared.generateCodeFrame(
|
|
|
3582
3729
|
}
|
|
3583
3730
|
userImports[local] = {
|
|
3584
3731
|
isType,
|
|
3585
|
-
imported
|
|
3732
|
+
imported,
|
|
3586
3733
|
local,
|
|
3587
3734
|
source: source2,
|
|
3588
3735
|
isFromSetup,
|
|
3589
3736
|
isUsedInTemplate
|
|
3590
3737
|
};
|
|
3591
3738
|
}
|
|
3592
|
-
function processDefineProps(node, declId
|
|
3593
|
-
if (!isCallOf(node, DEFINE_PROPS)) {
|
|
3739
|
+
function processDefineProps(node, declId) {
|
|
3740
|
+
if (!CompilerDOM.isCallOf(node, DEFINE_PROPS)) {
|
|
3594
3741
|
return false;
|
|
3595
3742
|
}
|
|
3596
3743
|
if (hasDefinePropsCall) {
|
|
@@ -3618,8 +3765,7 @@ ${shared.generateCodeFrame(
|
|
|
3618
3765
|
}
|
|
3619
3766
|
}
|
|
3620
3767
|
if (declId) {
|
|
3621
|
-
|
|
3622
|
-
if (enablePropsTransform && declId.type === "ObjectPattern") {
|
|
3768
|
+
if (declId.type === "ObjectPattern") {
|
|
3623
3769
|
propsDestructureDecl = declId;
|
|
3624
3770
|
for (const prop of declId.properties) {
|
|
3625
3771
|
if (prop.type === "ObjectProperty") {
|
|
@@ -3640,13 +3786,11 @@ ${shared.generateCodeFrame(
|
|
|
3640
3786
|
}
|
|
3641
3787
|
propsDestructuredBindings[propKey] = {
|
|
3642
3788
|
local: left.name,
|
|
3643
|
-
default: right
|
|
3644
|
-
isConst
|
|
3789
|
+
default: right
|
|
3645
3790
|
};
|
|
3646
3791
|
} else if (prop.value.type === "Identifier") {
|
|
3647
3792
|
propsDestructuredBindings[propKey] = {
|
|
3648
|
-
local: prop.value.name
|
|
3649
|
-
isConst
|
|
3793
|
+
local: prop.value.name
|
|
3650
3794
|
};
|
|
3651
3795
|
} else {
|
|
3652
3796
|
error(
|
|
@@ -3665,10 +3809,13 @@ ${shared.generateCodeFrame(
|
|
|
3665
3809
|
return true;
|
|
3666
3810
|
}
|
|
3667
3811
|
function processWithDefaults(node, declId, declKind) {
|
|
3668
|
-
if (!isCallOf(node, WITH_DEFAULTS)) {
|
|
3812
|
+
if (!CompilerDOM.isCallOf(node, WITH_DEFAULTS)) {
|
|
3669
3813
|
return false;
|
|
3670
3814
|
}
|
|
3671
|
-
|
|
3815
|
+
warnOnce(
|
|
3816
|
+
`withDefaults() has been deprecated. Props destructure is now reactive by default - use destructure with default values instead.`
|
|
3817
|
+
);
|
|
3818
|
+
if (processDefineProps(node.arguments[0], declId)) {
|
|
3672
3819
|
if (propsRuntimeDecl) {
|
|
3673
3820
|
error(
|
|
3674
3821
|
`${WITH_DEFAULTS} can only be used with type-based ${DEFINE_PROPS} declaration.`,
|
|
@@ -3698,7 +3845,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3698
3845
|
return true;
|
|
3699
3846
|
}
|
|
3700
3847
|
function processDefineEmits(node, declId) {
|
|
3701
|
-
if (!isCallOf(node, DEFINE_EMITS)) {
|
|
3848
|
+
if (!CompilerDOM.isCallOf(node, DEFINE_EMITS)) {
|
|
3702
3849
|
return false;
|
|
3703
3850
|
}
|
|
3704
3851
|
if (hasDefineEmitCall) {
|
|
@@ -3779,6 +3926,53 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3779
3926
|
});
|
|
3780
3927
|
});
|
|
3781
3928
|
}
|
|
3929
|
+
function processDefineOptions(node) {
|
|
3930
|
+
if (!CompilerDOM.isCallOf(node, DEFINE_OPTIONS)) {
|
|
3931
|
+
return false;
|
|
3932
|
+
}
|
|
3933
|
+
if (hasDefineOptionsCall) {
|
|
3934
|
+
error(`duplicate ${DEFINE_OPTIONS}() call`, node);
|
|
3935
|
+
}
|
|
3936
|
+
if (node.typeParameters) {
|
|
3937
|
+
error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node);
|
|
3938
|
+
}
|
|
3939
|
+
hasDefineOptionsCall = true;
|
|
3940
|
+
optionsRuntimeDecl = node.arguments[0];
|
|
3941
|
+
let propsOption = void 0;
|
|
3942
|
+
let emitsOption = void 0;
|
|
3943
|
+
let exposeOption = void 0;
|
|
3944
|
+
if (optionsRuntimeDecl.type === "ObjectExpression") {
|
|
3945
|
+
for (const prop of optionsRuntimeDecl.properties) {
|
|
3946
|
+
if ((prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier") {
|
|
3947
|
+
if (prop.key.name === "props")
|
|
3948
|
+
propsOption = prop;
|
|
3949
|
+
if (prop.key.name === "emits")
|
|
3950
|
+
emitsOption = prop;
|
|
3951
|
+
if (prop.key.name === "expose")
|
|
3952
|
+
exposeOption = prop;
|
|
3953
|
+
}
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
if (propsOption) {
|
|
3957
|
+
error(
|
|
3958
|
+
`${DEFINE_OPTIONS}() cannot be used to declare props. Use ${DEFINE_PROPS}() instead.`,
|
|
3959
|
+
propsOption
|
|
3960
|
+
);
|
|
3961
|
+
}
|
|
3962
|
+
if (emitsOption) {
|
|
3963
|
+
error(
|
|
3964
|
+
`${DEFINE_OPTIONS}() cannot be used to declare emits. Use ${DEFINE_EMITS}() instead.`,
|
|
3965
|
+
emitsOption
|
|
3966
|
+
);
|
|
3967
|
+
}
|
|
3968
|
+
if (exposeOption) {
|
|
3969
|
+
error(
|
|
3970
|
+
`${DEFINE_OPTIONS}() cannot be used to declare expose. Use ${DEFINE_EXPOSE}() instead.`,
|
|
3971
|
+
exposeOption
|
|
3972
|
+
);
|
|
3973
|
+
}
|
|
3974
|
+
return true;
|
|
3975
|
+
}
|
|
3782
3976
|
function resolveQualifiedType(node, qualifier) {
|
|
3783
3977
|
if (qualifier(node)) {
|
|
3784
3978
|
return node;
|
|
@@ -3807,7 +4001,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3807
4001
|
}
|
|
3808
4002
|
}
|
|
3809
4003
|
function processDefineExpose(node) {
|
|
3810
|
-
if (isCallOf(node, DEFINE_EXPOSE)) {
|
|
4004
|
+
if (CompilerDOM.isCallOf(node, DEFINE_EXPOSE)) {
|
|
3811
4005
|
if (hasDefineExposeCall) {
|
|
3812
4006
|
error(`duplicate ${DEFINE_EXPOSE}() call`, node);
|
|
3813
4007
|
}
|
|
@@ -3820,7 +4014,8 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3820
4014
|
if (!node)
|
|
3821
4015
|
return;
|
|
3822
4016
|
CompilerDOM.walkIdentifiers(node, (id) => {
|
|
3823
|
-
|
|
4017
|
+
const binding = setupBindings[id.name];
|
|
4018
|
+
if (binding && (binding !== "literal-const" || !hoistStatic)) {
|
|
3824
4019
|
error(
|
|
3825
4020
|
`\`${method}()\` in <script setup> cannot reference locally declared variables because it will be hoisted outside of the setup() function. If your component options require initialization in the module scope, use a separate normal <script> to export the options instead.`,
|
|
3826
4021
|
id
|
|
@@ -3867,9 +4062,9 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3867
4062
|
let propsDecls = `{
|
|
3868
4063
|
${keys.map((key) => {
|
|
3869
4064
|
let defaultString;
|
|
3870
|
-
const destructured = genDestructuredDefaultValue(key);
|
|
4065
|
+
const destructured = genDestructuredDefaultValue(key, props[key].type);
|
|
3871
4066
|
if (destructured) {
|
|
3872
|
-
defaultString = `default: ${destructured}`;
|
|
4067
|
+
defaultString = `default: ${destructured.valueString}${destructured.needSkipFactory ? `, skipFactory: true` : ``}`;
|
|
3873
4068
|
} else if (hasStaticDefaults) {
|
|
3874
4069
|
const prop = propsRuntimeDefaults.properties.find((node) => {
|
|
3875
4070
|
if (node.type === "SpreadElement")
|
|
@@ -3890,11 +4085,11 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3890
4085
|
}
|
|
3891
4086
|
}
|
|
3892
4087
|
}
|
|
3893
|
-
const { type, required } = props[key];
|
|
4088
|
+
const { type, required, skipCheck } = props[key];
|
|
3894
4089
|
if (!isProd) {
|
|
3895
4090
|
return `${key}: { type: ${toRuntimeTypeString(
|
|
3896
4091
|
type
|
|
3897
|
-
)}, required: ${required}${defaultString ? `, ${defaultString}` : ``} }`;
|
|
4092
|
+
)}, required: ${required}${skipCheck ? ", skipCheck: true" : ""}${defaultString ? `, ${defaultString}` : ``} }`;
|
|
3898
4093
|
} else if (type.some(
|
|
3899
4094
|
(el) => el === "Boolean" || (!hasStaticDefaults || defaultString) && el === "Function"
|
|
3900
4095
|
)) {
|
|
@@ -3913,15 +4108,30 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3913
4108
|
return `
|
|
3914
4109
|
props: ${propsDecls},`;
|
|
3915
4110
|
}
|
|
3916
|
-
function genDestructuredDefaultValue(key) {
|
|
4111
|
+
function genDestructuredDefaultValue(key, inferredType) {
|
|
3917
4112
|
const destructured = propsDestructuredBindings[key];
|
|
3918
|
-
|
|
4113
|
+
const defaultVal = destructured && destructured.default;
|
|
4114
|
+
if (defaultVal) {
|
|
3919
4115
|
const value = scriptSetup.content.slice(
|
|
3920
|
-
|
|
3921
|
-
|
|
4116
|
+
defaultVal.start,
|
|
4117
|
+
defaultVal.end
|
|
3922
4118
|
);
|
|
3923
|
-
const
|
|
3924
|
-
|
|
4119
|
+
const unwrapped = CompilerDOM.unwrapTSNode(defaultVal);
|
|
4120
|
+
if (inferredType && inferredType.length && !inferredType.includes(UNKNOWN_TYPE)) {
|
|
4121
|
+
const valueType = inferValueType(unwrapped);
|
|
4122
|
+
if (valueType && !inferredType.includes(valueType)) {
|
|
4123
|
+
error(
|
|
4124
|
+
`Default value of prop "${key}" does not match declared type.`,
|
|
4125
|
+
unwrapped
|
|
4126
|
+
);
|
|
4127
|
+
}
|
|
4128
|
+
}
|
|
4129
|
+
const needSkipFactory = !inferredType && (CompilerDOM.isFunctionType(unwrapped) || unwrapped.type === "Identifier");
|
|
4130
|
+
const needFactoryWrap = !needSkipFactory && !isLiteralNode(unwrapped) && !(inferredType == null ? void 0 : inferredType.includes("Function"));
|
|
4131
|
+
return {
|
|
4132
|
+
valueString: needFactoryWrap ? `() => (${value})` : value,
|
|
4133
|
+
needSkipFactory
|
|
4134
|
+
};
|
|
3925
4135
|
}
|
|
3926
4136
|
}
|
|
3927
4137
|
function genSetupPropsType(node) {
|
|
@@ -3974,7 +4184,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
3974
4184
|
for (const node of scriptAst.body) {
|
|
3975
4185
|
if (node.type === "ImportDeclaration") {
|
|
3976
4186
|
for (const specifier of node.specifiers) {
|
|
3977
|
-
const imported =
|
|
4187
|
+
const imported = CompilerDOM.getImportedName(specifier);
|
|
3978
4188
|
registerUserImport(
|
|
3979
4189
|
node.source.value,
|
|
3980
4190
|
specifier.local.name,
|
|
@@ -4004,10 +4214,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
4004
4214
|
for (let i = 0; i < node.specifiers.length; i++) {
|
|
4005
4215
|
const specifier = node.specifiers[i];
|
|
4006
4216
|
const local = specifier.local.name;
|
|
4007
|
-
|
|
4008
|
-
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
4009
|
-
imported = "*";
|
|
4010
|
-
}
|
|
4217
|
+
const imported = CompilerDOM.getImportedName(specifier);
|
|
4011
4218
|
const source2 = node.source.value;
|
|
4012
4219
|
const existing = userImports[local];
|
|
4013
4220
|
if (source2 === "vue" && (imported === DEFINE_PROPS || imported === DEFINE_EMITS || imported === DEFINE_EXPOSE)) {
|
|
@@ -4050,7 +4257,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
4050
4257
|
let optionProperties;
|
|
4051
4258
|
if (defaultExport.declaration.type === "ObjectExpression") {
|
|
4052
4259
|
optionProperties = defaultExport.declaration.properties;
|
|
4053
|
-
} else if (defaultExport.declaration.type === "CallExpression" && defaultExport.declaration.arguments[0].type === "ObjectExpression") {
|
|
4260
|
+
} else if (defaultExport.declaration.type === "CallExpression" && defaultExport.declaration.arguments[0] && defaultExport.declaration.arguments[0].type === "ObjectExpression") {
|
|
4054
4261
|
optionProperties = defaultExport.declaration.arguments[0].properties;
|
|
4055
4262
|
}
|
|
4056
4263
|
if (optionProperties) {
|
|
@@ -4065,7 +4272,7 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
4065
4272
|
}
|
|
4066
4273
|
const start = node.start + scriptStartOffset;
|
|
4067
4274
|
const end = node.declaration.start + scriptStartOffset;
|
|
4068
|
-
s.overwrite(start, end, `const ${
|
|
4275
|
+
s.overwrite(start, end, `const ${normalScriptDefaultVar} = `);
|
|
4069
4276
|
} else if (node.type === "ExportNamedDeclaration") {
|
|
4070
4277
|
const defaultSpecifier = node.specifiers.find(
|
|
4071
4278
|
(s2) => s2.exported.type === "Identifier" && s2.exported.name === "default"
|
|
@@ -4085,14 +4292,14 @@ Prefer using destructure default values, e.g. const { foo = 1 } = defineProps(..
|
|
|
4085
4292
|
}
|
|
4086
4293
|
if (node.source) {
|
|
4087
4294
|
s.prepend(
|
|
4088
|
-
`import { ${defaultSpecifier.local.name} as ${
|
|
4295
|
+
`import { ${defaultSpecifier.local.name} as ${normalScriptDefaultVar} } from '${node.source.value}'
|
|
4089
4296
|
`
|
|
4090
4297
|
);
|
|
4091
4298
|
} else {
|
|
4092
4299
|
s.appendLeft(
|
|
4093
4300
|
scriptEndOffset,
|
|
4094
4301
|
`
|
|
4095
|
-
const ${
|
|
4302
|
+
const ${normalScriptDefaultVar} = ${defaultSpecifier.local.name}
|
|
4096
4303
|
`
|
|
4097
4304
|
);
|
|
4098
4305
|
}
|
|
@@ -4131,45 +4338,60 @@ const ${DEFAULT_VAR} = ${defaultSpecifier.local.name}
|
|
|
4131
4338
|
);
|
|
4132
4339
|
}
|
|
4133
4340
|
if (node.type === "ExpressionStatement") {
|
|
4134
|
-
|
|
4341
|
+
const expr = CompilerDOM.unwrapTSNode(node.expression);
|
|
4342
|
+
if (processDefineProps(expr) || processDefineEmits(expr) || processDefineOptions(expr) || processWithDefaults(expr)) {
|
|
4135
4343
|
s.remove(node.start + startOffset, node.end + startOffset);
|
|
4136
|
-
} else if (processDefineExpose(
|
|
4137
|
-
const callee =
|
|
4344
|
+
} else if (processDefineExpose(expr)) {
|
|
4345
|
+
const callee = expr.callee;
|
|
4138
4346
|
s.overwrite(
|
|
4139
4347
|
callee.start + startOffset,
|
|
4140
4348
|
callee.end + startOffset,
|
|
4141
|
-
"
|
|
4349
|
+
"__expose"
|
|
4142
4350
|
);
|
|
4143
4351
|
}
|
|
4144
4352
|
}
|
|
4145
4353
|
if (node.type === "VariableDeclaration" && !node.declare) {
|
|
4146
4354
|
const total = node.declarations.length;
|
|
4147
4355
|
let left = total;
|
|
4356
|
+
let lastNonRemoved;
|
|
4148
4357
|
for (let i = 0; i < total; i++) {
|
|
4149
4358
|
const decl = node.declarations[i];
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4359
|
+
const init = decl.init && CompilerDOM.unwrapTSNode(decl.init);
|
|
4360
|
+
if (init) {
|
|
4361
|
+
if (processDefineOptions(init)) {
|
|
4362
|
+
error(
|
|
4363
|
+
`${DEFINE_OPTIONS}() has no returning value, it cannot be assigned.`,
|
|
4364
|
+
node
|
|
4365
|
+
);
|
|
4366
|
+
}
|
|
4367
|
+
const isDefineProps = processDefineProps(init, decl.id) || processWithDefaults(init, decl.id, node.kind);
|
|
4368
|
+
const isDefineEmits = processDefineEmits(init, decl.id);
|
|
4153
4369
|
if (isDefineProps || isDefineEmits) {
|
|
4154
4370
|
if (left === 1) {
|
|
4155
4371
|
s.remove(node.start + startOffset, node.end + startOffset);
|
|
4156
4372
|
} else {
|
|
4157
4373
|
let start = decl.start + startOffset;
|
|
4158
4374
|
let end = decl.end + startOffset;
|
|
4159
|
-
if (i ===
|
|
4160
|
-
|
|
4375
|
+
if (i === total - 1) {
|
|
4376
|
+
start = node.declarations[lastNonRemoved].end + startOffset;
|
|
4161
4377
|
} else {
|
|
4162
|
-
|
|
4378
|
+
end = node.declarations[i + 1].start + startOffset;
|
|
4163
4379
|
}
|
|
4164
4380
|
s.remove(start, end);
|
|
4165
4381
|
left--;
|
|
4166
4382
|
}
|
|
4383
|
+
} else {
|
|
4384
|
+
lastNonRemoved = i;
|
|
4167
4385
|
}
|
|
4168
4386
|
}
|
|
4169
4387
|
}
|
|
4170
4388
|
}
|
|
4171
|
-
|
|
4172
|
-
|
|
4389
|
+
let isAllLiteral = false;
|
|
4390
|
+
if ((node.type === "VariableDeclaration" || node.type === "FunctionDeclaration" || node.type === "ClassDeclaration" || node.type === "TSEnumDeclaration") && !node.declare) {
|
|
4391
|
+
isAllLiteral = walkDeclaration(node, setupBindings, vueImportAliases);
|
|
4392
|
+
}
|
|
4393
|
+
if (hoistStatic && isAllLiteral) {
|
|
4394
|
+
hoistNode(node);
|
|
4173
4395
|
}
|
|
4174
4396
|
if (node.type === "VariableDeclaration" && !node.declare || node.type.endsWith("Statement")) {
|
|
4175
4397
|
const scope = [scriptSetupAst.body];
|
|
@@ -4207,23 +4429,31 @@ const ${DEFAULT_VAR} = ${defaultSpecifier.local.name}
|
|
|
4207
4429
|
);
|
|
4208
4430
|
}
|
|
4209
4431
|
if (isTS) {
|
|
4210
|
-
if (node.type === "TSEnumDeclaration") {
|
|
4211
|
-
registerBinding(setupBindings, node.id, "setup-const");
|
|
4212
|
-
}
|
|
4213
4432
|
if (node.type.startsWith("TS") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "VariableDeclaration" && node.declare) {
|
|
4214
4433
|
recordType(node, declaredTypes);
|
|
4215
|
-
|
|
4434
|
+
if (node.type !== "TSEnumDeclaration") {
|
|
4435
|
+
hoistNode(node);
|
|
4436
|
+
}
|
|
4216
4437
|
}
|
|
4217
4438
|
}
|
|
4218
4439
|
}
|
|
4440
|
+
if (propsDestructureDecl) {
|
|
4441
|
+
transformDestructuredProps(
|
|
4442
|
+
scriptSetupAst,
|
|
4443
|
+
s,
|
|
4444
|
+
startOffset,
|
|
4445
|
+
propsDestructuredBindings,
|
|
4446
|
+
error,
|
|
4447
|
+
vueImportAliases.watch
|
|
4448
|
+
);
|
|
4449
|
+
}
|
|
4219
4450
|
if (enableReactivityTransform && // normal <script> had ref bindings that maybe used in <script setup>
|
|
4220
|
-
(refBindings || reactivityTransform.shouldTransform(scriptSetup.content))
|
|
4451
|
+
(refBindings || reactivityTransform.shouldTransform(scriptSetup.content))) {
|
|
4221
4452
|
const { rootRefs, importedHelpers } = reactivityTransform.transformAST(
|
|
4222
4453
|
scriptSetupAst,
|
|
4223
4454
|
s,
|
|
4224
4455
|
startOffset,
|
|
4225
|
-
refBindings
|
|
4226
|
-
propsDestructuredBindings
|
|
4456
|
+
refBindings
|
|
4227
4457
|
);
|
|
4228
4458
|
refBindings = refBindings ? [...refBindings, ...rootRefs] : rootRefs;
|
|
4229
4459
|
for (const h of importedHelpers) {
|
|
@@ -4240,6 +4470,7 @@ const ${DEFAULT_VAR} = ${defaultSpecifier.local.name}
|
|
|
4240
4470
|
checkInvalidScopeReference(propsRuntimeDefaults, DEFINE_PROPS);
|
|
4241
4471
|
checkInvalidScopeReference(propsDestructureDecl, DEFINE_PROPS);
|
|
4242
4472
|
checkInvalidScopeReference(emitsRuntimeDecl, DEFINE_EMITS);
|
|
4473
|
+
checkInvalidScopeReference(optionsRuntimeDecl, DEFINE_OPTIONS);
|
|
4243
4474
|
if (script) {
|
|
4244
4475
|
if (startOffset < scriptStartOffset) {
|
|
4245
4476
|
s.remove(0, startOffset);
|
|
@@ -4299,7 +4530,7 @@ const ${DEFAULT_VAR} = ${defaultSpecifier.local.name}
|
|
|
4299
4530
|
!(options.inlineTemplate && ((_a = options.templateOptions) == null ? void 0 : _a.ssr))) {
|
|
4300
4531
|
helperImports.add(CSS_VARS_HELPER);
|
|
4301
4532
|
helperImports.add("unref");
|
|
4302
|
-
s.
|
|
4533
|
+
s.prependLeft(
|
|
4303
4534
|
startOffset,
|
|
4304
4535
|
`
|
|
4305
4536
|
${genCssVarsCode(cssVars, bindingMetadata, scopeId, isProd)}
|
|
@@ -4334,7 +4565,7 @@ const ${propsDestructureRestId} = ${helper(
|
|
|
4334
4565
|
let __temp${any}, __restore${any}
|
|
4335
4566
|
`);
|
|
4336
4567
|
}
|
|
4337
|
-
const destructureElements = hasDefineExposeCall || !options.inlineTemplate ? [`expose`] : [];
|
|
4568
|
+
const destructureElements = hasDefineExposeCall || !options.inlineTemplate ? [`expose: __expose`] : [];
|
|
4338
4569
|
if (emitIdentifier) {
|
|
4339
4570
|
destructureElements.push(
|
|
4340
4571
|
emitIdentifier === `emit` ? `emit` : `emit: ${emitIdentifier}`
|
|
@@ -4461,7 +4692,9 @@ return ${returned}
|
|
|
4461
4692
|
for (const key in propsDestructuredBindings) {
|
|
4462
4693
|
const d = genDestructuredDefaultValue(key);
|
|
4463
4694
|
if (d)
|
|
4464
|
-
defaults.push(
|
|
4695
|
+
defaults.push(
|
|
4696
|
+
`${key}: ${d.valueString}${d.needSkipFactory ? `, __skip_${key}: true` : ``}`
|
|
4697
|
+
);
|
|
4465
4698
|
}
|
|
4466
4699
|
if (defaults.length) {
|
|
4467
4700
|
declCode = `${helper(
|
|
@@ -4482,15 +4715,20 @@ return ${returned}
|
|
|
4482
4715
|
} else if (emitsTypeDecl) {
|
|
4483
4716
|
runtimeOptions += genRuntimeEmits(typeDeclaredEmits);
|
|
4484
4717
|
}
|
|
4485
|
-
|
|
4718
|
+
let definedOptions = "";
|
|
4719
|
+
if (optionsRuntimeDecl) {
|
|
4720
|
+
definedOptions = scriptSetup.content.slice(optionsRuntimeDecl.start, optionsRuntimeDecl.end).trim();
|
|
4721
|
+
}
|
|
4722
|
+
const exposeCall = hasDefineExposeCall || options.inlineTemplate ? `` : ` __expose();
|
|
4486
4723
|
`;
|
|
4487
4724
|
if (isTS) {
|
|
4488
|
-
const def = defaultExport ? `
|
|
4489
|
-
...${
|
|
4725
|
+
const def = (defaultExport ? `
|
|
4726
|
+
...${normalScriptDefaultVar},` : ``) + (definedOptions ? `
|
|
4727
|
+
...${definedOptions},` : "");
|
|
4490
4728
|
s.prependLeft(
|
|
4491
4729
|
startOffset,
|
|
4492
4730
|
`
|
|
4493
|
-
|
|
4731
|
+
${genDefaultAs} /*#__PURE__*/${helper(
|
|
4494
4732
|
`defineComponent`
|
|
4495
4733
|
)}({${def}${runtimeOptions}
|
|
4496
4734
|
${hasAwait ? `async ` : ``}setup(${args}) {
|
|
@@ -4498,11 +4736,11 @@ ${exposeCall}`
|
|
|
4498
4736
|
);
|
|
4499
4737
|
s.appendRight(endOffset, `})`);
|
|
4500
4738
|
} else {
|
|
4501
|
-
if (defaultExport) {
|
|
4739
|
+
if (defaultExport || definedOptions) {
|
|
4502
4740
|
s.prependLeft(
|
|
4503
4741
|
startOffset,
|
|
4504
4742
|
`
|
|
4505
|
-
|
|
4743
|
+
${genDefaultAs} /*#__PURE__*/Object.assign(${defaultExport ? `${normalScriptDefaultVar}, ` : ""}${definedOptions ? `${definedOptions}, ` : ""}{${runtimeOptions}
|
|
4506
4744
|
${hasAwait ? `async ` : ``}setup(${args}) {
|
|
4507
4745
|
${exposeCall}`
|
|
4508
4746
|
);
|
|
@@ -4511,7 +4749,7 @@ ${exposeCall}`
|
|
|
4511
4749
|
s.prependLeft(
|
|
4512
4750
|
startOffset,
|
|
4513
4751
|
`
|
|
4514
|
-
|
|
4752
|
+
${genDefaultAs} {${runtimeOptions}
|
|
4515
4753
|
${hasAwait ? `async ` : ``}setup(${args}) {
|
|
4516
4754
|
${exposeCall}`
|
|
4517
4755
|
);
|
|
@@ -4543,26 +4781,33 @@ function registerBinding(bindings, node, type) {
|
|
|
4543
4781
|
bindings[node.name] = type;
|
|
4544
4782
|
}
|
|
4545
4783
|
function walkDeclaration(node, bindings, userImportAliases) {
|
|
4784
|
+
let isAllLiteral = false;
|
|
4546
4785
|
if (node.type === "VariableDeclaration") {
|
|
4547
4786
|
const isConst = node.kind === "const";
|
|
4548
|
-
|
|
4549
|
-
|
|
4787
|
+
isAllLiteral = isConst && node.declarations.every(
|
|
4788
|
+
(decl) => decl.id.type === "Identifier" && isStaticNode(decl.init)
|
|
4789
|
+
);
|
|
4790
|
+
for (const { id, init: _init } of node.declarations) {
|
|
4791
|
+
const init = _init && CompilerDOM.unwrapTSNode(_init);
|
|
4792
|
+
const isDefineCall = !!(isConst && CompilerDOM.isCallOf(
|
|
4550
4793
|
init,
|
|
4551
4794
|
(c) => c === DEFINE_PROPS || c === DEFINE_EMITS || c === WITH_DEFAULTS
|
|
4552
4795
|
));
|
|
4553
4796
|
if (id.type === "Identifier") {
|
|
4554
4797
|
let bindingType;
|
|
4555
4798
|
const userReactiveBinding = userImportAliases["reactive"];
|
|
4556
|
-
if (
|
|
4799
|
+
if (isAllLiteral || isConst && isStaticNode(init)) {
|
|
4800
|
+
bindingType = "literal-const";
|
|
4801
|
+
} else if (CompilerDOM.isCallOf(init, userReactiveBinding)) {
|
|
4557
4802
|
bindingType = isConst ? "setup-reactive-const" : "setup-let";
|
|
4558
4803
|
} else if (
|
|
4559
4804
|
// if a declaration is a const literal, we can mark it so that
|
|
4560
4805
|
// the generated render fn code doesn't need to unref() it
|
|
4561
4806
|
isDefineCall || isConst && canNeverBeRef(init, userReactiveBinding)
|
|
4562
4807
|
) {
|
|
4563
|
-
bindingType = isCallOf(init, DEFINE_PROPS) ? "setup-reactive-const" : "setup-const";
|
|
4808
|
+
bindingType = CompilerDOM.isCallOf(init, DEFINE_PROPS) ? "setup-reactive-const" : "setup-const";
|
|
4564
4809
|
} else if (isConst) {
|
|
4565
|
-
if (isCallOf(init, userImportAliases["ref"])) {
|
|
4810
|
+
if (CompilerDOM.isCallOf(init, userImportAliases["ref"])) {
|
|
4566
4811
|
bindingType = "setup-ref";
|
|
4567
4812
|
} else {
|
|
4568
4813
|
bindingType = "setup-maybe-ref";
|
|
@@ -4572,7 +4817,7 @@ function walkDeclaration(node, bindings, userImportAliases) {
|
|
|
4572
4817
|
}
|
|
4573
4818
|
registerBinding(bindings, id, bindingType);
|
|
4574
4819
|
} else {
|
|
4575
|
-
if (isCallOf(init, DEFINE_PROPS)) {
|
|
4820
|
+
if (CompilerDOM.isCallOf(init, DEFINE_PROPS)) {
|
|
4576
4821
|
continue;
|
|
4577
4822
|
}
|
|
4578
4823
|
if (id.type === "ObjectPattern") {
|
|
@@ -4582,9 +4827,15 @@ function walkDeclaration(node, bindings, userImportAliases) {
|
|
|
4582
4827
|
}
|
|
4583
4828
|
}
|
|
4584
4829
|
}
|
|
4585
|
-
} else if (node.type === "TSEnumDeclaration"
|
|
4830
|
+
} else if (node.type === "TSEnumDeclaration") {
|
|
4831
|
+
isAllLiteral = node.members.every(
|
|
4832
|
+
(member) => !member.initializer || isStaticNode(member.initializer)
|
|
4833
|
+
);
|
|
4834
|
+
bindings[node.id.name] = isAllLiteral ? "literal-const" : "setup-const";
|
|
4835
|
+
} else if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
|
|
4586
4836
|
bindings[node.id.name] = "setup-const";
|
|
4587
4837
|
}
|
|
4838
|
+
return isAllLiteral;
|
|
4588
4839
|
}
|
|
4589
4840
|
function walkObjectPattern(node, bindings, isConst, isDefineCall = false) {
|
|
4590
4841
|
for (const p of node.properties) {
|
|
@@ -4636,26 +4887,39 @@ function recordType(node, declaredTypes) {
|
|
|
4636
4887
|
);
|
|
4637
4888
|
} else if (node.type === "ExportNamedDeclaration" && node.declaration) {
|
|
4638
4889
|
recordType(node.declaration, declaredTypes);
|
|
4890
|
+
} else if (node.type === "TSEnumDeclaration") {
|
|
4891
|
+
declaredTypes[node.id.name] = inferEnumType(node);
|
|
4639
4892
|
}
|
|
4640
4893
|
}
|
|
4641
|
-
function extractRuntimeProps(node, props, declaredTypes
|
|
4894
|
+
function extractRuntimeProps(node, props, declaredTypes) {
|
|
4642
4895
|
const members = node.type === "TSTypeLiteral" ? node.members : node.body;
|
|
4643
4896
|
for (const m of members) {
|
|
4644
4897
|
if ((m.type === "TSPropertySignature" || m.type === "TSMethodSignature") && m.key.type === "Identifier") {
|
|
4645
4898
|
let type;
|
|
4899
|
+
let skipCheck = false;
|
|
4646
4900
|
if (m.type === "TSMethodSignature") {
|
|
4647
4901
|
type = ["Function"];
|
|
4648
4902
|
} else if (m.typeAnnotation) {
|
|
4649
4903
|
type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes);
|
|
4904
|
+
if (type.includes(UNKNOWN_TYPE)) {
|
|
4905
|
+
if (type.includes("Boolean") || type.includes("Function")) {
|
|
4906
|
+
type = type.filter((t) => t !== UNKNOWN_TYPE);
|
|
4907
|
+
skipCheck = true;
|
|
4908
|
+
} else {
|
|
4909
|
+
type = ["null"];
|
|
4910
|
+
}
|
|
4911
|
+
}
|
|
4650
4912
|
}
|
|
4651
4913
|
props[m.key.name] = {
|
|
4652
4914
|
key: m.key.name,
|
|
4653
4915
|
required: !m.optional,
|
|
4654
|
-
type: type || [`null`]
|
|
4916
|
+
type: type || [`null`],
|
|
4917
|
+
skipCheck
|
|
4655
4918
|
};
|
|
4656
4919
|
}
|
|
4657
4920
|
}
|
|
4658
4921
|
}
|
|
4922
|
+
const UNKNOWN_TYPE = "Unknown";
|
|
4659
4923
|
function inferRuntimeType(node, declaredTypes) {
|
|
4660
4924
|
switch (node.type) {
|
|
4661
4925
|
case "TSStringKeyword":
|
|
@@ -4666,8 +4930,19 @@ function inferRuntimeType(node, declaredTypes) {
|
|
|
4666
4930
|
return ["Boolean"];
|
|
4667
4931
|
case "TSObjectKeyword":
|
|
4668
4932
|
return ["Object"];
|
|
4669
|
-
case "
|
|
4670
|
-
return ["
|
|
4933
|
+
case "TSNullKeyword":
|
|
4934
|
+
return ["null"];
|
|
4935
|
+
case "TSTypeLiteral": {
|
|
4936
|
+
const types = /* @__PURE__ */ new Set();
|
|
4937
|
+
for (const m of node.members) {
|
|
4938
|
+
if (m.type === "TSCallSignatureDeclaration" || m.type === "TSConstructSignatureDeclaration") {
|
|
4939
|
+
types.add("Function");
|
|
4940
|
+
} else {
|
|
4941
|
+
types.add("Object");
|
|
4942
|
+
}
|
|
4943
|
+
}
|
|
4944
|
+
return types.size ? Array.from(types) : ["Object"];
|
|
4945
|
+
}
|
|
4671
4946
|
case "TSFunctionType":
|
|
4672
4947
|
return ["Function"];
|
|
4673
4948
|
case "TSArrayType":
|
|
@@ -4683,7 +4958,7 @@ function inferRuntimeType(node, declaredTypes) {
|
|
|
4683
4958
|
case "BigIntLiteral":
|
|
4684
4959
|
return ["Number"];
|
|
4685
4960
|
default:
|
|
4686
|
-
return [
|
|
4961
|
+
return [UNKNOWN_TYPE];
|
|
4687
4962
|
}
|
|
4688
4963
|
case "TSTypeReference":
|
|
4689
4964
|
if (node.typeName.type === "Identifier") {
|
|
@@ -4701,40 +4976,110 @@ function inferRuntimeType(node, declaredTypes) {
|
|
|
4701
4976
|
case "Date":
|
|
4702
4977
|
case "Promise":
|
|
4703
4978
|
return [node.typeName.name];
|
|
4704
|
-
case "Record":
|
|
4705
4979
|
case "Partial":
|
|
4980
|
+
case "Required":
|
|
4706
4981
|
case "Readonly":
|
|
4982
|
+
case "Record":
|
|
4707
4983
|
case "Pick":
|
|
4708
4984
|
case "Omit":
|
|
4709
|
-
case "Exclude":
|
|
4710
|
-
case "Extract":
|
|
4711
|
-
case "Required":
|
|
4712
4985
|
case "InstanceType":
|
|
4713
4986
|
return ["Object"];
|
|
4987
|
+
case "Uppercase":
|
|
4988
|
+
case "Lowercase":
|
|
4989
|
+
case "Capitalize":
|
|
4990
|
+
case "Uncapitalize":
|
|
4991
|
+
return ["String"];
|
|
4992
|
+
case "Parameters":
|
|
4993
|
+
case "ConstructorParameters":
|
|
4994
|
+
return ["Array"];
|
|
4995
|
+
case "NonNullable":
|
|
4996
|
+
if (node.typeParameters && node.typeParameters.params[0]) {
|
|
4997
|
+
return inferRuntimeType(
|
|
4998
|
+
node.typeParameters.params[0],
|
|
4999
|
+
declaredTypes
|
|
5000
|
+
).filter((t) => t !== "null");
|
|
5001
|
+
}
|
|
5002
|
+
break;
|
|
5003
|
+
case "Extract":
|
|
5004
|
+
if (node.typeParameters && node.typeParameters.params[1]) {
|
|
5005
|
+
return inferRuntimeType(
|
|
5006
|
+
node.typeParameters.params[1],
|
|
5007
|
+
declaredTypes
|
|
5008
|
+
);
|
|
5009
|
+
}
|
|
5010
|
+
break;
|
|
5011
|
+
case "Exclude":
|
|
5012
|
+
case "OmitThisParameter":
|
|
5013
|
+
if (node.typeParameters && node.typeParameters.params[0]) {
|
|
5014
|
+
return inferRuntimeType(
|
|
5015
|
+
node.typeParameters.params[0],
|
|
5016
|
+
declaredTypes
|
|
5017
|
+
);
|
|
5018
|
+
}
|
|
5019
|
+
break;
|
|
4714
5020
|
}
|
|
4715
5021
|
}
|
|
4716
|
-
return [
|
|
5022
|
+
return [UNKNOWN_TYPE];
|
|
4717
5023
|
case "TSParenthesizedType":
|
|
4718
5024
|
return inferRuntimeType(node.typeAnnotation, declaredTypes);
|
|
4719
5025
|
case "TSUnionType":
|
|
4720
|
-
return
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
];
|
|
4727
|
-
case "TSIntersectionType":
|
|
4728
|
-
return ["Object"];
|
|
5026
|
+
return flattenTypes(node.types, declaredTypes);
|
|
5027
|
+
case "TSIntersectionType": {
|
|
5028
|
+
return flattenTypes(node.types, declaredTypes).filter(
|
|
5029
|
+
(t) => t !== UNKNOWN_TYPE
|
|
5030
|
+
);
|
|
5031
|
+
}
|
|
4729
5032
|
case "TSSymbolKeyword":
|
|
4730
5033
|
return ["Symbol"];
|
|
4731
5034
|
default:
|
|
4732
|
-
return [
|
|
5035
|
+
return [UNKNOWN_TYPE];
|
|
4733
5036
|
}
|
|
4734
5037
|
}
|
|
5038
|
+
function flattenTypes(types, declaredTypes) {
|
|
5039
|
+
return [
|
|
5040
|
+
...new Set(
|
|
5041
|
+
[].concat(
|
|
5042
|
+
...types.map((t) => inferRuntimeType(t, declaredTypes))
|
|
5043
|
+
)
|
|
5044
|
+
)
|
|
5045
|
+
];
|
|
5046
|
+
}
|
|
4735
5047
|
function toRuntimeTypeString(types) {
|
|
4736
5048
|
return types.length > 1 ? `[${types.join(", ")}]` : types[0];
|
|
4737
5049
|
}
|
|
5050
|
+
function inferEnumType(node) {
|
|
5051
|
+
const types = /* @__PURE__ */ new Set();
|
|
5052
|
+
for (const m of node.members) {
|
|
5053
|
+
if (m.initializer) {
|
|
5054
|
+
switch (m.initializer.type) {
|
|
5055
|
+
case "StringLiteral":
|
|
5056
|
+
types.add("String");
|
|
5057
|
+
break;
|
|
5058
|
+
case "NumericLiteral":
|
|
5059
|
+
types.add("Number");
|
|
5060
|
+
break;
|
|
5061
|
+
}
|
|
5062
|
+
}
|
|
5063
|
+
}
|
|
5064
|
+
return types.size ? [...types] : ["Number"];
|
|
5065
|
+
}
|
|
5066
|
+
function inferValueType(node) {
|
|
5067
|
+
switch (node.type) {
|
|
5068
|
+
case "StringLiteral":
|
|
5069
|
+
return "String";
|
|
5070
|
+
case "NumericLiteral":
|
|
5071
|
+
return "Number";
|
|
5072
|
+
case "BooleanLiteral":
|
|
5073
|
+
return "Boolean";
|
|
5074
|
+
case "ObjectExpression":
|
|
5075
|
+
return "Object";
|
|
5076
|
+
case "ArrayExpression":
|
|
5077
|
+
return "Array";
|
|
5078
|
+
case "FunctionExpression":
|
|
5079
|
+
case "ArrowFunctionExpression":
|
|
5080
|
+
return "Function";
|
|
5081
|
+
}
|
|
5082
|
+
}
|
|
4738
5083
|
function extractRuntimeEmits(node, emits) {
|
|
4739
5084
|
if (node.type === "TSTypeLiteral" || node.type === "TSInterfaceBody") {
|
|
4740
5085
|
const members = node.type === "TSTypeLiteral" ? node.members : node.body;
|
|
@@ -4768,11 +5113,8 @@ function genRuntimeEmits(emits) {
|
|
|
4768
5113
|
return emits.size ? `
|
|
4769
5114
|
emits: [${Array.from(emits).map((p) => JSON.stringify(p)).join(", ")}],` : ``;
|
|
4770
5115
|
}
|
|
4771
|
-
function isCallOf(node, test) {
|
|
4772
|
-
return !!(node && test && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : test(node.callee.name)));
|
|
4773
|
-
}
|
|
4774
5116
|
function canNeverBeRef(node, userReactiveImport) {
|
|
4775
|
-
if (isCallOf(node, userReactiveImport)) {
|
|
5117
|
+
if (CompilerDOM.isCallOf(node, userReactiveImport)) {
|
|
4776
5118
|
return true;
|
|
4777
5119
|
}
|
|
4778
5120
|
switch (node.type) {
|
|
@@ -4792,12 +5134,40 @@ function canNeverBeRef(node, userReactiveImport) {
|
|
|
4792
5134
|
userReactiveImport
|
|
4793
5135
|
);
|
|
4794
5136
|
default:
|
|
4795
|
-
if (node
|
|
5137
|
+
if (isLiteralNode(node)) {
|
|
5138
|
+
return true;
|
|
5139
|
+
}
|
|
5140
|
+
return false;
|
|
5141
|
+
}
|
|
5142
|
+
}
|
|
5143
|
+
function isStaticNode(node) {
|
|
5144
|
+
switch (node.type) {
|
|
5145
|
+
case "UnaryExpression":
|
|
5146
|
+
return isStaticNode(node.argument);
|
|
5147
|
+
case "LogicalExpression":
|
|
5148
|
+
case "BinaryExpression":
|
|
5149
|
+
return isStaticNode(node.left) && isStaticNode(node.right);
|
|
5150
|
+
case "ConditionalExpression": {
|
|
5151
|
+
return isStaticNode(node.test) && isStaticNode(node.consequent) && isStaticNode(node.alternate);
|
|
5152
|
+
}
|
|
5153
|
+
case "SequenceExpression":
|
|
5154
|
+
case "TemplateLiteral":
|
|
5155
|
+
return node.expressions.every((expr) => isStaticNode(expr));
|
|
5156
|
+
case "ParenthesizedExpression":
|
|
5157
|
+
case "TSNonNullExpression":
|
|
5158
|
+
case "TSAsExpression":
|
|
5159
|
+
case "TSTypeAssertion":
|
|
5160
|
+
return isStaticNode(node.expression);
|
|
5161
|
+
default:
|
|
5162
|
+
if (isLiteralNode(node)) {
|
|
4796
5163
|
return true;
|
|
4797
5164
|
}
|
|
4798
5165
|
return false;
|
|
4799
5166
|
}
|
|
4800
5167
|
}
|
|
5168
|
+
function isLiteralNode(node) {
|
|
5169
|
+
return node.type.endsWith("Literal");
|
|
5170
|
+
}
|
|
4801
5171
|
function analyzeScriptBindings(ast) {
|
|
4802
5172
|
for (const node of ast) {
|
|
4803
5173
|
if (node.type === "ExportDefaultDeclaration" && node.declaration.type === "ObjectExpression") {
|
|
@@ -13910,6 +14280,7 @@ function preprocess(options, preprocessor) {
|
|
|
13910
14280
|
);
|
|
13911
14281
|
}
|
|
13912
14282
|
|
|
14283
|
+
const version = "3.3.0-alpha.6";
|
|
13913
14284
|
const walk = estreeWalker.walk;
|
|
13914
14285
|
|
|
13915
14286
|
exports.MagicString = MagicString;
|
|
@@ -13928,4 +14299,6 @@ exports.compileStyleAsync = compileStyleAsync;
|
|
|
13928
14299
|
exports.compileTemplate = compileTemplate;
|
|
13929
14300
|
exports.parse = parse$2;
|
|
13930
14301
|
exports.rewriteDefault = rewriteDefault;
|
|
14302
|
+
exports.rewriteDefaultAST = rewriteDefaultAST;
|
|
14303
|
+
exports.version = version;
|
|
13931
14304
|
exports.walk = walk;
|