Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/rolldown.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const identifierReplacements: Record<string, Record<string, string>> = {
'rolldown/utils': {
TransformOptions$1: 'rolldown_utils_TransformOptions',
TransformResult$2: 'rolldown_utils_TransformResult',
ESTree$1: 'rolldown_utils_ESTree',
},
'node:http': {
Server$1: 'http.Server',
Expand Down
46 changes: 46 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,52 @@ describe('build', () => {
assertOutputHashContentChange(result[0], result[1])
})

test('transform hook meta keeps rolldown magicString and ast', async () => {
const metaTypes: Array<
[string | undefined, string | undefined, boolean | undefined]
> = []
const result = (await build({
root: resolve(dirname, 'packages/build-project'),
logLevel: 'silent',
build: {
write: false,
minify: false,
},
plugins: [
{
name: 'test',
resolveId(id) {
if (id === 'entry.js') {
return '\0' + id
}
},
load(id) {
if (id === '\0entry.js') {
return `console.log('foo')`
}
},
transform(_code, id, options) {
if (id === '\0entry.js') {
metaTypes.push([
typeof options?.magicString,
typeof options?.ast,
options?.ssr,
])
const magicString = options!.magicString!
magicString.replaceAll(`'foo'`, `'bar'`)
return { code: magicString }
}
},
},
],
})) as RolldownOutput
expect(metaTypes).toStrictEqual([['object', 'object', false]])
const [chunk] = result.output.filter(
(chunk): chunk is OutputChunk => chunk.type === 'chunk',
)
expect(chunk?.code).toContain(`console.log("bar")`)
})

test('file hash should change when renderBuiltUrl changes', async () => {
const createRenderBuiltUrl = (base: string) => (filename: string) =>
`${base}/${filename}`
Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,16 @@ function injectSsrFlag<T extends Record<string, any>>(
pluginName: string,
): T & { ssr?: boolean } {
let ssr = environment.config.consumer === 'server'
const newOptions = { ...options, ssr } as T & {
ssr?: boolean
}
// spreading the options would drop non-enumerable properties (e.g. the lazy
// `magicString` and `ast` getters rolldown attaches to the transform meta),
// so copy the property descriptors instead
const newOptions = Object.defineProperties(
{},
{
...(options ? Object.getOwnPropertyDescriptors(options) : {}),
ssr: { value: ssr, writable: true, enumerable: true, configurable: true },
},
) as T & { ssr?: boolean }

if (
isFutureDeprecationEnabled(
Expand Down
14 changes: 14 additions & 0 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import type {
PluginContextMeta,
ResolveIdResult,
Plugin as RolldownPlugin,
RolldownMagicString,
TransformPluginContext,
TransformResult,
} from 'rolldown'
import type { ESTree } from 'rolldown/utils'
import type { PartialEnvironment } from './baseEnvironment'
import type { BuildAppHook } from './build'
import type {
Expand Down Expand Up @@ -163,6 +165,18 @@ export interface Plugin<A = any> extends RolldownPlugin<A> {
options?: {
moduleType: ModuleType
ssr?: boolean | undefined
/**
* A lazily-created MagicString instance for the module's code.
* Only available during build.
* @experimental
*/
magicString?: RolldownMagicString | undefined
/**
* The lazily-parsed AST of the module's code.
* Only available during build.
* @experimental
*/
ast?: ESTree.Program | undefined
},
) => Promise<TransformResult> | TransformResult,
{
Expand Down
Loading