Skip to content

perf(compiler): faster and more accurate source maps - #16126

Open
johnsoncodehk wants to merge 42 commits into
vuejs:minorfrom
johnsoncodehk:cursor/muggle-string-0eaa
Open

johnsoncodehk wants to merge 42 commits into
vuejs:minorfrom
johnsoncodehk:cursor/muggle-string-0eaa

Conversation

@johnsoncodehk

@johnsoncodehk johnsoncodehk commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

@vue/compiler-sfc no longer edits scripts with magic-string and no longer re-exports MagicString (an intentional break, see Downstream); @vue/compiler-core, @vue/compiler-vapor, @vue/compiler-sfc and @vue/compat no longer depend on source-map-js; code output is unchanged, only source maps change.

Why

  • A more accurate model for source edits: every piece of generated code carries the source range it came from, and the map is built from those pieces, instead of being tracked separately from the edits.
  • Source maps are controllable behavior, and the map bugs that came from the old tracking are fixed, listed below.
  • Fewer dependencies: the install tree of @vue/compiler-sfc drops from 12 to 11 packages. source-map-js stays in that tree only because postcss depends on it.
  • Less CPU and memory when maps are on.

Fixed

`const __returned__`, inserted by `compileScript`
const __returned__ = { props }
^^^^^^^^^^^^^^^^^^

2 | const props = defineProps(['msg'])
  |               ^ base
PR: unmapped
`_ctx.bar` in a multi-line `:class`
    b: _ctx.bar
       ^^^^^^^^

2 |   <div :class="{
  |                                     ^ base (past the end of the line)
3 |     a: foo,
4 |     b: bar
  |        ^ PR
vapor `_ctx.selected` in `_createSelector`
  const _selector0 = _createSelector(() => _ctx.selected)
                                           ^^^^^^^^^^^^^

1 | <template>
  | ^ base
6 |       :class="selected === row.id ? 'a' : ''"
  |               ^ PR
`_createTextVNode(` of `a`
    _cache[0] || (_cache[0] = _createTextVNode("a", -1 /* CACHED */)),
                              ^^^^^^^^^^^^^^^^^

2 |   <div>a<span/>b {{ x }}</div>
  |                               ^ base
  |        ^ PR
`_toDisplayString(` with `inlineTemplate`
  return (_openBlock(), _createElementBlock("h1", null, _toDisplayString(__props.msg), 1 /* TEXT */))
                                                        ^^^^^^^^^^^^^^^^^

6 |   <h1>{{ msg }}</h1>
  |                     ^ base
  |       ^ PR
`_withDirectives(` of `v-model`
  return _withDirectives((_openBlock(), _createElementBlock("select", {
         ^^^^^^^^^^^^^^^^

2 |   <select v-model="model"></select>
  |   ^ PR
base: unmapped
`_toDisplayString(` with a custom `compiler` and `inlineTemplate`
    _createElementVNode("p", null, _toDisplayString(_ctx.total), 1 /* TEXT */),
                                   ^^^^^^^^^^^^^^^^^

4 | <template><p>{{ total }}</p>
  |                   ^ base
  |              ^ PR
the error of a custom `compiler`
Error parsing JavaScript expression: Unexpected token (1:4)

1 | <script setup>
  |          ^ base
2 | </script>
3 | <template><p v-if="a +"></p></template>
  |                    ^ PR
`_ctx.record('enter', el)` in a multi-line `@enter`
            _ctx.record('enter', el)
            ^^^^^^^^^^^^

32 |         @enter="
   |                                                        ^ base (past the end of the line)
  …
34 |             record('enter', el)
   |             ^ PR
`_openBlock(` of a `v-for` row
          return (_openBlock(), _createElementBlock("tr", {
                  ^^^^^^^^^^^

108 |       <tr
    |       ^ PR
109 |         v-for="row of rows"
    |                   ^ base
`_toDisplayString(` in a `v-for` row
            _createElementVNode("td", _hoisted_6, _toDisplayString(row.id), 1 /* TEXT */),
                                                  ^^^^^^^^^^^^^^^^^

113 |         <td class="col-md-1">{{ row.id }}</td>
    |                                               ^ base
    |                              ^ PR
_withCtx( of a <template #default> slot
      default: _withCtx(({ foo }) => [
               ^^^^^^^^^

13 |     <template #default="{ foo }">
   |                       ^ base
   |     ^ PR
vapor `_toDisplayString(` in a v-for row
    _setText(x15, _toDisplayString(_for_item0.value.id))
                  ^^^^^^^^^^^^^^^^^

111 |         :class="selected === row.id ? 'danger' : ''"
    |                                 ^ base
113 |         <td class="col-md-1">{{ row.id }}</td>
    |                              ^ PR
vapor `_toDisplayString(` of `v-text`
  _renderEffect(() => _setText(x0, _toDisplayString(_ctx.foo)))
                                   ^^^^^^^^^^^^^^^^^

1 | <div v-text="foo" />
  |      ^ PR
base: unmapped
vapor `_toDisplayString(` of `v-text` on a component
  const n0 = _createAssetComponent("Comp", { textContent: () => (_toDisplayString(_ctx.foo)) }, null, true)
                                                                 ^^^^^^^^^^^^^^^^^

1 | <Comp v-text="foo" />
  | ^ base
  |       ^ PR
await await in <script setup>
<script setup>
await await foo() // foo() resolves to 42
</script>

base:

;(
  ([__temp,__restore] = _withAsyncContext(async () => (
  ([__temp,__restore] = _withAsyncContext(() => foo())),
  await __temp,
  __restore()
))),
  __temp = await __temp,
  __restore(),
  __temp
)

this PR:

;(
  ([__temp,__restore] = _withAsyncContext(async () => (
    ([__temp,__restore] = _withAsyncContext(() => foo())),
    __temp = await __temp,
    __restore(),
    __temp
  ))),
  await __temp,
  __restore()
)
base this PR
value the outer await gets undefined 42
`await` in a macro argument hoisted out of `setup()`
<script setup> base this PR
const n = defineModel('n', { default: await foo() }) SyntaxError: Unexpected token ')' renders, without <Suspense>
const n = defineModel('n', { get: v => v * 2, default: await foo() }) SyntaxError: Unexpected token '(' renders, without <Suspense>
const p = defineProps({ a: { default: await foo() } }) renders <!----> without <Suspense> renders, without <Suspense>
const p = withDefaults(defineProps<{ a?: number }>(), { a: await foo() }) renders <!----> without <Suspense> renders, without <Suspense>
const e = defineEmits(await foo()) throws Cannot split a chunk that has already been edited renders, without <Suspense>
defineOptions({ name: await foo() }) SyntaxError: Unexpected token ')' renders, without <Suspense>
`await` in a destructured prop default
<script setup> base this PR
const { a = await foo() } = defineProps(['a']) a: () => (await foo()), SyntaxError: Unexpected reserved word 'await' Default value of prop "a" cannot contain await because it is evaluated in a factory function, outside of setup().
const { a = { b: await foo() } } = defineProps<{ a?: object }>() default: () => ({ b: await foo() }), SyntaxError: Unexpected reserved word 'await' Default value of prop "a" cannot contain await because it is evaluated in a factory function, outside of setup().
`defineProps()` options hoisted out of `setup()`
  props: /*@__PURE__*/_mergeModels({ a: { default: foo() } }, {
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^

3 | const p = defineProps({ a: { default: foo() } })
  |                       ^ PR
base: unmapped
`defineModel()` options that can't be split
<script setup>, 2 instances base this PR
const n = defineModel('n', makeOptions()) makeOptions runs 3 times makeOptions runs once
case base this PR
compileScript map sources with inlineTemplate, filename C:\proj\src\App.vue ["C:/proj/src/App.vue", "C:\\proj\\src\\App.vue"] ["C:\\proj\\src\\App.vue"]
compileTemplate with inMap, in @vue/compiler-sfc bundled by esbuild with keepNames throws ReferenceError: __name is not defined compiles

Results

Base is the minor merge-base 747e8d6 and this PR is 2c1842a (branch tip now 045fae5; table still that commit), measured on 1297 .vue files with Node 24 on Apple Silicon, one round. CPU is user + sys.

Benchmark CPU base (ms) CPU this PR (ms) Peak RSS base (MB) Peak RSS this PR (MB)
compileScript warm, maps on (3795 calls) 2988 2108 (-29%) 848 563 (-34%)
compileScript cold, maps on (6130 calls) 4517 3575 (-21%) 759 445 (-41%)
compileTemplate, inMap, no ast (2574 calls) 1924 1220 (-37%) 631 475 (-25%)
parse, sourceMap (1297 files) 491 277 (-44%) 371 289 (-22%)
compileTemplate with ast 1283 1274 (-1%) 667 523 (-22%)
compiler-dom compile 349 350 (+0%) 292 292 (+0%)

Retained heap of 1297 parsed descriptors: base 113.4 MB, this PR 42.0 MB (-63%).

File Base (bytes) This PR (bytes)
compiler-sfc.esm-browser.js 1689178 1603089 (-5%)
compiler-vapor.esm-browser.js 953009 878592 (-8%)
vue.global.prod.js 180990 181029 (+0.02%)

vite.config.ts: Vitest browser mode's dep scan resolves @vue/compiler-core to its CJS entry and misses source dependencies (e.g. muggle-string), so Vite optimizes them late and reloads the test page. A scan plugin forces the source alias; the old per-project entities/decode optimizeDeps.exclude entries are removed.

Downstream

Package Issue PR
@vue/repl Breaking: MagicString is no longer re-exported from vue/compiler-sfc (or @vue/compiler-sfc). Intentional; import from magic-string instead. 4.7.2 and earlier break; fix is on main, to be released in 4.7.3. vuejs/repl#405
vue-simple-compiler Same MagicString import (MagicString is not a constructor at 30:13). Also bundleSourceMap throws on generated-only segments: compiler-inserted code has no source, so originalLine / originalColumn are null; copying as original: { line, column } fails at 167:19. Skip or pass original: null when m.source is null. Jinjiang/vue-simple-compiler#5
@vitejs/plugin-vue playground vue-sourcemap snapshots (serve-js, serve-ts, serve-html, serve-no-script, serve-empty-script, serve-no-template) change only mappings (token maps instead of per-character; generated-only segments on compiler-inserted lines). Update with -u. —
vue-macros short-bind sets prop.arg.loc.start.offset to Number.POSITIVE_INFINITY so Volar does not duplicate type hints; ranges must be in 0..source.length, so compile with sourceMap: true throws RangeError. Remove that poison offset upstream; Vue will not special-case it. vue-macros/vue-macros#1028

cursoragent and others added 6 commits November 4, 2026 05:09
EditedString has the API and types of the existing string-editing export. muggle-string renders its text and source maps; the maps follow muggle-string's rules, so generated code maps to the source it came from and inserted code maps to nothing.

muggle-string 1.0.2 is exempted from minimumReleaseAge, as it was published today.
compiler-sfc edits and exports EditedString in place of the existing string-editing export, under the same name and types, and no longer depends on it.
In inline mode, the script map is taken decoded from EditedString, the template map is read once with source-map-js, and the merged segments are written with encodeMappings of muggle-string, in the order and with the duplicates dropped that the source-map-js generator gives them. compiler-sfc no longer goes through the generator for the merge.
With sourceMap false, compileScript passes sourceMap false to the inline compileTemplate, so no template map is generated only to be dropped.
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4440e894-74cb-4fc4-b4d9-19e7ceb47938

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@16126
npm i https://pkg.pr.new/@vue/compiler-core@16126
yarn add https://pkg.pr.new/@vue/compiler-core@16126.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@16126
npm i https://pkg.pr.new/@vue/compiler-dom@16126
yarn add https://pkg.pr.new/@vue/compiler-dom@16126.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@16126
npm i https://pkg.pr.new/@vue/compiler-sfc@16126
yarn add https://pkg.pr.new/@vue/compiler-sfc@16126.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@16126
npm i https://pkg.pr.new/@vue/compiler-ssr@16126
yarn add https://pkg.pr.new/@vue/compiler-ssr@16126.tgz

@vue/compiler-vapor

pnpm add https://pkg.pr.new/@vue/compiler-vapor@16126
npm i https://pkg.pr.new/@vue/compiler-vapor@16126
yarn add https://pkg.pr.new/@vue/compiler-vapor@16126.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@16126
npm i https://pkg.pr.new/@vue/reactivity@16126
yarn add https://pkg.pr.new/@vue/reactivity@16126.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@16126
npm i https://pkg.pr.new/@vue/runtime-core@16126
yarn add https://pkg.pr.new/@vue/runtime-core@16126.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@16126
npm i https://pkg.pr.new/@vue/runtime-dom@16126
yarn add https://pkg.pr.new/@vue/runtime-dom@16126.tgz

@vue/runtime-vapor

pnpm add https://pkg.pr.new/@vue/runtime-vapor@16126
npm i https://pkg.pr.new/@vue/runtime-vapor@16126
yarn add https://pkg.pr.new/@vue/runtime-vapor@16126.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@16126
npm i https://pkg.pr.new/@vue/server-renderer@16126
yarn add https://pkg.pr.new/@vue/server-renderer@16126.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@16126
npm i https://pkg.pr.new/@vue/shared@16126
yarn add https://pkg.pr.new/@vue/shared@16126.tgz

vue

pnpm add https://pkg.pr.new/vue@16126
npm i https://pkg.pr.new/vue@16126
yarn add https://pkg.pr.new/vue@16126.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@16126
npm i https://pkg.pr.new/@vue/compat@16126
yarn add https://pkg.pr.new/@vue/compat@16126.tgz

commit: 2c1842a

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Size Report

Bundles

File Size Gzip Brotli
compiler-dom.global.prod.js 88.1 kB (+746 B) 31 kB (+283 B) 27.2 kB (+247 B)
runtime-dom.global.prod.js 121 kB 45.3 kB 40.5 kB
vue.global.prod.js 181 kB (+11 B) 65.5 kB (-6 B) 58.4 kB (-70 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 53.9 kB 21 kB 19.1 kB
createApp 62.9 kB 24.3 kB 22.1 kB
createApp + vaporInteropPlugin 118 kB 43 kB 38.7 kB
createVaporApp 34.2 kB 13.2 kB 12.2 kB
createSSRApp 67.8 kB 26.3 kB 23.8 kB
createVaporSSRApp 42 kB 16 kB 14.8 kB
defineCustomElement 69.5 kB 26.3 kB 23.9 kB
defineVaporCustomElement 49.1 kB 18 kB 16.5 kB
overall 78.4 kB 29.8 kB 27 kB

@johnsoncodehk

Copy link
Copy Markdown
Member Author

It seems 4 of the failures are related to this PR.

  • vite-plugin-vue: the source map test snapshots need updating.
  • vue-i18n, nuxt, vuetify: install fails because muggle-string@1.0.2 is younger than their minimumReleaseAge, they should pass on a rerun once it is old enough.

* build: use muggle-string with decodeMappings

* refactor(compiler-sfc): build block maps from segments

* refactor(compiler-sfc): remap template lines by segments

* refactor(compiler-sfc): decode the inlined template map

* refactor(compiler-core): build source maps from segments

* build(compiler-sfc): remove source-map-js dependency

* build: use muggle-string 1.0.3

* perf(compiler): cache decoded source map segments to avoid redundant decoding

Store decoded segments in WeakMap when encoding source maps, and retrieve
them when decoding to skip repeated decodeMappings calls.

The cache is owned by Vue's encoder side (compiler-core and compiler-sfc)
and keyed by the encoded map object. Consumers check the cache first and
fall back to decodeMappings on cache miss, so external maps still work.

Cache lives in:
- compiler-core/src/sourcemap.ts for maps from template codegen
- compiler-sfc/src/sourcemap.ts for maps from parse and transformations

Maps flow decoded through the pipeline:
- parse.ts generateSourceMap encodes + caches
- codegen.ts toJSON encodes + caches
- compileTemplate.ts mapLines uses cache + re-caches result
- compileScript.ts mergeSourceMaps uses cache + re-caches result

Parse cache preserves map object identity, so cached descriptors benefit
from segment reuse across multiple compileTemplate calls.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build: use muggle-string 1.0.2

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler): keep decoded segments of Vue encoded maps in one WeakMap

Source maps now travel decoded through the compiler and are only encoded
when written. Every map Vue encodes registers its segments in the
`decodedMappings` WeakMap exported by @vue/compiler-core (and re-exported
by @vue/compiler-sfc), keyed by the map object, so consumers read the
segments instead of decoding them.

Nothing in Vue decodes mappings anymore: the inMap of compileTemplate and
the template map merged by compileScript must be maps Vue encoded, and any
other map now throws.

This replaces the two duplicate caches in compiler-core and compiler-sfc
and their decode fallbacks, and restores mapLines returning oldMap when
there is no newMap.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler): keep codegen source map segments on the context

The codegen contexts of compiler-core and compiler-vapor now hold the
segments of each generated line and the name indexes directly, added with
addMapping, and generate builds the encoded map once at the end with
createSourceMap, which registers its segments in decodedMappings. This
drops the CodegenSourceMapGenerator object and its addMapping/toJSON
methods.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build: use muggle-string 1.0.3

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-sfc): decode source maps Vue did not encode

A map from a custom template compiler, or a descriptor map that was
serialized or cloned, is not in decodedMappings. Reading such a map is
where decoding belongs, so it is decoded with decodeMappings instead of
being rejected. Maps Vue encoded are still read from decodedMappings.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): carry segments on source maps instead of a WeakMap

A map Vue builds from segments keeps them in the non-enumerable
`_segments`, and its `mappings` is encoded when first read, after which
only the string is kept. mapLines and mergeSourceMaps read `_segments`
when present and otherwise decode `mappings`. The template block map from
parse, which is only read by compileTemplate with inMap and no ast, is
encoded right away and keeps no segments.

This removes the decodedMappings WeakMap, which kept the segments of
every map alive alongside the descriptors that hold them.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build(compiler-core): keep the source map encoder out of browser builds

Browser builds generate no codegen source maps, so defineMappings is a
no-op there like createSourceMap, which keeps encodeMappings out of the
compiler-dom browser bundles.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): encode source maps when built and decode when read

Every map Vue builds, from parse, codegen, mapLines and mergeSourceMaps,
is encoded to a plain string right away and keeps no segments; mapLines
and mergeSourceMaps decode mappings with decodeMappings when they need
segments. Keeping segments alive on the maps cost more CPU and memory
than decoding them again.

This removes defineMappings, getSegments and the `_segments` field.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@johnsoncodehk johnsoncodehk changed the title feat(compiler-sfc): render the string-editing export with muggle-string refactor(compiler): replace source-map-js and the string-editing dependency with muggle-string Nov 4, 2026
@johnsoncodehk johnsoncodehk changed the title refactor(compiler): replace source-map-js and the string-editing dependency with muggle-string refactor(compiler): replace magic-string and source-map-js with muggle-string Nov 4, 2026
johnsoncodehk and others added 2 commits November 4, 2026 20:36
…#19)

* build: use muggle-string with decodeMappings

* refactor(compiler-sfc): build block maps from segments

* refactor(compiler-sfc): remap template lines by segments

* refactor(compiler-sfc): decode the inlined template map

* refactor(compiler-core): build source maps from segments

* build(compiler-sfc): remove source-map-js dependency

* build: use muggle-string 1.0.3

* perf(compiler): cache decoded source map segments to avoid redundant decoding

Store decoded segments in WeakMap when encoding source maps, and retrieve
them when decoding to skip repeated decodeMappings calls.

The cache is owned by Vue's encoder side (compiler-core and compiler-sfc)
and keyed by the encoded map object. Consumers check the cache first and
fall back to decodeMappings on cache miss, so external maps still work.

Cache lives in:
- compiler-core/src/sourcemap.ts for maps from template codegen
- compiler-sfc/src/sourcemap.ts for maps from parse and transformations

Maps flow decoded through the pipeline:
- parse.ts generateSourceMap encodes + caches
- codegen.ts toJSON encodes + caches
- compileTemplate.ts mapLines uses cache + re-caches result
- compileScript.ts mergeSourceMaps uses cache + re-caches result

Parse cache preserves map object identity, so cached descriptors benefit
from segment reuse across multiple compileTemplate calls.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build: use muggle-string 1.0.2

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler): keep decoded segments of Vue encoded maps in one WeakMap

Source maps now travel decoded through the compiler and are only encoded
when written. Every map Vue encodes registers its segments in the
`decodedMappings` WeakMap exported by @vue/compiler-core (and re-exported
by @vue/compiler-sfc), keyed by the map object, so consumers read the
segments instead of decoding them.

Nothing in Vue decodes mappings anymore: the inMap of compileTemplate and
the template map merged by compileScript must be maps Vue encoded, and any
other map now throws.

This replaces the two duplicate caches in compiler-core and compiler-sfc
and their decode fallbacks, and restores mapLines returning oldMap when
there is no newMap.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler): keep codegen source map segments on the context

The codegen contexts of compiler-core and compiler-vapor now hold the
segments of each generated line and the name indexes directly, added with
addMapping, and generate builds the encoded map once at the end with
createSourceMap, which registers its segments in decodedMappings. This
drops the CodegenSourceMapGenerator object and its addMapping/toJSON
methods.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build: use muggle-string 1.0.3

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-sfc): decode source maps Vue did not encode

A map from a custom template compiler, or a descriptor map that was
serialized or cloned, is not in decodedMappings. Reading such a map is
where decoding belongs, so it is decoded with decodeMappings instead of
being rejected. Maps Vue encoded are still read from decodedMappings.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): carry segments on source maps instead of a WeakMap

A map Vue builds from segments keeps them in the non-enumerable
`_segments`, and its `mappings` is encoded when first read, after which
only the string is kept. mapLines and mergeSourceMaps read `_segments`
when present and otherwise decode `mappings`. The template block map from
parse, which is only read by compileTemplate with inMap and no ast, is
encoded right away and keeps no segments.

This removes the decodedMappings WeakMap, which kept the segments of
every map alive alongside the descriptors that hold them.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* build(compiler-core): keep the source map encoder out of browser builds

Browser builds generate no codegen source maps, so defineMappings is a
no-op there like createSourceMap, which keeps encodeMappings out of the
compiler-dom browser bundles.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): encode source maps when built and decode when read

Every map Vue builds, from parse, codegen, mapLines and mergeSourceMaps,
is encoded to a plain string right away and keeps no segments; mapLines
and mergeSourceMaps decode mappings with decodeMappings when they need
segments. Keeping segments alive on the maps cost more CPU and memory
than decoding them again.

This removes defineMappings, getSegments and the `_segments` field.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler): count the newlines before an identifier in its location

The line and column of a prefixed identifier were advanced over the
identifier's own text instead of the expression before it, so an
identifier after a newline kept the expression's first line and got a
column past its end. The offset was right; source maps pointed at the
wrong place.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-vapor): locate the selector of a v-for in the template

The selector expression of a createSelector call took the location of
its node in the parsed expression, whose line and column count from the
expression and which has no offset, so its source map pointed at the
start of the template.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): render template source maps from muggle-string pieces

Codegen of compiler-core and compiler-vapor adds a piece per push: the
text of a node maps to the node's range, and text without a node maps
to the end of the last node on its line, as an empty range. muggle-string
renders the map from the pieces, so codegen no longer tracks generated
lines and columns, and vapor no longer pads the code with line feeds
for its preamble.

The maps change where that helps debugging:
- the helper calls of an interpolation, a vnode call and a slot map to
  their node, e.g. `_toDisplayString(` maps to `{{` instead of the end
  of the element before it
- `_ctx.foo` has no name, so a debugger no longer shows the render
  context `_ctx` as `foo`
- code that a newline of the codegen starts maps nowhere instead of
  holding the last mapping of the line before

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler-sfc): inline the pieces of the render function into the script map

In inline mode the pieces of the template map go into the render of the
script at the text that holds the render function, so the map of the
script is rendered once with both, in place of decoding, merging and
sorting two maps. A template compiler that gives only a map has its
segments read as pieces. A template map that parse gave for the block
is a shift of lines and columns, so its first segment gives the shift,
in place of looking up each line.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-core): map the text call of a text node to its text

The createTextVNode call had no location, so a debugger stopping on it
fell back to whatever came before it on the line or showed no position.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler-core): map the indentation of a line with the range after it

A line indented before a located node wrote an unmapped segment for the
indentation and a mapped one for the node. Joined, the line starts with one
mapped segment, which finds the same position at any column of the line.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-vapor): map the display string call of an interpolation to its expression

As in the vdom codegen, a debugger stopping on the toDisplayString call of
an interpolation now shows the interpolated expression instead of no position.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
* refactor(compiler-sfc): rewrite default exports on a muggle-string rewrite

rewriteDefault and the normal script path build a SourceRewrite of the
script and render its text, instead of going through EditedString.
rewriteDefaultAST now takes that SourceRewrite and reads the original
text from its source.

The output is unchanged.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler-sfc): compile scripts on a muggle-string rewrite

ScriptCompileContext edits the SFC source through a SourceRewrite of
muggle-string instead of EditedString. Macros insert with insertBefore
and insertAfter, remove with an empty replace, and replace code through
ctx.replace, which writes it as a range of the source so that it maps
to the code it replaces. Hoisting places a cut at the start of the
output.

Code that goes in front of earlier code at one position is now built
in its final order before it is inserted: the helper imports, the
template preamble and the import of a re-exported default before all
of the output, and the setup() head, the async temp variables and the
css vars code at the start of <script setup>. The output is unchanged.

The output and its map are rendered in one pass, with
renderWithSourceMap, or renderWithDecodedSourceMap when the template
map is merged in, instead of a render for the text and another for the
map.

The map names the file as given in `sources`, as the block and template
maps do. A Windows path used to be written with forward slashes in the
script map only, so an inline map listed the same file twice, with its
content twice.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler-sfc): remove EditedString

Nothing in compiler-sfc edits through EditedString anymore. Remove it,
its tests, and its export under the old name, whose users can create a
SourceRewrite with muggle-string instead.

BREAKING CHANGE: @vue/compiler-sfc no longer exports the string editor
that EditedString stood in for. rewriteDefaultAST takes a SourceRewrite
of muggle-string, and ScriptCompileContext.s is one.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@netlify

netlify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for vue-sfc-playground failed. Why did it fail? →

Name Link
🔨 Latest commit 36364c9
🔍 Latest deploy log https://app.netlify.com/projects/vue-sfc-playground/deploys/6aeb53c1ff07cb553ed893b4

cursoragent and others added 2 commits November 4, 2026 13:20
@vue/repl imports a string editor from vue/compiler-sfc, which no longer exports one, so the playground build failed with a missing export. The playground resolves that import to a module that re-exports compiler-sfc with an editor for the methods the module compiler uses.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…ng editor"

This reverts commit c472118.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
johnsoncodehk and others added 5 commits November 4, 2026 23:38
…emapping (#21)

* refactor(compiler-sfc): shift template pieces instead of remapping

Pieces compiled from the template block are shifted by its offset in the SFC, and codegen takes the caller's source so inline maps check it by identity. Preprocessed templates are left unmapped, since the preprocessor gives no positions.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler-sfc): generate block pieces at their offset in the SFC

Codegen takes the offset of the template in the SFC, so the map it renders for a template compiled from the block content is the final one, instead of shifting the pieces and rendering a second map.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
* refactor(compiler-sfc): shift template pieces instead of remapping

Pieces compiled from the template block are shifted by its offset in the SFC, and codegen takes the caller's source so inline maps check it by identity. Preprocessed templates are left unmapped, since the preprocessor gives no positions.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler-sfc): generate block pieces at their offset in the SFC

Codegen takes the offset of the template in the SFC, so the map it renders for a template compiled from the block content is the final one, instead of shifting the pieces and rendering a second map.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-core): map text starting a line to the node it is generated in

Closing lines such as `]))` carried a segment without a source, so a
position on them mapped nowhere. They now map to the end of the node they
close. The comment added for a v-if without v-else still maps nowhere.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* fix(compiler-core): map a line to the node its first text is generated in

The node is taken when the line's first text is pushed, not at the newline,
so a v-if test or a slot's closing bracket maps to its own node. A slot
function has the loc of its first child, so it does not count as the node.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
… inMap (#23)

* refactor(compiler-sfc): shift template pieces instead of remapping

Pieces compiled from the template block are shifted by its offset in the SFC, and codegen takes the caller's source so inline maps check it by identity. Preprocessed templates are left unmapped, since the preprocessor gives no positions.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler-sfc): generate block pieces at their offset in the SFC

Codegen takes the offset of the template in the SFC, so the map it renders for a template compiled from the block content is the final one, instead of shifting the pieces and rendering a second map.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler-sfc): place the template in the SFC by its AST, not inMap

The AST of the descriptor's template now has the loc of the block, so
compileTemplate takes the SFC and the block's start from it, also for a
custom compiler that cannot reuse it. Errors are moved by that start,
columns on its first line included. inMap is no longer read.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* test(compiler-sfc): place errors of a template whose content repeats earlier in the SFC

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…pieces (#24)

* refactor(compiler-sfc): shift template pieces instead of remapping

Pieces compiled from the template block are shifted by its offset in the SFC, and codegen takes the caller's source so inline maps check it by identity. Preprocessed templates are left unmapped, since the preprocessor gives no positions.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler-sfc): generate block pieces at their offset in the SFC

Codegen takes the offset of the template in the SFC, so the map it renders for a template compiled from the block content is the final one, instead of shifting the pieces and rendering a second map.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* refactor(compiler-sfc): place the template in the SFC by its AST, not inMap

The AST of the descriptor's template now has the loc of the block, so
compileTemplate takes the SFC and the block's start from it, also for a
custom compiler that cannot reuse it. Errors are moved by that start,
columns on its first line included. inMap is no longer read.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* test(compiler-sfc): place errors of a template whose content repeats earlier in the SFC

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

* perf(compiler): render no template map for a caller that renders the pieces

compileScript inlines the template by its pieces and never read the map
that codegen rendered from them. Codegen given pieceSource now leaves the
map to its caller, and the preamble of an inlined render function builds
no pieces.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
compiler-sfc no longer exports MagicString, which @vue/repl 4.7.2
imports, so the playground preview fails in dev. repl main (59212b9)
rewrites modules without it; use its pkg.pr.new build until a release
ships it.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@johnsoncodehk
johnsoncodehk force-pushed the cursor/muggle-string-0eaa branch from c2af455 to 1fb793e Compare November 4, 2026 18:07
The pieces of template codegen dropped the names that the source-map
codegen gave, e.g. `foo` for `_ctx.foo`. A located identifier whose
generated text differs from it now names its piece, which muggle-string
writes to `names`.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
cursoragent and others added 13 commits November 4, 2026 19:12
…plays it

A text value kept only its expression, so `toDisplayString(` mapped to the
expression in the interpolation instead of its `{{`, and that of a v-text on
a component mapped nowhere. Set-text operations now keep the loc of the text,
interpolation or v-text that displays each value, and a v-text prop that is
displayed keeps the loc of its directive, as in compiler-dom.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…internal

addPiece, createSourcePiece, createSourceMap and CodegenPiece are exported
only for the codegen of compiler-vapor and compiler-sfc, so they and the
`pieces` of the codegen contexts are internal and stay out of the d.ts.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
… at the <script setup> tags by rank

Each piece of that code is inserted where it is produced, by the writer of
its rank, instead of being kept in a variable and joined into one string in
its final order: the helper imports, the template preamble and the import of
a re-exported default before all of the output, and the setup() head, the
async temp variables, the css vars code, the returned bindings and the end of
setup() at the tags of <script setup>. The ranks decide their order, so it no
longer depends on where the code is joined or on the order of the calls.
The output is unchanged.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…waits in it

In `await await foo`, both awaits end at one char, and the code after each was
placed there in the order of the calls: the outer await, processed first, put
its code inside the inner one's. Each await now writes that code by the writer
of a rank above those of the awaits in it, so the inner await's code is inside
the outer one's whatever the order of the calls.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
… setup() to the module

The arguments of defineProps, withDefaults, defineEmits, defineOptions and
the prop options of defineModel are hoisted into the component options, so
their code runs in the module, not in setup(). The walk for top-level awaits
still processed an await in them: it made setup() async and wrote the async
context code into their removed range, as `_useModel(__props, 'n'(` or
`__props(\n  ([__temp,__restore] = _withAsyncContext(() => ))`, or, before
this branch, threw for defineEmits.

The walk now skips the hoisted arguments, which one list gives to it and to
the check of their references to setup() bindings, so an await in them is the
module's, as their other code is.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…rapped in a factory

A destructured prop default that is not a literal, a function or an external
identifier is placed in a factory function of the props, which runs for each
instance outside of setup(). An await in it is not in an async function
there: the output did not parse (`a: () => (await foo())`), and no factory
can give the awaited value, as an async one gives a promise. This is the
case an await in a hoisted macro argument is not the module's, so it is now
an error naming the prop.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
The props, emits, options and model options copied into the component
options were written as strings and mapped nowhere. Generate them as code
whose copies of the source are ranges of it, so they map to it.
extractRuntimeProps keeps returning a string.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…be split

Options that are not an object of static keys were copied into the
props of the component and left in the useModel() call, so they were
evaluated in the module and again in every setup(), and a reference to
a local of <script setup> threw when the module loaded. Hoist them to a
module-level declaration that both read, and check them as hoisted
macro arguments.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
The render function was inserted as text and, when rendering the map,
found again by searching the rendered text for it to splice its pieces
in. Insert its pieces instead.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
1.0.3 is deprecated. 1.0.2 is past minimumReleaseAge, so its exemption
is removed.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
…f push()

The code pushed to a CodegenContext is no longer tracked by position,
so push() reads no newlineIndex: newline() appends its unmapped text
itself. Both are kept for compatibility, to be removed in a later minor.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
The rule that text without a node maps to the end of the last node on its
line, or else to the end of the node it is generated in, lived in core's
codegen context and, without the fallback, again in Vapor's
codeFragmentToString. It is now one piece writer in compiler-core
(createPieceWriter, internal), used by both. Vapor gives the node through a
fragment of the code of a compound expression, so the leading text of an
expression on a line with nothing mapped before it, e.g. the `!` of
`const __ok = !_ctx.ok`, maps to the end of the expression instead of
nowhere.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
Every map the compilers give has `version: 3`, but RawSourceMap typed it a
string, as source-map-js does, so the maps rendered by muggle-string and the
one parse builds were cast through unknown. The type is now a number and the
casts are gone. postcss's map, typed by source-map-js, gets the number
restated when compileStyle returns it; the tests that hand maps to
source-map-js's consumer pass them untyped, as some already did.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@johnsoncodehk

Copy link
Copy Markdown
Member Author

/ecosystem-ci run

Vitest browser mode makes the dependency scan resolve @vue/compiler-core to
its CJS entry, so the scan missed the dependencies of the aliased source
(entities/decode, muggle-string) and Vite reloaded the test page when it
optimized them late, failing the import of the vapor e2e test. Resolve the
package to its source in the scan as an interim, and drop the
entities/decode excludes it made necessary.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
@vuejs vuejs deleted a comment from johnsoncodehk Nov 5, 2026
@vue-bot

vue-bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📝 Ran ecosystem CI: Open

suite result latest scheduled
test-utils ✅ success ✅ success
vueuse ✅ success ✅ success
language-tools ✅ success ✅ success
vitepress ✅ success ✅ success
vue-macros ❌ failure ✅ success
nuxt ❌ failure ✅ success
vant ✅ success ✅ success
primevue ✅ success ✅ success
radix-vue ❌ failure ✅ success
quasar ✅ success ✅ success
pinia ✅ success ✅ success
vue-simple-compiler ❌ failure ✅ success
vue-i18n ❌ failure ✅ success
router ❌ failure ✅ success
vuetify ❌ failure ✅ success
vite-plugin-vue ❌ failure ✅ success

cursoragent and others added 10 commits November 5, 2026 10:02
The test stripped the pieces of a compiler that, given the piece source,
also returns no map, so it asserted the template unmapped while giving
nothing to map. A third-party compiler maps its input; its map is placed at
the template block, as it was before the pieces. Give the test such a
compiler and mark it expected-fail until compileTemplate places that map.
…pected-fail

The preprocessor gives no positions of its output in the template, so no
map of it can be right and none is given. List the map it blocks as an
expected-fail test beside the one asserting no wrong map.
…ce-map-js

The packages no longer depend on source-map-js; only the tests read maps
with it. Read them with trace-mapping, a consumer independent of
muggle-string, which renders the maps, and drop source-map-js from the root
devDependencies and the catalog.

Regenerating the lockfile also resolves the vue that the benchmark's
@vitejs/plugin-vue gets as an auto-installed peer to 3.6.0-rc.14.
The explorer was the last part of the repo reading maps with
source-map-js. Its highlight spans up to the next mapping on the line, as
the column spans of source-map-js did. A cursor between mappings now finds
one on its own line only, instead of the last one of an earlier line.
…ource-map-js

The packages render source maps with muggle-string and no longer depend on
source-map-js; only the tests read maps with it, a second implementation of
the format in the repo. Decode the maps with muggle-string's
decodeMappings, which needs muggle-string 1.0.3, and drop source-map-js from
the root devDependencies and the catalog.

Regenerating the lockfile also resolves the vue that the benchmark's
@vitejs/plugin-vue gets as an auto-installed peer to 3.6.0-rc.14.
Decode the map with muggle-string's decodeMappings, which the bundle already
carries through the compiler, instead of a source-map-js consumer. The
template-to-output lookup stays on the line of the cursor rather than falling
back to an earlier line.
The code that an await nested in another await's argument generates now sits one level deeper than the outer await's, and its closing parenthesis lines up with the line it opens on. Only whitespace changes; the code and the mapped and unmapped ranges of its source map are the same.

Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants