@vue/compiler-sfc 3.4.0-alpha.4 → 3.4.0-beta.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.
@@ -50,13 +50,6 @@ function concatStrings(strs) {
50
50
  function isLiteralNode(node) {
51
51
  return node.type.endsWith("Literal");
52
52
  }
53
- function unwrapTSNode(node) {
54
- if (CompilerDOM.TS_NODE_TYPES.includes(node.type)) {
55
- return unwrapTSNode(node.expression);
56
- } else {
57
- return node;
58
- }
59
- }
60
53
  function isCallOf(node, test) {
61
54
  return !!(node && test && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : test(node.callee.name)));
62
55
  }
@@ -15489,10 +15482,118 @@ function getObjectOrArrayExpressionKeys(value) {
15489
15482
  return [];
15490
15483
  }
15491
15484
 
15485
+ var _a, _b;
15486
+ class ScriptCompileContext {
15487
+ constructor(descriptor, options) {
15488
+ this.descriptor = descriptor;
15489
+ this.options = options;
15490
+ this.isCE = false;
15491
+ this.source = this.descriptor.source;
15492
+ this.filename = this.descriptor.filename;
15493
+ this.s = new MagicString(this.source);
15494
+ this.startOffset = (_a = this.descriptor.scriptSetup) == null ? void 0 : _a.loc.start.offset;
15495
+ this.endOffset = (_b = this.descriptor.scriptSetup) == null ? void 0 : _b.loc.end.offset;
15496
+ this.userImports = /* @__PURE__ */ Object.create(null);
15497
+ // macros presence check
15498
+ this.hasDefinePropsCall = false;
15499
+ this.hasDefineEmitCall = false;
15500
+ this.hasDefineExposeCall = false;
15501
+ this.hasDefaultExportName = false;
15502
+ this.hasDefaultExportRender = false;
15503
+ this.hasDefineOptionsCall = false;
15504
+ this.hasDefineSlotsCall = false;
15505
+ this.hasDefineModelCall = false;
15506
+ this.propsDestructuredBindings = /* @__PURE__ */ Object.create(null);
15507
+ // defineModel
15508
+ this.modelDecls = /* @__PURE__ */ Object.create(null);
15509
+ // codegen
15510
+ this.bindingMetadata = {};
15511
+ this.helperImports = /* @__PURE__ */ new Set();
15512
+ const { script, scriptSetup } = descriptor;
15513
+ const scriptLang = script && script.lang;
15514
+ const scriptSetupLang = scriptSetup && scriptSetup.lang;
15515
+ this.isJS = scriptLang === "js" || scriptLang === "jsx" || scriptSetupLang === "js" || scriptSetupLang === "jsx";
15516
+ this.isTS = scriptLang === "ts" || scriptLang === "tsx" || scriptSetupLang === "ts" || scriptSetupLang === "tsx";
15517
+ const customElement = options.customElement;
15518
+ const filename = this.descriptor.filename;
15519
+ if (customElement) {
15520
+ this.isCE = typeof customElement === "boolean" ? customElement : customElement(filename);
15521
+ }
15522
+ const plugins = resolveParserPlugins(
15523
+ scriptLang || scriptSetupLang,
15524
+ options.babelParserPlugins
15525
+ );
15526
+ function parse(input, offset) {
15527
+ try {
15528
+ return parser$1.parse(input, {
15529
+ plugins,
15530
+ sourceType: "module"
15531
+ }).program;
15532
+ } catch (e) {
15533
+ e.message = `[vue/compiler-sfc] ${e.message}
15534
+
15535
+ ${descriptor.filename}
15536
+ ${shared.generateCodeFrame(
15537
+ descriptor.source,
15538
+ e.pos + offset,
15539
+ e.pos + offset + 1
15540
+ )}`;
15541
+ throw e;
15542
+ }
15543
+ }
15544
+ this.scriptAst = descriptor.script && parse(descriptor.script.content, descriptor.script.loc.start.offset);
15545
+ this.scriptSetupAst = descriptor.scriptSetup && parse(descriptor.scriptSetup.content, this.startOffset);
15546
+ }
15547
+ helper(key) {
15548
+ this.helperImports.add(key);
15549
+ return `_${key}`;
15550
+ }
15551
+ getString(node, scriptSetup = true) {
15552
+ const block = scriptSetup ? this.descriptor.scriptSetup : this.descriptor.script;
15553
+ return block.content.slice(node.start, node.end);
15554
+ }
15555
+ error(msg, node, scope) {
15556
+ const offset = scope ? scope.offset : this.startOffset;
15557
+ throw new Error(
15558
+ `[@vue/compiler-sfc] ${msg}
15559
+
15560
+ ${(scope || this.descriptor).filename}
15561
+ ${shared.generateCodeFrame(
15562
+ (scope || this.descriptor).source,
15563
+ node.start + offset,
15564
+ node.end + offset
15565
+ )}`
15566
+ );
15567
+ }
15568
+ }
15569
+ function resolveParserPlugins(lang, userPlugins, dts = false) {
15570
+ const plugins = [];
15571
+ if (!userPlugins || !userPlugins.some(
15572
+ (p) => p === "importAssertions" || p === "importAttributes" || shared.isArray(p) && p[0] === "importAttributes"
15573
+ )) {
15574
+ plugins.push("importAttributes");
15575
+ }
15576
+ if (lang === "jsx" || lang === "tsx") {
15577
+ plugins.push("jsx");
15578
+ } else if (userPlugins) {
15579
+ userPlugins = userPlugins.filter((p) => p !== "jsx");
15580
+ }
15581
+ if (lang === "ts" || lang === "tsx") {
15582
+ plugins.push(["typescript", { dts }], "explicitResourceManagement");
15583
+ if (!userPlugins || !userPlugins.includes("decorators")) {
15584
+ plugins.push("decorators-legacy");
15585
+ }
15586
+ }
15587
+ if (userPlugins) {
15588
+ plugins.push(...userPlugins);
15589
+ }
15590
+ return plugins;
15591
+ }
15592
+
15492
15593
  function rewriteDefault(input, as, parserPlugins) {
15493
15594
  const ast = parser$1.parse(input, {
15494
15595
  sourceType: "module",
15495
- plugins: parserPlugins
15596
+ plugins: resolveParserPlugins("js", parserPlugins)
15496
15597
  }).program.body;
15497
15598
  const s = new MagicString(input);
15498
15599
  rewriteDefaultAST(ast, s, as);
@@ -15626,103 +15727,6 @@ export default ${defaultVar}`;
15626
15727
  }
15627
15728
  }
15628
15729
 
15629
- var _a, _b;
15630
- class ScriptCompileContext {
15631
- constructor(descriptor, options) {
15632
- this.descriptor = descriptor;
15633
- this.options = options;
15634
- this.source = this.descriptor.source;
15635
- this.filename = this.descriptor.filename;
15636
- this.s = new MagicString(this.source);
15637
- this.startOffset = (_a = this.descriptor.scriptSetup) == null ? void 0 : _a.loc.start.offset;
15638
- this.endOffset = (_b = this.descriptor.scriptSetup) == null ? void 0 : _b.loc.end.offset;
15639
- this.userImports = /* @__PURE__ */ Object.create(null);
15640
- // macros presence check
15641
- this.hasDefinePropsCall = false;
15642
- this.hasDefineEmitCall = false;
15643
- this.hasDefineExposeCall = false;
15644
- this.hasDefaultExportName = false;
15645
- this.hasDefaultExportRender = false;
15646
- this.hasDefineOptionsCall = false;
15647
- this.hasDefineSlotsCall = false;
15648
- this.hasDefineModelCall = false;
15649
- this.propsDestructuredBindings = /* @__PURE__ */ Object.create(null);
15650
- // defineModel
15651
- this.modelDecls = /* @__PURE__ */ Object.create(null);
15652
- // codegen
15653
- this.bindingMetadata = {};
15654
- this.helperImports = /* @__PURE__ */ new Set();
15655
- const { script, scriptSetup } = descriptor;
15656
- const scriptLang = script && script.lang;
15657
- const scriptSetupLang = scriptSetup && scriptSetup.lang;
15658
- this.isJS = scriptLang === "js" || scriptLang === "jsx" || scriptSetupLang === "js" || scriptSetupLang === "jsx";
15659
- this.isTS = scriptLang === "ts" || scriptLang === "tsx" || scriptSetupLang === "ts" || scriptSetupLang === "tsx";
15660
- const plugins = resolveParserPlugins(
15661
- scriptLang || scriptSetupLang,
15662
- options.babelParserPlugins
15663
- );
15664
- function parse(input, offset) {
15665
- try {
15666
- return parser$1.parse(input, {
15667
- plugins,
15668
- sourceType: "module"
15669
- }).program;
15670
- } catch (e) {
15671
- e.message = `[vue/compiler-sfc] ${e.message}
15672
-
15673
- ${descriptor.filename}
15674
- ${shared.generateCodeFrame(
15675
- descriptor.source,
15676
- e.pos + offset,
15677
- e.pos + offset + 1
15678
- )}`;
15679
- throw e;
15680
- }
15681
- }
15682
- this.scriptAst = descriptor.script && parse(descriptor.script.content, descriptor.script.loc.start.offset);
15683
- this.scriptSetupAst = descriptor.scriptSetup && parse(descriptor.scriptSetup.content, this.startOffset);
15684
- }
15685
- helper(key) {
15686
- this.helperImports.add(key);
15687
- return `_${key}`;
15688
- }
15689
- getString(node, scriptSetup = true) {
15690
- const block = scriptSetup ? this.descriptor.scriptSetup : this.descriptor.script;
15691
- return block.content.slice(node.start, node.end);
15692
- }
15693
- error(msg, node, scope) {
15694
- const offset = scope ? scope.offset : this.startOffset;
15695
- throw new Error(
15696
- `[@vue/compiler-sfc] ${msg}
15697
-
15698
- ${(scope || this.descriptor).filename}
15699
- ${shared.generateCodeFrame(
15700
- (scope || this.descriptor).source,
15701
- node.start + offset,
15702
- node.end + offset
15703
- )}`
15704
- );
15705
- }
15706
- }
15707
- function resolveParserPlugins(lang, userPlugins, dts = false) {
15708
- const plugins = [];
15709
- if (lang === "jsx" || lang === "tsx") {
15710
- plugins.push("jsx");
15711
- } else if (userPlugins) {
15712
- userPlugins = userPlugins.filter((p) => p !== "jsx");
15713
- }
15714
- if (lang === "ts" || lang === "tsx") {
15715
- plugins.push(["typescript", { dts }]);
15716
- if (!userPlugins || !userPlugins.includes("decorators")) {
15717
- plugins.push("decorators-legacy");
15718
- }
15719
- }
15720
- if (userPlugins) {
15721
- plugins.push(...userPlugins);
15722
- }
15723
- return plugins;
15724
- }
15725
-
15726
15730
  var balancedMatch = balanced$1;
15727
15731
  function balanced$1(a, b, str) {
15728
15732
  if (a instanceof RegExp) a = maybeMatch(a, str);
@@ -18803,6 +18807,7 @@ function inferRuntimeType(ctx, node, scope = node._ownerScope || ctxToScope(ctx)
18803
18807
  scope
18804
18808
  );
18805
18809
  }
18810
+ break;
18806
18811
  case "TSMethodSignature":
18807
18812
  case "TSFunctionType":
18808
18813
  return ["Function"];
@@ -19067,22 +19072,11 @@ function processDefineModel(ctx, node, declId) {
19067
19072
  if (!isCallOf(node, DEFINE_MODEL)) {
19068
19073
  return false;
19069
19074
  }
19070
- if (!ctx.options.defineModel) {
19071
- warnOnce(
19072
- `defineModel() is an experimental feature and disabled by default.
19073
- To enable it, follow the RFC at https://github.com/vuejs/rfcs/discussions/503.`
19074
- );
19075
- return false;
19076
- }
19077
- warnOnce(
19078
- `This project is using defineModel(), which is an experimental feature. It may receive breaking changes or be removed in the future, so use at your own risk.
19079
- To stay updated, follow the RFC at https://github.com/vuejs/rfcs/discussions/503.`
19080
- );
19081
19075
  ctx.hasDefineModelCall = true;
19082
19076
  const type = node.typeParameters && node.typeParameters.params[0] || void 0;
19083
19077
  let modelName;
19084
19078
  let options;
19085
- const arg0 = node.arguments[0] && unwrapTSNode(node.arguments[0]);
19079
+ const arg0 = node.arguments[0] && CompilerDOM.unwrapTSNode(node.arguments[0]);
19086
19080
  if (arg0 && arg0.type === "StringLiteral") {
19087
19081
  modelName = arg0.value;
19088
19082
  options = node.arguments[1];
@@ -19344,6 +19338,15 @@ function genRuntimePropFromType(ctx, { key, required, type, skipCheck }, hasStat
19344
19338
  defaultString
19345
19339
  ])} }`;
19346
19340
  } else {
19341
+ if (ctx.isCE) {
19342
+ if (defaultString) {
19343
+ return `${finalKey}: ${`{ ${defaultString}, type: ${toRuntimeTypeString(
19344
+ type
19345
+ )} }`}`;
19346
+ } else {
19347
+ return `${finalKey}: {type: ${toRuntimeTypeString(type)}}`;
19348
+ }
19349
+ }
19347
19350
  return `${finalKey}: ${defaultString ? `{ ${defaultString} }` : `{}`}`;
19348
19351
  }
19349
19352
  }
@@ -19357,7 +19360,7 @@ function genDestructuredDefaultValue(ctx, key, inferredType) {
19357
19360
  const defaultVal = destructured && destructured.default;
19358
19361
  if (defaultVal) {
19359
19362
  const value = ctx.getString(defaultVal);
19360
- const unwrapped = unwrapTSNode(defaultVal);
19363
+ const unwrapped = CompilerDOM.unwrapTSNode(defaultVal);
19361
19364
  if (inferredType && inferredType.length && !inferredType.includes("null")) {
19362
19365
  const valueType = inferValueType(unwrapped);
19363
19366
  if (valueType && !inferredType.includes(valueType)) {
@@ -19496,7 +19499,7 @@ function transformDestructuredProps(ctx, vueImportAliases) {
19496
19499
  return;
19497
19500
  }
19498
19501
  for (const decl of stmt.declarations) {
19499
- const isDefineProps = isRoot && decl.init && isCallOf(unwrapTSNode(decl.init), "defineProps");
19502
+ const isDefineProps = isRoot && decl.init && isCallOf(CompilerDOM.unwrapTSNode(decl.init), "defineProps");
19500
19503
  for (const id of CompilerDOM.extractIdentifiers(decl.id)) {
19501
19504
  if (isDefineProps) {
19502
19505
  excludedIds.add(id);
@@ -19527,7 +19530,7 @@ function transformDestructuredProps(ctx, vueImportAliases) {
19527
19530
  }
19528
19531
  function checkUsage(node, method, alias = method) {
19529
19532
  if (isCallOf(node, alias)) {
19530
- const arg = unwrapTSNode(node.arguments[0]);
19533
+ const arg = CompilerDOM.unwrapTSNode(node.arguments[0]);
19531
19534
  if (arg.type === "Identifier" && currentScope[arg.name]) {
19532
19535
  ctx.error(
19533
19536
  `"${arg.name}" is a destructured prop and should not be passed directly to ${method}(). Pass a getter () => ${arg.name} instead.`,
@@ -19709,7 +19712,7 @@ function processDefineOptions(ctx, node) {
19709
19712
  if (!node.arguments[0])
19710
19713
  return true;
19711
19714
  ctx.hasDefineOptionsCall = true;
19712
- ctx.optionsRuntimeDecl = unwrapTSNode(node.arguments[0]);
19715
+ ctx.optionsRuntimeDecl = CompilerDOM.unwrapTSNode(node.arguments[0]);
19713
19716
  let propsOption = void 0;
19714
19717
  let emitsOption = void 0;
19715
19718
  let exposeOption = void 0;
@@ -20018,7 +20021,7 @@ const ${normalScriptDefaultVar} = ${defaultSpecifier.local.name}
20018
20021
  }
20019
20022
  for (const node of scriptSetupAst.body) {
20020
20023
  if (node.type === "ExpressionStatement") {
20021
- const expr = unwrapTSNode(node.expression);
20024
+ const expr = CompilerDOM.unwrapTSNode(node.expression);
20022
20025
  if (processDefineProps(ctx, expr) || processDefineEmits(ctx, expr) || processDefineOptions(ctx, expr) || processDefineSlots(ctx, expr)) {
20023
20026
  ctx.s.remove(node.start + startOffset, node.end + startOffset);
20024
20027
  } else if (processDefineExpose(ctx, expr)) {
@@ -20038,7 +20041,7 @@ const ${normalScriptDefaultVar} = ${defaultSpecifier.local.name}
20038
20041
  let lastNonRemoved;
20039
20042
  for (let i = 0; i < total; i++) {
20040
20043
  const decl = node.declarations[i];
20041
- const init = decl.init && unwrapTSNode(decl.init);
20044
+ const init = decl.init && CompilerDOM.unwrapTSNode(decl.init);
20042
20045
  if (init) {
20043
20046
  if (processDefineOptions(ctx, init)) {
20044
20047
  ctx.error(
@@ -20411,7 +20414,7 @@ function walkDeclaration(from, node, bindings, userImportAliases, hoistStatic) {
20411
20414
  (decl) => decl.id.type === "Identifier" && isStaticNode(decl.init)
20412
20415
  );
20413
20416
  for (const { id, init: _init } of node.declarations) {
20414
- const init = _init && unwrapTSNode(_init);
20417
+ const init = _init && CompilerDOM.unwrapTSNode(_init);
20415
20418
  const isDefineCall = !!(isConst && isCallOf(
20416
20419
  init,
20417
20420
  (c) => c === DEFINE_PROPS || c === DEFINE_EMITS || c === WITH_DEFAULTS
@@ -20531,7 +20534,7 @@ function canNeverBeRef(node, userReactiveImport) {
20531
20534
  }
20532
20535
  }
20533
20536
  function isStaticNode(node) {
20534
- node = unwrapTSNode(node);
20537
+ node = CompilerDOM.unwrapTSNode(node);
20535
20538
  switch (node.type) {
20536
20539
  case "UnaryExpression":
20537
20540
  return isStaticNode(node.argument);
@@ -20556,7 +20559,7 @@ function isStaticNode(node) {
20556
20559
  return false;
20557
20560
  }
20558
20561
 
20559
- const version = "3.4.0-alpha.4";
20562
+ const version = "3.4.0-beta.2";
20560
20563
  const parseCache = parseCache$1;
20561
20564
  const walk = estreeWalker.walk;
20562
20565
  const shouldTransformRef = () => false;
@@ -117,11 +117,6 @@ export interface SFCScriptCompileOptions {
117
117
  * @default true
118
118
  */
119
119
  hoistStatic?: boolean;
120
- /**
121
- * (**Experimental**) Enable macro `defineModel`
122
- * @default false
123
- */
124
- defineModel?: boolean;
125
120
  /**
126
121
  * (**Experimental**) Enable reactive destructure for `defineProps`
127
122
  * @default false
@@ -136,6 +131,10 @@ export interface SFCScriptCompileOptions {
136
131
  fileExists(file: string): boolean;
137
132
  readFile(file: string): string | undefined;
138
133
  };
134
+ /**
135
+ * Transform Vue SFCs into custom elements.
136
+ */
137
+ customElement?: boolean | ((filename: string) => boolean);
139
138
  }
140
139
  interface ImportBinding {
141
140
  isType: boolean;
@@ -349,6 +348,7 @@ export declare class ScriptCompileContext {
349
348
  options: Partial<SFCScriptCompileOptions>;
350
349
  isJS: boolean;
351
350
  isTS: boolean;
351
+ isCE: boolean;
352
352
  scriptAst: Program | null;
353
353
  scriptSetupAst: Program | null;
354
354
  source: string;
@@ -412,7 +412,7 @@ export type SimpleTypeResolveOptions = Partial<Pick<SFCScriptCompileOptions, 'gl
412
412
  * }
413
413
  * ```
414
414
  */
415
- export type SimpleTypeResolveContext = Pick<ScriptCompileContext, 'source' | 'filename' | 'error' | 'helper' | 'getString' | 'propsTypeDecl' | 'propsRuntimeDefaults' | 'propsDestructuredBindings' | 'emitsTypeDecl'> & Partial<Pick<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>> & {
415
+ export type SimpleTypeResolveContext = Pick<ScriptCompileContext, 'source' | 'filename' | 'error' | 'helper' | 'getString' | 'propsTypeDecl' | 'propsRuntimeDefaults' | 'propsDestructuredBindings' | 'emitsTypeDecl' | 'isCE'> & Partial<Pick<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>> & {
416
416
  ast: Statement[];
417
417
  options: SimpleTypeResolveOptions;
418
418
  };