Skip to content

Commit a3b28e8

Browse files
authored
fix(build): keep magicString and ast in transform hook meta (#24012)
1 parent dd780cd commit a3b28e8

4 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎packages/vite/rolldown.dts.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const identifierReplacements: Record<string, Record<string, string>> = {
8383
'rolldown/utils': {
8484
TransformOptions$1: 'rolldown_utils_TransformOptions',
8585
TransformResult$2: 'rolldown_utils_TransformResult',
86+
ESTree$1: 'rolldown_utils_ESTree',
8687
},
8788
'node:http': {
8889
Server$1: 'http.Server',

‎packages/vite/src/node/__tests__/build.spec.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,52 @@ describe('build', () => {
9494
assertOutputHashContentChange(result[0], result[1])
9595
})
9696

97+
test('transform hook meta keeps rolldown magicString and ast', async () => {
98+
const metaTypes: Array<
99+
[string | undefined, string | undefined, boolean | undefined]
100+
> = []
101+
const result = (await build({
102+
root: resolve(dirname, 'packages/build-project'),
103+
logLevel: 'silent',
104+
build: {
105+
write: false,
106+
minify: false,
107+
},
108+
plugins: [
109+
{
110+
name: 'test',
111+
resolveId(id) {
112+
if (id === 'entry.js') {
113+
return '\0' + id
114+
}
115+
},
116+
load(id) {
117+
if (id === '\0entry.js') {
118+
return `console.log('foo')`
119+
}
120+
},
121+
transform(_code, id, options) {
122+
if (id === '\0entry.js') {
123+
metaTypes.push([
124+
typeof options?.magicString,
125+
typeof options?.ast,
126+
options?.ssr,
127+
])
128+
const magicString = options!.magicString!
129+
magicString.replaceAll(`'foo'`, `'bar'`)
130+
return { code: magicString }
131+
}
132+
},
133+
},
134+
],
135+
})) as RolldownOutput
136+
expect(metaTypes).toStrictEqual([['object', 'object', false]])
137+
const [chunk] = result.output.filter(
138+
(chunk): chunk is OutputChunk => chunk.type === 'chunk',
139+
)
140+
expect(chunk?.code).toContain(`console.log("bar")`)
141+
})
142+
97143
test('file hash should change when renderBuiltUrl changes', async () => {
98144
const createRenderBuiltUrl = (base: string) => (filename: string) =>
99145
`${base}/${filename}`

‎packages/vite/src/node/build.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,9 +1544,16 @@ function injectSsrFlag<T extends Record<string, any>>(
15441544
pluginName: string,
15451545
): T & { ssr?: boolean } {
15461546
let ssr = environment.config.consumer === 'server'
1547-
const newOptions = { ...options, ssr } as T & {
1548-
ssr?: boolean
1549-
}
1547+
// spreading the options would drop non-enumerable properties (e.g. the lazy
1548+
// `magicString` and `ast` getters rolldown attaches to the transform meta),
1549+
// so copy the property descriptors instead
1550+
const newOptions = Object.defineProperties(
1551+
{},
1552+
{
1553+
...(options ? Object.getOwnPropertyDescriptors(options) : {}),
1554+
ssr: { value: ssr, writable: true, enumerable: true, configurable: true },
1555+
},
1556+
) as T & { ssr?: boolean }
15501557

15511558
if (
15521559
isFutureDeprecationEnabled(

‎packages/vite/src/node/plugin.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import type {
1010
PluginContextMeta,
1111
ResolveIdResult,
1212
Plugin as RolldownPlugin,
13+
RolldownMagicString,
1314
TransformPluginContext,
1415
TransformResult,
1516
} from 'rolldown'
17+
import type { ESTree } from 'rolldown/utils'
1618
import type { PartialEnvironment } from './baseEnvironment'
1719
import type { BuildAppHook } from './build'
1820
import type {
@@ -163,6 +165,18 @@ export interface Plugin<A = any> extends RolldownPlugin<A> {
163165
options?: {
164166
moduleType: ModuleType
165167
ssr?: boolean | undefined
168+
/**
169+
* A lazily-created MagicString instance for the module's code.
170+
* Only available during build.
171+
* @experimental
172+
*/
173+
magicString?: RolldownMagicString | undefined
174+
/**
175+
* The lazily-parsed AST of the module's code.
176+
* Only available during build.
177+
* @experimental
178+
*/
179+
ast?: ESTree.Program | undefined
166180
},
167181
) => Promise<TransformResult> | TransformResult,
168182
{

0 commit comments

Comments
 (0)