@vue/compiler-sfc 2.7.0-alpha.8 → 2.7.0-alpha.9

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.
@@ -5,6 +5,144 @@ declare interface AssetURLOptions {
5
5
  [name: string]: string | string[];
6
6
  }
7
7
 
8
+ declare type ASTAttr = {
9
+ name: string;
10
+ value: any;
11
+ dynamic?: boolean;
12
+ start?: number;
13
+ end?: number;
14
+ };
15
+
16
+ declare type ASTDirective = {
17
+ name: string;
18
+ rawName: string;
19
+ value: string;
20
+ arg: string | null;
21
+ isDynamicArg: boolean;
22
+ modifiers: ASTModifiers | null;
23
+ start?: number;
24
+ end?: number;
25
+ };
26
+
27
+ declare type ASTElement = {
28
+ type: 1;
29
+ tag: string;
30
+ attrsList: Array<ASTAttr>;
31
+ attrsMap: {
32
+ [key: string]: any;
33
+ };
34
+ rawAttrsMap: {
35
+ [key: string]: ASTAttr;
36
+ };
37
+ parent: ASTElement | void;
38
+ children: Array<ASTNode>;
39
+ start?: number;
40
+ end?: number;
41
+ processed?: true;
42
+ static?: boolean;
43
+ staticRoot?: boolean;
44
+ staticInFor?: boolean;
45
+ staticProcessed?: boolean;
46
+ hasBindings?: boolean;
47
+ text?: string;
48
+ attrs?: Array<ASTAttr>;
49
+ dynamicAttrs?: Array<ASTAttr>;
50
+ props?: Array<ASTAttr>;
51
+ plain?: boolean;
52
+ pre?: true;
53
+ ns?: string;
54
+ component?: string;
55
+ inlineTemplate?: true;
56
+ transitionMode?: string | null;
57
+ slotName?: string | null;
58
+ slotTarget?: string | null;
59
+ slotTargetDynamic?: boolean;
60
+ slotScope?: string | null;
61
+ scopedSlots?: {
62
+ [name: string]: ASTElement;
63
+ };
64
+ ref?: string;
65
+ refInFor?: boolean;
66
+ if?: string;
67
+ ifProcessed?: boolean;
68
+ elseif?: string;
69
+ else?: true;
70
+ ifConditions?: ASTIfConditions;
71
+ for?: string;
72
+ forProcessed?: boolean;
73
+ key?: string;
74
+ alias?: string;
75
+ iterator1?: string;
76
+ iterator2?: string;
77
+ staticClass?: string;
78
+ classBinding?: string;
79
+ staticStyle?: string;
80
+ styleBinding?: string;
81
+ events?: ASTElementHandlers;
82
+ nativeEvents?: ASTElementHandlers;
83
+ transition?: string | true;
84
+ transitionOnAppear?: boolean;
85
+ model?: {
86
+ value: string;
87
+ callback: string;
88
+ expression: string;
89
+ };
90
+ directives?: Array<ASTDirective>;
91
+ forbidden?: true;
92
+ once?: true;
93
+ onceProcessed?: boolean;
94
+ wrapData?: (code: string) => string;
95
+ wrapListeners?: (code: string) => string;
96
+ ssrOptimizability?: number;
97
+ };
98
+
99
+ declare type ASTElementHandler = {
100
+ value: string;
101
+ params?: Array<any>;
102
+ modifiers: ASTModifiers | null;
103
+ dynamic?: boolean;
104
+ start?: number;
105
+ end?: number;
106
+ };
107
+
108
+ declare type ASTElementHandlers = {
109
+ [key: string]: ASTElementHandler | Array<ASTElementHandler>;
110
+ };
111
+
112
+ declare type ASTExpression = {
113
+ type: 2;
114
+ expression: string;
115
+ text: string;
116
+ tokens: Array<string | Object>;
117
+ static?: boolean;
118
+ ssrOptimizability?: number;
119
+ start?: number;
120
+ end?: number;
121
+ };
122
+
123
+ declare type ASTIfCondition = {
124
+ exp: string | null;
125
+ block: ASTElement;
126
+ };
127
+
128
+ declare type ASTIfConditions = Array<ASTIfCondition>;
129
+
130
+ declare type ASTModifiers = {
131
+ [key: string]: boolean;
132
+ };
133
+
134
+ declare type ASTNode = ASTElement | ASTText | ASTExpression;
135
+
136
+ declare type ASTText = {
137
+ type: 3;
138
+ text: string;
139
+ static?: boolean;
140
+ isComment?: boolean;
141
+ ssrOptimizability?: number;
142
+ start?: number;
143
+ end?: number;
144
+ };
145
+
8
146
  declare type BindingMetadata = {
9
147
  [key: string]: BindingTypes | undefined;
10
148
  } & {
@@ -53,6 +191,44 @@ declare const enum BindingTypes {
53
191
  OPTIONS = "options"
54
192
  }
55
193
 
194
+ declare type CompiledResult = {
195
+ ast: ASTElement | null;
196
+ render: string;
197
+ staticRenderFns: Array<string>;
198
+ stringRenderFns?: Array<string>;
199
+ errors?: Array<string | WarningMessage>;
200
+ tips?: Array<string | WarningMessage>;
201
+ };
202
+
203
+ declare type CompilerOptions = {
204
+ warn?: Function;
205
+ modules?: Array<ModuleOptions>;
206
+ directives?: {
207
+ [key: string]: Function;
208
+ };
209
+ staticKeys?: string;
210
+ isUnaryTag?: (tag: string) => boolean | undefined;
211
+ canBeLeftOpenTag?: (tag: string) => boolean | undefined;
212
+ isReservedTag?: (tag: string) => boolean | undefined;
213
+ preserveWhitespace?: boolean;
214
+ whitespace?: 'preserve' | 'condense';
215
+ optimize?: boolean;
216
+ mustUseProp?: (tag: string, type: string | null, name: string) => boolean;
217
+ isPreTag?: (attr: string) => boolean | null;
218
+ getTagNamespace?: (tag: string) => string | undefined;
219
+ expectHTML?: boolean;
220
+ isFromDOM?: boolean;
221
+ shouldDecodeTags?: boolean;
222
+ shouldDecodeNewlines?: boolean;
223
+ shouldDecodeNewlinesForHref?: boolean;
224
+ outputSourceRange?: boolean;
225
+ shouldKeepComment?: boolean;
226
+ delimiters?: [string, string];
227
+ comments?: boolean;
228
+ scopeId?: string;
229
+ bindings?: BindingMetadata;
230
+ };
231
+
56
232
  /**
57
233
  * Compile `<script setup>`
58
234
  * It requires the whole SFC descriptor because we need to handle and merge
@@ -76,6 +252,15 @@ declare interface ImportBinding {
76
252
  isUsedInTemplate: boolean;
77
253
  }
78
254
 
255
+ declare type ModuleOptions = {
256
+ preTransformNode?: (el: ASTElement) => ASTElement | null | void;
257
+ transformNode?: (el: ASTElement) => ASTElement | null | void;
258
+ postTransformNode?: (el: ASTElement) => void;
259
+ genData?: (el: ASTElement) => string;
260
+ transformCode?: (el: ASTElement, code: string) => string;
261
+ staticKeys?: Array<string>;
262
+ };
263
+
79
264
  export declare function parse(options: ParseOptions): SFCDescriptor;
80
265
 
81
266
  declare interface ParseOptions {
@@ -203,6 +388,7 @@ export declare interface TemplateCompileOptions {
203
388
  optimizeSSR?: boolean;
204
389
  prettify?: boolean;
205
390
  isTS?: boolean;
391
+ bindings?: BindingMetadata;
206
392
  }
207
393
 
208
394
  export declare interface TemplateCompileResult {
@@ -227,14 +413,7 @@ declare interface VueTemplateCompiler {
227
413
  ssrCompile(template: string, options: VueTemplateCompilerOptions): VueTemplateCompilerResults;
228
414
  }
229
415
 
230
- declare interface VueTemplateCompilerOptions {
231
- modules?: Object[];
232
- outputSourceRange?: boolean;
233
- whitespace?: 'preserve' | 'condense';
234
- directives?: {
235
- [key: string]: Function;
236
- };
237
- }
416
+ declare type VueTemplateCompilerOptions = CompilerOptions;
238
417
 
239
418
  declare interface VueTemplateCompilerParseOptions {
240
419
  pad?: 'line' | 'space' | boolean;
@@ -242,13 +421,7 @@ declare interface VueTemplateCompilerParseOptions {
242
421
  outputSourceRange?: boolean;
243
422
  }
244
423
 
245
- declare interface VueTemplateCompilerResults {
246
- ast: Object | undefined;
247
- render: string;
248
- staticRenderFns: string[];
249
- errors: (string | WarningMessage)[];
250
- tips: (string | WarningMessage)[];
251
- }
424
+ declare type VueTemplateCompilerResults = CompiledResult;
252
425
 
253
426
  declare type WarningMessage = {
254
427
  msg: string;
@@ -5789,7 +5789,7 @@ function resolveAsset(options, type, id, warnMissing) {
5789
5789
  // fallback to prototype chain
5790
5790
  const res = assets[id] || assets[camelizedId] || assets[PascalCaseId];
5791
5791
  if (process.env.NODE_ENV !== 'production' && warnMissing && !res) {
5792
- warn$3('Failed to resolve ' + type.slice(0, -1) + ': ' + id, options);
5792
+ warn$3('Failed to resolve ' + type.slice(0, -1) + ': ' + id);
5793
5793
  }
5794
5794
  return res;
5795
5795
  }
@@ -8772,7 +8772,7 @@ function compileScript(sfc, options = {}) {
8772
8772
  const any = isTS ? `: any` : ``;
8773
8773
  s.prependLeft(startOffset, `\nlet __temp${any}, __restore${any}\n`);
8774
8774
  }
8775
- const destructureElements = [`expose`];
8775
+ const destructureElements = hasDefineExposeCall ? [`expose`] : [];
8776
8776
  if (emitIdentifier) {
8777
8777
  destructureElements.push(emitIdentifier === `emit` ? `emit` : `emit: ${emitIdentifier}`);
8778
8778
  }
@@ -8789,7 +8789,9 @@ function compileScript(sfc, options = {}) {
8789
8789
  allBindings[key] = true;
8790
8790
  }
8791
8791
  }
8792
- const returned = `{ ${Object.keys(allBindings).join(', ')} }`;
8792
+ // __sfc marker indicates these bindings are compiled from <script setup>
8793
+ // and should not be proxied on `this`
8794
+ const returned = `{ ${`__sfc: true,`}${Object.keys(allBindings).join(', ')} }`;
8793
8795
  s.appendRight(endOffset, `\nreturn ${returned}\n}\n\n`);
8794
8796
  // 11. finalize default export
8795
8797
  let runtimeOptions = ``;
@@ -8816,9 +8818,6 @@ function compileScript(sfc, options = {}) {
8816
8818
  else if (emitsTypeDecl) {
8817
8819
  runtimeOptions += genRuntimeEmits(typeDeclaredEmits);
8818
8820
  }
8819
- // <script setup> components are closed by default. If the user did not
8820
- // explicitly call `defineExpose`, call expose() with no args.
8821
- const exposeCall = hasDefineExposeCall ? `` : ` expose();\n`;
8822
8821
  // wrap setup code with function.
8823
8822
  if (isTS) {
8824
8823
  // for TS, make sure the exported type is still valid type with
@@ -8827,7 +8826,7 @@ function compileScript(sfc, options = {}) {
8827
8826
  // user's TS setting should compile it down to proper targets
8828
8827
  // export default defineComponent({ ...__default__, ... })
8829
8828
  const def = defaultExport ? `\n ...${DEFAULT_VAR},` : ``;
8830
- s.prependLeft(startOffset, `\nexport default /*#__PURE__*/${helper(`defineComponent`)}({${def}${runtimeOptions}\n ${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
8829
+ s.prependLeft(startOffset, `\nexport default /*#__PURE__*/${helper(`defineComponent`)}({${def}${runtimeOptions}\n ${hasAwait ? `async ` : ``}setup(${args}) {\n`);
8831
8830
  s.appendRight(endOffset, `})`);
8832
8831
  }
8833
8832
  else {
@@ -8835,12 +8834,12 @@ function compileScript(sfc, options = {}) {
8835
8834
  // without TS, can't rely on rest spread, so we use Object.assign
8836
8835
  // export default Object.assign(__default__, { ... })
8837
8836
  s.prependLeft(startOffset, `\nexport default /*#__PURE__*/Object.assign(${DEFAULT_VAR}, {${runtimeOptions}\n ` +
8838
- `${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
8837
+ `${hasAwait ? `async ` : ``}setup(${args}) {\n`);
8839
8838
  s.appendRight(endOffset, `})`);
8840
8839
  }
8841
8840
  else {
8842
8841
  s.prependLeft(startOffset, `\nexport default {${runtimeOptions}\n ` +
8843
- `${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
8842
+ `${hasAwait ? `async ` : ``}setup(${args}) {\n`);
8844
8843
  s.appendRight(endOffset, `}`);
8845
8844
  }
8846
8845
  }
@@ -11741,8 +11740,19 @@ function genElement(el, state) {
11741
11740
  if (!el.plain || (el.pre && state.maybeComponent(el))) {
11742
11741
  data = genData(el, state);
11743
11742
  }
11743
+ let tag;
11744
+ // check if this is a component in <script setup>
11745
+ const bindings = state.options.bindings;
11746
+ if (bindings && bindings.__isScriptSetup !== false) {
11747
+ tag =
11748
+ checkBindingType(bindings, el.tag) ||
11749
+ checkBindingType(bindings, camelize(el.tag)) ||
11750
+ checkBindingType(bindings, capitalize(camelize(el.tag)));
11751
+ }
11752
+ if (!tag)
11753
+ tag = `'${el.tag}'`;
11744
11754
  const children = el.inlineTemplate ? null : genChildren(el, state, true);
11745
- code = `_c('${el.tag}'${data ? `,${data}` : '' // data
11755
+ code = `_c(${tag}${data ? `,${data}` : '' // data
11746
11756
  }${children ? `,${children}` : '' // children
11747
11757
  })`;
11748
11758
  }
@@ -11753,6 +11763,12 @@ function genElement(el, state) {
11753
11763
  return code;
11754
11764
  }
11755
11765
  }
11766
+ function checkBindingType(bindings, key) {
11767
+ const type = bindings[key];
11768
+ if (type && type.startsWith('setup')) {
11769
+ return key;
11770
+ }
11771
+ }
11756
11772
  // hoist static sub-trees out
11757
11773
  function genStatic(el, state) {
11758
11774
  el.staticProcessed = true;
@@ -12907,7 +12923,7 @@ const doNotPrefix = makeMap('Infinity,undefined,NaN,isFinite,isNaN,' +
12907
12923
  * The input is expected to be the render function code directly returned from
12908
12924
  * `compile()` calls, e.g. `with(this){return ...}`
12909
12925
  */
12910
- function stripWith(source, fnName = '', isFunctional = false, isTS = false, babelOptions = {}) {
12926
+ function prefixIdentifiers(source, fnName = '', isFunctional = false, isTS = false, babelOptions = {}, bindings) {
12911
12927
  source = `function ${fnName}(${isFunctional ? `_c,_vm` : ``}){${source}\n}`;
12912
12928
  const s = new MagicString(source);
12913
12929
  const plugins = [
@@ -12915,21 +12931,36 @@ function stripWith(source, fnName = '', isFunctional = false, isTS = false, babe
12915
12931
  ...((babelOptions === null || babelOptions === void 0 ? void 0 : babelOptions.plugins) || [])
12916
12932
  ];
12917
12933
  const ast = parser$1.parseExpression(source, Object.assign(Object.assign({}, babelOptions), { plugins }));
12934
+ const isScriptSetup = bindings && bindings.__isScriptSetup !== false;
12918
12935
  walkIdentifiers(ast, ident => {
12919
- if (doNotPrefix(ident.name)) {
12936
+ const { name } = ident;
12937
+ if (doNotPrefix(name)) {
12938
+ return;
12939
+ }
12940
+ if (!isScriptSetup) {
12941
+ s.prependRight(ident.start, '_vm.');
12920
12942
  return;
12921
12943
  }
12922
- s.prependRight(ident.start, '_vm.');
12944
+ s.overwrite(ident.start, ident.end, rewriteIdentifier(name, bindings));
12923
12945
  }, node => {
12924
12946
  if (node.type === 'WithStatement') {
12925
12947
  s.remove(node.start, node.body.start + 1);
12926
12948
  s.remove(node.end - 1, node.end);
12927
12949
  if (!isFunctional) {
12928
- s.prependRight(node.start, `var _vm=this;var _c=_vm._self._c;`);
12950
+ s.prependRight(node.start, `var _vm=this,_c=_vm._self._c${isScriptSetup ? `,_setup=_vm._setupProxy;` : `;`}`);
12929
12951
  }
12930
12952
  }
12931
12953
  });
12932
12954
  return s.toString();
12955
+ }
12956
+ function rewriteIdentifier(name, bindings) {
12957
+ const type = bindings[name];
12958
+ if (type && type.startsWith('setup')) {
12959
+ return `_setup.${name}`;
12960
+ }
12961
+ else {
12962
+ return `_vm.${name}`;
12963
+ }
12933
12964
  }
12934
12965
 
12935
12966
  function compileTemplate(options) {
@@ -12977,7 +13008,7 @@ function preprocess$1(options, preprocessor) {
12977
13008
  return res;
12978
13009
  }
12979
13010
  function actuallyCompile(options) {
12980
- const { source, compiler = _compiler, compilerOptions = {}, transpileOptions = {}, transformAssetUrls, transformAssetUrlsOptions, isProduction = process.env.NODE_ENV === 'production', isFunctional = false, optimizeSSR = false, prettify = true, isTS = false } = options;
13011
+ const { source, compiler = _compiler, compilerOptions = {}, transpileOptions = {}, transformAssetUrls, transformAssetUrlsOptions, isProduction = process.env.NODE_ENV === 'production', isFunctional = false, optimizeSSR = false, prettify = true, isTS = false, bindings } = options;
12981
13012
  const compile = optimizeSSR && compiler.ssrCompile ? compiler.ssrCompile : compiler.compile;
12982
13013
  let finalCompilerOptions = compilerOptions;
12983
13014
  if (transformAssetUrls) {
@@ -12992,6 +13023,7 @@ function actuallyCompile(options) {
12992
13023
  filename: options.filename
12993
13024
  });
12994
13025
  }
13026
+ finalCompilerOptions.bindings = bindings;
12995
13027
  const { ast, render, staticRenderFns, tips, errors } = compile(source, finalCompilerOptions);
12996
13028
  if (errors && errors.length) {
12997
13029
  return {
@@ -13005,8 +13037,8 @@ function actuallyCompile(options) {
13005
13037
  else {
13006
13038
  // transpile code with vue-template-es2015-compiler, which is a forked
13007
13039
  // version of Buble that applies ES2015 transforms + stripping `with` usage
13008
- let code = `var __render__ = ${stripWith(render, `render`, isFunctional, isTS, transpileOptions)}\n` +
13009
- `var __staticRenderFns__ = [${staticRenderFns.map(code => stripWith(code, ``, isFunctional, isTS, transpileOptions))}]` +
13040
+ let code = `var __render__ = ${prefixIdentifiers(render, `render`, isFunctional, isTS, transpileOptions, bindings)}\n` +
13041
+ `var __staticRenderFns__ = [${staticRenderFns.map(code => prefixIdentifiers(code, ``, isFunctional, isTS, transpileOptions, bindings))}]` +
13010
13042
  `\n`;
13011
13043
  // #23 we use __render__ to avoid `render` not being prefixed by the
13012
13044
  // transpiler when stripping with, but revert it back to `render` to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vue/compiler-sfc",
3
- "version": "2.7.0-alpha.8",
3
+ "version": "2.7.0-alpha.9",
4
4
  "description": "compiler-sfc for Vue 2",
5
5
  "main": "dist/compiler-sfc.js",
6
6
  "types": "dist/compiler-sfc.d.ts",