Skip to content

Commit 83f46e2

Browse files
committed
chore: use convertSpanUtf16 and remove compat layer
1 parent 94eaa87 commit 83f46e2

File tree

2 files changed

+6
-58
lines changed

2 files changed

+6
-58
lines changed

packages/vite/src/node/parseAst.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import MagicString, {
2-
type SourceMap,
3-
type SourceMapOptions,
4-
} from 'magic-string'
51
import {
62
type ParseResult,
73
parseAst as rolldownParseAst,
@@ -19,6 +15,7 @@ const parseAstGeneric = (
1915
sourceType: 'module',
2016
lang: 'js',
2117
preserveParens: false,
18+
convertSpanUtf16: true,
2219
...opts,
2320
})
2421
if (result.errors.length > 0) {
@@ -37,6 +34,7 @@ export const parseAstGenericAsync = async (
3734
sourceType: 'module',
3835
lang: 'js',
3936
preserveParens: false,
37+
convertSpanUtf16: true,
4038
...opts,
4139
})
4240
if (result.errors.length > 0) {
@@ -46,52 +44,6 @@ export const parseAstGenericAsync = async (
4644
return result
4745
}
4846

49-
export class MagicStringWrapper {
50-
private oxcMs: ParseResult['magicString']
51-
private ms: MagicString
52-
53-
constructor(s: ParseResult['magicString']) {
54-
this.oxcMs = s
55-
this.ms = new MagicString(s.toString())
56-
}
57-
58-
private getO(pos: number): number {
59-
return this.oxcMs.getUtf16ByteOffset(pos)
60-
}
61-
62-
append(str: string): void {
63-
this.ms.append(str)
64-
}
65-
66-
appendLeft(start: number, str: string): void {
67-
this.ms.appendLeft(this.getO(start), str)
68-
}
69-
70-
prependRight(start: number, str: string): void {
71-
this.ms.prependRight(this.getO(start), str)
72-
}
73-
74-
update(start: number, end: number, str: string): void {
75-
this.ms.update(this.getO(start), this.getO(end), str)
76-
}
77-
78-
move(start: number, end: number, index: number): void {
79-
this.ms.move(this.getO(start), this.getO(end), this.getO(index))
80-
}
81-
82-
remove(start: number, end: number): void {
83-
this.ms.remove(this.getO(start), this.getO(end))
84-
}
85-
86-
generateMap(options: SourceMapOptions): SourceMap {
87-
return this.ms.generateMap(options)
88-
}
89-
90-
toString(): string {
91-
return this.ms.toString()
92-
}
93-
}
94-
9547
export const parseAst = (code: string, opts?: any, filename?: string): any => {
9648
return parseAstGeneric(code, opts, filename).program
9749
}

packages/vite/src/node/ssr/ssrTransform.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import MagicString from 'magic-string'
23
import type { RollupAstNode } from 'rollup'
34
import type { SourceMap } from 'rolldown'
45
import type {
@@ -17,10 +18,7 @@ import type {
1718
import { extract_names as extractNames } from 'periscopic'
1819
import { walk as eswalk } from 'estree-walker'
1920
import type { RawSourceMap } from '@ampproject/remapping'
20-
import {
21-
MagicStringWrapper,
22-
parseAstGenericAsync as rolldownParseAstGenericAsync,
23-
} from '../parseAst'
21+
import { parseAstAsync as rolldownParseAstAsync } from '../parseAst'
2422
import type { TransformResult } from '../server/transformRequest'
2523
import {
2624
combineSourcemaps,
@@ -82,12 +80,10 @@ async function ssrTransformScript(
8280
url: string,
8381
originalCode: string,
8482
): Promise<TransformResult | null> {
83+
const s = new MagicString(code)
8584
let ast: any
86-
let s: MagicStringWrapper
8785
try {
88-
const result = await rolldownParseAstGenericAsync(code)
89-
ast = result.program
90-
s = new MagicStringWrapper(result.magicString)
86+
ast = await rolldownParseAstAsync(code)
9187
} catch (err) {
9288
// enhance known rollup errors
9389
// https://github.com/rollup/rollup/blob/42e587e0e37bc0661aa39fe7ad6f1d7fd33f825c/src/utils/bufferToAst.ts#L17-L22

0 commit comments

Comments
 (0)