Skip to content

Commit fea06dd

Browse files
authored
refactor: use parseAst from rolldown (#81)
* refactor: use parseAst from rolldown other than ssrTransform * refactor: use parseAst from rolldown for ssrTransform * chore: remove `rollup/parseAst` from bundle config
1 parent 992b031 commit fea06dd

File tree

11 files changed

+192
-41
lines changed

11 files changed

+192
-41
lines changed

packages/vite/rollup.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ const nodeConfig = defineConfig({
110110
external: [
111111
/^vite\//,
112112
'fsevents',
113-
'rollup/parseAst',
113+
'rolldown/parseAst',
114+
'rolldown/experimental',
114115
/^tsx\//,
115116
/^#/,
116-
'rolldown/experimental',
117117
...Object.keys(pkg.dependencies),
118118
...Object.keys(pkg.peerDependencies),
119119
],
@@ -191,7 +191,7 @@ const moduleRunnerConfig = defineConfig({
191191
external: [
192192
'fsevents',
193193
'lightningcss',
194-
'rollup/parseAst',
194+
'rolldown/parseAst',
195195
...Object.keys(pkg.dependencies),
196196
],
197197
plugins: [

packages/vite/rollup.dts.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const pkg = JSON.parse(
1616
const external = [
1717
/^node:*/,
1818
/^vite\//,
19-
'rollup/parseAst',
19+
'rolldown/parseAst',
2020
'rolldown/experimental',
2121
...Object.keys(pkg.dependencies),
2222
...Object.keys(pkg.peerDependencies),

packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, expect, test } from 'vitest'
2-
import { parseAst } from 'rollup/parseAst'
32
import { assetImportMetaUrlPlugin } from '../../plugins/assetImportMetaUrl'
43
import { resolveConfig } from '../../config'
54
import { PartialEnvironment } from '../../baseEnvironment'
@@ -12,7 +11,7 @@ async function createAssetImportMetaurlPluginTransform() {
1211
return async (code: string) => {
1312
// @ts-expect-error transform.handler should exist
1413
const result = await instance.transform.handler.call(
15-
{ environment, parse: parseAst },
14+
{ environment },
1615
code,
1716
'foo.ts',
1817
)

packages/vite/src/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as Rollup from 'rolldown'
22

33
export type { Rollup }
4-
export { parseAst, parseAstAsync } from 'rollup/parseAst'
4+
export { parseAst, parseAstAsync } from './parseAst'
55
export {
66
defineConfig,
77
loadConfigFromFile,

packages/vite/src/node/parseAst.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import MagicString, {
2+
type SourceMap,
3+
type SourceMapOptions,
4+
} from 'magic-string'
5+
import {
6+
type ParseResult,
7+
parseAst as rolldownParseAst,
8+
parseAstAsync as rolldownParseAstAsync,
9+
} from 'rolldown/parseAst'
10+
11+
// TODO: move this compat layer to Rolldown
12+
13+
const parseAstGeneric = (
14+
code: string,
15+
opts?: any,
16+
filename?: string,
17+
): ParseResult => {
18+
const result = rolldownParseAst(filename ?? 'file.js', code, {
19+
sourceType: 'module',
20+
lang: 'js',
21+
...opts,
22+
})
23+
if (result.errors.length > 0) {
24+
throw new AggregateError(result.errors)
25+
}
26+
27+
return result
28+
}
29+
30+
export const parseAstGenericAsync = async (
31+
code: string,
32+
opts?: any,
33+
filename?: string,
34+
): Promise<ParseResult> => {
35+
const result = await rolldownParseAstAsync(filename ?? 'file.js', code, {
36+
sourceType: 'module',
37+
lang: 'js',
38+
...opts,
39+
})
40+
if (result.errors.length > 0) {
41+
throw new AggregateError(result.errors)
42+
}
43+
44+
return result
45+
}
46+
47+
export class MagicStringWrapper {
48+
private oxcMs: ParseResult['magicString']
49+
private ms: MagicString
50+
51+
constructor(s: ParseResult['magicString']) {
52+
this.oxcMs = s
53+
this.ms = new MagicString(s.toString())
54+
}
55+
56+
private getO(pos: number): number {
57+
return this.oxcMs.getUtf16ByteOffset(pos)
58+
}
59+
60+
append(str: string): void {
61+
this.ms.append(str)
62+
}
63+
64+
appendLeft(start: number, str: string): void {
65+
this.ms.appendLeft(this.getO(start), str)
66+
}
67+
68+
prependRight(start: number, str: string): void {
69+
this.ms.prependRight(this.getO(start), str)
70+
}
71+
72+
update(start: number, end: number, str: string): void {
73+
this.ms.update(this.getO(start), this.getO(end), str)
74+
}
75+
76+
move(start: number, end: number, index: number): void {
77+
this.ms.move(this.getO(start), this.getO(end), this.getO(index))
78+
}
79+
80+
remove(start: number, end: number): void {
81+
this.ms.remove(this.getO(start), this.getO(end))
82+
}
83+
84+
generateMap(options: SourceMapOptions): SourceMap {
85+
return this.ms.generateMap(options)
86+
}
87+
88+
toString(): string {
89+
return this.ms.toString()
90+
}
91+
}
92+
93+
export const parseAst = (code: string, opts?: any, filename?: string): any => {
94+
return parseAstGeneric(code, opts, filename).program
95+
}
96+
97+
export const parseAstAsync = async (
98+
code: string,
99+
opts?: any,
100+
filename?: string,
101+
): Promise<any> => {
102+
const result = await parseAstGenericAsync(code, opts, filename)
103+
return result.program
104+
}

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'node:path'
22
import MagicString from 'magic-string'
33
import { stripLiteral } from 'strip-literal'
4-
import { parseAst } from 'rollup/parseAst'
54
import type { RolldownPlugin } from 'rolldown'
5+
import { parseAst } from '../parseAst'
66
import type { ResolvedConfig } from '../config'
77
import {
88
injectQuery,

packages/vite/src/node/plugins/dynamicImportVars.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { posix } from 'node:path'
22
import MagicString from 'magic-string'
33
import { init, parse as parseImports } from 'es-module-lexer'
44
import type { ImportSpecifier } from 'es-module-lexer'
5-
import { parseAst } from 'rollup/parseAst'
65
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
6+
import { parseAst } from '../parseAst'
77
import type { Plugin } from '../plugin'
88
import type { ResolvedConfig } from '../config'
99
import { CLIENT_ENTRY } from '../constants'

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import type {
99
ImportSpecifier,
1010
} from 'es-module-lexer'
1111
import { init, parse as parseImports } from 'es-module-lexer'
12-
import { parseAst } from 'rollup/parseAst'
1312
import type { StaticImport } from 'mlly'
1413
import { ESM_STATIC_IMPORT_RE, parseStaticImport } from 'mlly'
1514
import { makeLegalIdentifier } from '@rollup/pluginutils'
1615
import type { PartialResolvedId, RollupError } from 'rolldown'
1716
import type { Identifier, Literal } from 'estree'
17+
import { parseAst } from '../parseAst'
1818
import {
1919
CLIENT_DIR,
2020
CLIENT_PUBLIC_PATH,
@@ -970,7 +970,8 @@ export function transformCjsImport(
970970
node.type === 'ImportDeclaration' ||
971971
node.type === 'ExportNamedDeclaration'
972972
) {
973-
if (!node.specifiers.length) {
973+
// NOTE: node.specifiers can be null in OXC: https://github.com/oxc-project/oxc/issues/2854#issuecomment-2595115817
974+
if (!node.specifiers?.length) {
974975
return `import "${url}"`
975976
}
976977

packages/vite/src/node/plugins/importMetaGlob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type { CustomPluginOptions, RollupError } from 'rolldown'
1515
import MagicString from 'magic-string'
1616
import { stringifyQuery } from 'ufo'
1717
import type { GeneralImportGlobOptions } from 'types/importGlob'
18-
import { parseAstAsync } from 'rollup/parseAst'
1918
import { escapePath, glob } from 'tinyglobby'
19+
import { parseAstAsync } from '../parseAst'
2020
import type { Plugin } from '../plugin'
2121
import type { EnvironmentModuleNode } from '../server/moduleGraph'
2222
import type { ResolvedConfig } from '../config'

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ SOFTWARE.
3232
import fs from 'node:fs'
3333
import { join } from 'node:path'
3434
import { performance } from 'node:perf_hooks'
35-
import { parseAst as rollupParseAst } from 'rollup/parseAst'
3635
import type {
3736
AsyncPluginHooks,
3837
CustomPluginOptions,
@@ -62,6 +61,7 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
6261
import MagicString from 'magic-string'
6362
import type { FSWatcher } from 'dep-types/chokidar'
6463
import colors from 'picocolors'
64+
import { parseAst as rolldownParseAst } from '../parseAst'
6565
import type { Plugin } from '../plugin'
6666
import {
6767
combineSourcemaps,
@@ -578,7 +578,7 @@ class PluginContext
578578
}
579579

580580
parse(code: string, opts: any) {
581-
return rollupParseAst(code, opts)
581+
return rolldownParseAst(code, opts)
582582
}
583583

584584
async resolve(

0 commit comments

Comments
 (0)