@vue/compiler-sfc 3.2.24 → 3.2.28

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.
@@ -14,7 +14,7 @@ var url = require('url');
14
14
  var CompilerSSR = require('@vue/compiler-ssr');
15
15
  var fs = require('fs');
16
16
  var util$2 = require('util');
17
- var refTransform = require('@vue/ref-transform');
17
+ var reactivityTransform = require('@vue/reactivity-transform');
18
18
  var _postcss = require('postcss');
19
19
 
20
20
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e['default'] : e; }
@@ -109,7 +109,7 @@ function sum (o) {
109
109
  var hashSum = sum;
110
110
 
111
111
  const CSS_VARS_HELPER = `useCssVars`;
112
- const cssVarRE = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g;
112
+ const cssVarRE = /\bv-bind\s*\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^;]*))\s*\)/g;
113
113
  function genCssVarsFromList(vars, id, isProd) {
114
114
  return `{\n ${vars
115
115
  .map(key => `"${genVarName(id, key, isProd)}": (${key})`)
@@ -3276,6 +3276,8 @@ const DEFINE_PROPS = 'defineProps';
3276
3276
  const DEFINE_EMITS = 'defineEmits';
3277
3277
  const DEFINE_EXPOSE = 'defineExpose';
3278
3278
  const WITH_DEFAULTS = 'withDefaults';
3279
+ // constants
3280
+ const DEFAULT_VAR = `__default__`;
3279
3281
  const isBuiltInDir = shared.makeMap(`once,memo,if,else,else-if,slot,text,html,on,bind,model,show,cloak,is`);
3280
3282
  /**
3281
3283
  * Compile `<script setup>`
@@ -3285,8 +3287,11 @@ const isBuiltInDir = shared.makeMap(`once,memo,if,else,else-if,slot,text,html,on
3285
3287
  function compileScript(sfc, options) {
3286
3288
  let { script, scriptSetup, source, filename } = sfc;
3287
3289
  // feature flags
3288
- const enableRefTransform = !!options.refSugar || !!options.refTransform;
3289
- const enablePropsTransform = !!options.propsDestructureTransform;
3290
+ // TODO remove support for deprecated options when out of experimental
3291
+ const enableReactivityTransform = !!options.reactivityTransform ||
3292
+ !!options.refSugar ||
3293
+ !!options.refTransform;
3294
+ const enablePropsTransform = !!options.reactivityTransform || !!options.propsDestructureTransform;
3290
3295
  const isProd = !!options.isProd;
3291
3296
  const genSourceMap = options.sourceMap !== false;
3292
3297
  let refBindings;
@@ -3303,6 +3308,7 @@ function compileScript(sfc, options) {
3303
3308
  scriptLang === 'tsx' ||
3304
3309
  scriptSetupLang === 'ts' ||
3305
3310
  scriptSetupLang === 'tsx';
3311
+ // resolve parser plugins
3306
3312
  const plugins = [];
3307
3313
  if (!isTS || scriptLang === 'tsx' || scriptSetupLang === 'tsx') {
3308
3314
  plugins.push('jsx');
@@ -3327,11 +3333,11 @@ function compileScript(sfc, options) {
3327
3333
  sourceType: 'module'
3328
3334
  }).program;
3329
3335
  const bindings = analyzeScriptBindings(scriptAst.body);
3330
- if (enableRefTransform && refTransform.shouldTransform(content)) {
3336
+ if (enableReactivityTransform && reactivityTransform.shouldTransform(content)) {
3331
3337
  const s = new MagicString__default(source);
3332
3338
  const startOffset = script.loc.start.offset;
3333
3339
  const endOffset = script.loc.end.offset;
3334
- const { importedHelpers } = refTransform.transformAST(scriptAst, s, startOffset);
3340
+ const { importedHelpers } = reactivityTransform.transformAST(scriptAst, s, startOffset);
3335
3341
  if (importedHelpers.length) {
3336
3342
  s.prepend(`import { ${importedHelpers
3337
3343
  .map(h => `${h} as _${h}`)
@@ -3349,9 +3355,9 @@ function compileScript(sfc, options) {
3349
3355
  }
3350
3356
  }
3351
3357
  if (cssVars.length) {
3352
- content = rewriteDefault(content, `__default__`, plugins);
3358
+ content = rewriteDefault(content, DEFAULT_VAR, plugins);
3353
3359
  content += genNormalScriptCssVarsCode(cssVars, bindings, scopeId, isProd);
3354
- content += `\nexport default __default__`;
3360
+ content += `\nexport default ${DEFAULT_VAR}`;
3355
3361
  }
3356
3362
  return Object.assign(Object.assign({}, script), { content,
3357
3363
  map,
@@ -3373,7 +3379,6 @@ function compileScript(sfc, options) {
3373
3379
  }
3374
3380
  // metadata that needs to be returned
3375
3381
  const bindingMetadata = {};
3376
- const defaultTempVar = `__default__`;
3377
3382
  const helperImports = new Set();
3378
3383
  const userImports = Object.create(null);
3379
3384
  const userImportAlias = Object.create(null);
@@ -3738,7 +3743,6 @@ function compileScript(sfc, options) {
3738
3743
  // 1. process normal <script> first if it exists
3739
3744
  let scriptAst;
3740
3745
  if (script) {
3741
- // import dedupe between <script> and <script setup>
3742
3746
  scriptAst = parse(script.content, {
3743
3747
  plugins,
3744
3748
  sourceType: 'module'
@@ -3756,9 +3760,10 @@ function compileScript(sfc, options) {
3756
3760
  else if (node.type === 'ExportDefaultDeclaration') {
3757
3761
  // export default
3758
3762
  defaultExport = node;
3763
+ // export default { ... } --> const __default__ = { ... }
3759
3764
  const start = node.start + scriptStartOffset;
3760
3765
  const end = node.declaration.start + scriptStartOffset;
3761
- s.overwrite(start, end, `const ${defaultTempVar} = `);
3766
+ s.overwrite(start, end, `const ${DEFAULT_VAR} = `);
3762
3767
  }
3763
3768
  else if (node.type === 'ExportNamedDeclaration') {
3764
3769
  const defaultSpecifier = node.specifiers.find(s => s.exported.type === 'Identifier' && s.exported.name === 'default');
@@ -3775,12 +3780,12 @@ function compileScript(sfc, options) {
3775
3780
  // export { x as default } from './x'
3776
3781
  // rewrite to `import { x as __default__ } from './x'` and
3777
3782
  // add to top
3778
- s.prepend(`import { ${defaultSpecifier.local.name} as ${defaultTempVar} } from '${node.source.value}'\n`);
3783
+ s.prepend(`import { ${defaultSpecifier.local.name} as ${DEFAULT_VAR} } from '${node.source.value}'\n`);
3779
3784
  }
3780
3785
  else {
3781
3786
  // export { x as default }
3782
3787
  // rewrite to `const __default__ = x` and move to end
3783
- s.append(`\nconst ${defaultTempVar} = ${defaultSpecifier.local.name}\n`);
3788
+ s.appendLeft(scriptEndOffset, `\nconst ${DEFAULT_VAR} = ${defaultSpecifier.local.name}\n`);
3784
3789
  }
3785
3790
  }
3786
3791
  if (node.declaration) {
@@ -3795,14 +3800,20 @@ function compileScript(sfc, options) {
3795
3800
  walkDeclaration(node, scriptBindings, userImportAlias);
3796
3801
  }
3797
3802
  }
3798
- // apply ref transform
3799
- if (enableRefTransform && refTransform.shouldTransform(script.content)) {
3800
- const { rootRefs: rootVars, importedHelpers } = refTransform.transformAST(scriptAst, s, scriptStartOffset);
3801
- refBindings = rootVars;
3803
+ // apply reactivity transform
3804
+ if (enableReactivityTransform && reactivityTransform.shouldTransform(script.content)) {
3805
+ const { rootRefs, importedHelpers } = reactivityTransform.transformAST(scriptAst, s, scriptStartOffset);
3806
+ refBindings = rootRefs;
3802
3807
  for (const h of importedHelpers) {
3803
3808
  helperImports.add(h);
3804
3809
  }
3805
3810
  }
3811
+ // <script> after <script setup>
3812
+ // we need to move the block up so that `const __default__` is
3813
+ // declared before being used in the actual component definition
3814
+ if (scriptStartOffset > startOffset) {
3815
+ s.move(scriptStartOffset, scriptEndOffset, 0);
3816
+ }
3806
3817
  }
3807
3818
  // 2. parse <script setup> and walk over top level statements
3808
3819
  const scriptSetupAst = parse(scriptSetup.content, {
@@ -3977,10 +3988,12 @@ function compileScript(sfc, options) {
3977
3988
  }
3978
3989
  }
3979
3990
  }
3980
- // 3. Apply ref sugar transform
3981
- if ((enableRefTransform && refTransform.shouldTransform(scriptSetup.content)) ||
3991
+ // 3. Apply reactivity transform
3992
+ if ((enableReactivityTransform &&
3993
+ // normal <script> had ref bindings that maybe used in <script setup>
3994
+ (refBindings || reactivityTransform.shouldTransform(scriptSetup.content))) ||
3982
3995
  propsDestructureDecl) {
3983
- const { rootRefs, importedHelpers } = refTransform.transformAST(scriptSetupAst, s, startOffset, refBindings, propsDestructuredBindings, !enableRefTransform);
3996
+ const { rootRefs, importedHelpers } = reactivityTransform.transformAST(scriptSetupAst, s, startOffset, refBindings, propsDestructuredBindings);
3984
3997
  refBindings = refBindings ? [...refBindings, ...rootRefs] : rootRefs;
3985
3998
  for (const h of importedHelpers) {
3986
3999
  helperImports.add(h);
@@ -4205,29 +4218,24 @@ function compileScript(sfc, options) {
4205
4218
  // <script setup> components are closed by default. If the user did not
4206
4219
  // explicitly call `defineExpose`, call expose() with no args.
4207
4220
  const exposeCall = hasDefineExposeCall || options.inlineTemplate ? `` : ` expose();\n`;
4221
+ // wrap setup code with function.
4208
4222
  if (isTS) {
4209
4223
  // for TS, make sure the exported type is still valid type with
4210
4224
  // correct props information
4211
4225
  // we have to use object spread for types to be merged properly
4212
4226
  // user's TS setting should compile it down to proper targets
4213
- const def = defaultExport ? `\n ...${defaultTempVar},` : ``;
4214
- // wrap setup code with function.
4215
- // export the content of <script setup> as a named export, `setup`.
4216
- // this allows `import { setup } from '*.vue'` for testing purposes.
4217
- if (defaultExport) {
4218
- s.prependLeft(startOffset, `\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`);
4219
- s.append(`\nexport default /*#__PURE__*/${helper(`defineComponent`)}({${def}${runtimeOptions}\n setup})`);
4220
- }
4221
- else {
4222
- s.prependLeft(startOffset, `\nexport default /*#__PURE__*/${helper(`defineComponent`)}({${def}${runtimeOptions}\n ${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
4223
- s.appendRight(endOffset, `})`);
4224
- }
4227
+ // export default defineComponent({ ...__default__, ... })
4228
+ const def = defaultExport ? `\n ...${DEFAULT_VAR},` : ``;
4229
+ s.prependLeft(startOffset, `\nexport default /*#__PURE__*/${helper(`defineComponent`)}({${def}${runtimeOptions}\n ${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
4230
+ s.appendRight(endOffset, `})`);
4225
4231
  }
4226
4232
  else {
4227
4233
  if (defaultExport) {
4228
- // can't rely on spread operator in non ts mode
4229
- s.prependLeft(startOffset, `\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`);
4230
- s.append(`\nexport default /*#__PURE__*/ Object.assign(${defaultTempVar}, {${runtimeOptions}\n setup\n})\n`);
4234
+ // without TS, can't rely on rest spread, so we use Object.assign
4235
+ // export default Object.assign(__default__, { ... })
4236
+ s.prependLeft(startOffset, `\nexport default /*#__PURE__*/Object.assign(${DEFAULT_VAR}, {${runtimeOptions}\n ` +
4237
+ `${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}`);
4238
+ s.appendRight(endOffset, `})`);
4231
4239
  }
4232
4240
  else {
4233
4241
  s.prependLeft(startOffset, `\nexport default {${runtimeOptions}\n ` +
@@ -8414,7 +8422,11 @@ var Parser = /*#__PURE__*/function () {
8414
8422
  }
8415
8423
 
8416
8424
  var hasClass = indexesOf(word, '.').filter(function (i) {
8417
- return word[i - 1] !== '\\';
8425
+ // Allow escaped dot within class name
8426
+ var escapedDot = word[i - 1] === '\\'; // Allow decimal numbers percent in @keyframes
8427
+
8428
+ var isKeyframesPercent = /^\d+\.\d+%$/.test(word);
8429
+ return !escapedDot && !isKeyframesPercent;
8418
8430
  });
8419
8431
  var hasId = indexesOf(word, '#').filter(function (i) {
8420
8432
  return word[i - 1] !== '\\';
@@ -14987,6 +14999,7 @@ var parse$2 = function(input) {
14987
14999
  after = token;
14988
15000
  } else if (prev && prev.type === "div") {
14989
15001
  prev.after = token;
15002
+ prev.sourceEndIndex += token.length;
14990
15003
  } else if (
14991
15004
  code === comma ||
14992
15005
  code === colon ||
@@ -15000,6 +15013,7 @@ var parse$2 = function(input) {
15000
15013
  tokens.push({
15001
15014
  type: "space",
15002
15015
  sourceIndex: pos,
15016
+ sourceEndIndex: next,
15003
15017
  value: token
15004
15018
  });
15005
15019
  }
@@ -15031,22 +15045,25 @@ var parse$2 = function(input) {
15031
15045
  }
15032
15046
  } while (escape);
15033
15047
  token.value = value.slice(pos + 1, next);
15034
-
15048
+ token.sourceEndIndex = token.unclosed ? next : next + 1;
15035
15049
  tokens.push(token);
15036
15050
  pos = next + 1;
15037
15051
  code = value.charCodeAt(pos);
15038
15052
 
15039
15053
  // Comments
15040
15054
  } else if (code === slash && value.charCodeAt(pos + 1) === star) {
15055
+ next = value.indexOf("*/", pos);
15056
+
15041
15057
  token = {
15042
15058
  type: "comment",
15043
- sourceIndex: pos
15059
+ sourceIndex: pos,
15060
+ sourceEndIndex: next + 2
15044
15061
  };
15045
15062
 
15046
- next = value.indexOf("*/", pos);
15047
15063
  if (next === -1) {
15048
15064
  token.unclosed = true;
15049
15065
  next = value.length;
15066
+ token.sourceEndIndex = next;
15050
15067
  }
15051
15068
 
15052
15069
  token.value = value.slice(pos + 2, next);
@@ -15066,6 +15083,7 @@ var parse$2 = function(input) {
15066
15083
  tokens.push({
15067
15084
  type: "word",
15068
15085
  sourceIndex: pos - before.length,
15086
+ sourceEndIndex: pos + token.length,
15069
15087
  value: token
15070
15088
  });
15071
15089
  pos += 1;
@@ -15078,6 +15096,7 @@ var parse$2 = function(input) {
15078
15096
  tokens.push({
15079
15097
  type: "div",
15080
15098
  sourceIndex: pos - before.length,
15099
+ sourceEndIndex: pos + token.length,
15081
15100
  value: token,
15082
15101
  before: before,
15083
15102
  after: ""
@@ -15133,6 +15152,7 @@ var parse$2 = function(input) {
15133
15152
  {
15134
15153
  type: "word",
15135
15154
  sourceIndex: pos,
15155
+ sourceEndIndex: whitespacePos + 1,
15136
15156
  value: value.slice(pos, whitespacePos + 1)
15137
15157
  }
15138
15158
  ];
@@ -15144,21 +15164,25 @@ var parse$2 = function(input) {
15144
15164
  token.nodes.push({
15145
15165
  type: "space",
15146
15166
  sourceIndex: whitespacePos + 1,
15167
+ sourceEndIndex: next,
15147
15168
  value: value.slice(whitespacePos + 1, next)
15148
15169
  });
15149
15170
  } else {
15150
15171
  token.after = value.slice(whitespacePos + 1, next);
15172
+ token.sourceEndIndex = next;
15151
15173
  }
15152
15174
  } else {
15153
15175
  token.after = "";
15154
15176
  token.nodes = [];
15155
15177
  }
15156
15178
  pos = next + 1;
15179
+ token.sourceEndIndex = token.unclosed ? next : pos;
15157
15180
  code = value.charCodeAt(pos);
15158
15181
  tokens.push(token);
15159
15182
  } else {
15160
15183
  balanced += 1;
15161
15184
  token.after = "";
15185
+ token.sourceEndIndex = pos + 1;
15162
15186
  tokens.push(token);
15163
15187
  stack.push(token);
15164
15188
  tokens = token.nodes = [];
@@ -15172,8 +15196,10 @@ var parse$2 = function(input) {
15172
15196
  code = value.charCodeAt(pos);
15173
15197
 
15174
15198
  parent.after = after;
15199
+ parent.sourceEndIndex += after.length;
15175
15200
  after = "";
15176
15201
  balanced -= 1;
15202
+ stack[stack.length - 1].sourceEndIndex = pos;
15177
15203
  stack.pop();
15178
15204
  parent = stack[balanced];
15179
15205
  tokens = parent.nodes;
@@ -15219,12 +15245,14 @@ var parse$2 = function(input) {
15219
15245
  tokens.push({
15220
15246
  type: "unicode-range",
15221
15247
  sourceIndex: pos,
15248
+ sourceEndIndex: next,
15222
15249
  value: token
15223
15250
  });
15224
15251
  } else {
15225
15252
  tokens.push({
15226
15253
  type: "word",
15227
15254
  sourceIndex: pos,
15255
+ sourceEndIndex: next,
15228
15256
  value: token
15229
15257
  });
15230
15258
  }
@@ -15235,6 +15263,7 @@ var parse$2 = function(input) {
15235
15263
 
15236
15264
  for (pos = stack.length - 1; pos; pos -= 1) {
15237
15265
  stack[pos].unclosed = true;
15266
+ stack[pos].sourceEndIndex = value.length;
15238
15267
  }
15239
15268
 
15240
15269
  return stack[0].nodes;
@@ -17251,9 +17280,9 @@ exports.generateCodeFrame = compilerCore.generateCodeFrame;
17251
17280
  exports.isInDestructureAssignment = compilerCore.isInDestructureAssignment;
17252
17281
  exports.isStaticProperty = compilerCore.isStaticProperty;
17253
17282
  exports.walkIdentifiers = compilerCore.walkIdentifiers;
17254
- exports.shouldTransformRef = refTransform.shouldTransform;
17255
- exports.transformRef = refTransform.transform;
17256
- exports.transformRefAST = refTransform.transformAST;
17283
+ exports.shouldTransformRef = reactivityTransform.shouldTransform;
17284
+ exports.transformRef = reactivityTransform.transform;
17285
+ exports.transformRefAST = reactivityTransform.transformAST;
17257
17286
  exports.compileScript = compileScript;
17258
17287
  exports.compileStyle = compileStyle;
17259
17288
  exports.compileStyleAsync = compileStyleAsync;
@@ -15,13 +15,13 @@ import { ParserPlugin } from '@babel/parser';
15
15
  import { RawSourceMap } from 'source-map';
16
16
  import { Result } from 'postcss';
17
17
  import { RootNode } from '@vue/compiler-core';
18
- import { shouldTransform as shouldTransformRef } from '@vue/ref-transform';
18
+ import { shouldTransform as shouldTransformRef } from '@vue/reactivity-transform';
19
19
  import { SourceLocation } from '@vue/compiler-core';
20
- import { transform as transformRef } from '@vue/ref-transform';
21
- import { transformAST as transformRefAST } from '@vue/ref-transform';
20
+ import { transform as transformRef } from '@vue/reactivity-transform';
21
+ import { transformAST as transformRefAST } from '@vue/reactivity-transform';
22
22
  import { walkIdentifiers } from '@vue/compiler-core';
23
23
 
24
- declare interface AssetURLOptions {
24
+ export declare interface AssetURLOptions {
25
25
  /**
26
26
  * If base is provided, instead of transforming relative asset urls into
27
27
  * imports, they will be directly rewritten to absolute urls.
@@ -34,7 +34,7 @@ declare interface AssetURLOptions {
34
34
  tags?: AssetURLTagConfig;
35
35
  }
36
36
 
37
- declare interface AssetURLTagConfig {
37
+ export declare interface AssetURLTagConfig {
38
38
  [name: string]: string[];
39
39
  }
40
40
 
@@ -150,7 +150,7 @@ export declare interface SFCParseOptions {
150
150
  compiler?: TemplateCompiler;
151
151
  }
152
152
 
153
- declare interface SFCParseResult {
153
+ export declare interface SFCParseResult {
154
154
  descriptor: SFCDescriptor;
155
155
  errors: (CompilerError | SyntaxError)[];
156
156
  }
@@ -188,20 +188,27 @@ export declare interface SFCScriptCompileOptions {
188
188
  * https://babeljs.io/docs/en/babel-parser#plugins
189
189
  */
190
190
  babelParserPlugins?: ParserPlugin[];
191
+ /**
192
+ * (Experimental) Enable syntax transform for using refs without `.value` and
193
+ * using destructured props with reactivity
194
+ */
195
+ reactivityTransform?: boolean;
191
196
  /**
192
197
  * (Experimental) Enable syntax transform for using refs without `.value`
193
198
  * https://github.com/vuejs/rfcs/discussions/369
199
+ * @deprecated now part of `reactivityTransform`
194
200
  * @default false
195
201
  */
196
202
  refTransform?: boolean;
197
203
  /**
198
204
  * (Experimental) Enable syntax transform for destructuring from defineProps()
199
205
  * https://github.com/vuejs/rfcs/discussions/394
206
+ * @deprecated now part of `reactivityTransform`
200
207
  * @default false
201
208
  */
202
209
  propsDestructureTransform?: boolean;
203
210
  /**
204
- * @deprecated use `refTransform` instead.
211
+ * @deprecated use `reactivityTransform` instead.
205
212
  */
206
213
  refSugar?: boolean;
207
214
  /**