Skip to content

Commit 5b6b016

Browse files
authored
fix(build): resolve rollupOptions.input paths (#5601)
1 parent a17da55 commit 5b6b016

File tree

4 files changed

+109
-20
lines changed

4 files changed

+109
-20
lines changed

packages/plugin-legacy/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ async function buildPolyfillChunk(
596596
bundle[polyfillChunk.name] = polyfillChunk
597597
}
598598

599-
const polyfillId = 'vite/legacy-polyfills'
599+
const polyfillId = '\0vite/legacy-polyfills'
600600

601601
/**
602602
* @param {Set<string>} imports

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolveLibFilename } from '../build'
22
import { resolve } from 'path'
3+
import { resolveConfig } from '..'
34

45
describe('resolveLibFilename', () => {
56
test('custom filename function', () => {
@@ -65,3 +66,72 @@ describe('resolveLibFilename', () => {
6566
}).toThrow()
6667
})
6768
})
69+
70+
describe('resolvePaths', () => {
71+
test('resolve build.rollupOptions.input', async () => {
72+
const config = await resolveConfig({
73+
build: {
74+
rollupOptions: {
75+
input: 'index.html'
76+
}
77+
}
78+
}, 'build', 'production')
79+
80+
expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
81+
})
82+
test('resolve build.rollupOptions.input{}', async () => {
83+
const config = await resolveConfig({
84+
build: {
85+
rollupOptions: {
86+
input: {
87+
index: 'index.html'
88+
}
89+
}
90+
}
91+
}, 'build', 'production')
92+
93+
expect(config.build.rollupOptions.input['index']).toBe(resolve('index.html'))
94+
})
95+
96+
test('resolve build.rollupOptions.input[]', async () => {
97+
const config = await resolveConfig({
98+
build: {
99+
rollupOptions: {
100+
input: ['index.html']
101+
}
102+
}
103+
}, 'build', 'production')
104+
105+
expect(config.build.rollupOptions.input).toStrictEqual([resolve('index.html')])
106+
})
107+
108+
test('resolve index.html', async () => {
109+
const config = await resolveConfig({}, 'build', 'production')
110+
111+
expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
112+
})
113+
114+
test('resolve build.outdir', async () => {
115+
const config = await resolveConfig({ build: { outDir: 'outDir' } }, 'build', 'production')
116+
117+
expect(config.build.outDir).toBe(resolve('outDir'))
118+
})
119+
120+
test('resolve default build.outdir', async () => {
121+
const config = await resolveConfig({}, 'build', 'production')
122+
123+
expect(config.build.outDir).toBe(resolve('dist'))
124+
})
125+
126+
test('resolve build.lib.entry', async () => {
127+
const config = await resolveConfig({ build: { lib: { entry: 'index.html' } } }, 'build', 'production')
128+
129+
expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
130+
})
131+
132+
test('resolve build.ssr', async () => {
133+
const config = await resolveConfig({ build: { ssr: 'ssr.ts' } }, 'build', 'production')
134+
135+
expect(config.build.rollupOptions.input).toBe(resolve('ssr.ts'))
136+
})
137+
})

packages/vite/src/node/build.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export type ResolvedBuildOptions = Required<
233233
>
234234
>
235235

236-
export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
236+
export function resolveBuildOptions(root: string, raw?: BuildOptions): ResolvedBuildOptions {
237237
const resolved: ResolvedBuildOptions = {
238238
target: 'modules',
239239
polyfillModulePreload: true,
@@ -269,6 +269,40 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
269269
}
270270
}
271271

272+
const resolve = (p: string) => p.startsWith('\0') ? p : path.resolve(root, p)
273+
274+
resolved.outDir = resolve(resolved.outDir)
275+
276+
let input
277+
278+
if (raw?.rollupOptions?.input) {
279+
input = Array.isArray(raw.rollupOptions.input)
280+
? raw.rollupOptions.input.map(input => resolve(input))
281+
: typeof raw.rollupOptions.input === 'object'
282+
? Object.assign(
283+
// @ts-ignore
284+
...Object.keys(raw.rollupOptions.input).map(key => ({ [key]: resolve(raw.rollupOptions.input[key]) }))
285+
)
286+
: resolve(raw.rollupOptions.input)
287+
} else {
288+
input = resolve(
289+
raw?.lib
290+
? raw.lib.entry
291+
: typeof raw?.ssr === 'string'
292+
? raw.ssr
293+
: 'index.html'
294+
)
295+
}
296+
297+
if (!!raw?.ssr && typeof input === 'string' && input.endsWith('.html')) {
298+
throw new Error(
299+
`rollupOptions.input should not be an html file when building for SSR. ` +
300+
`Please specify a dedicated SSR entry.`
301+
)
302+
}
303+
304+
resolved.rollupOptions.input = input
305+
272306
// handle special build targets
273307
if (resolved.target === 'modules') {
274308
// Support browserslist
@@ -362,6 +396,8 @@ async function doBuild(
362396
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
363397
const config = await resolveConfig(inlineConfig, 'build', 'production')
364398
const options = config.build
399+
const input = options.rollupOptions.input
400+
const outDir = options.outDir
365401
const ssr = !!options.ssr
366402
const libOptions = options.lib
367403

@@ -373,22 +409,6 @@ async function doBuild(
373409
)
374410
)
375411

376-
const resolve = (p: string) => path.resolve(config.root, p)
377-
const input = libOptions
378-
? resolve(libOptions.entry)
379-
: typeof options.ssr === 'string'
380-
? resolve(options.ssr)
381-
: options.rollupOptions?.input || resolve('index.html')
382-
383-
if (ssr && typeof input === 'string' && input.endsWith('.html')) {
384-
throw new Error(
385-
`rollupOptions.input should not be an html file when building for SSR. ` +
386-
`Please specify a dedicated SSR entry.`
387-
)
388-
}
389-
390-
const outDir = resolve(options.outDir)
391-
392412
// inject ssr arg to plugin load/transform hooks
393413
const plugins = (
394414
ssr ? config.plugins.map((p) => injectSsrFlagToHooks(p)) : config.plugins
@@ -421,7 +441,6 @@ async function doBuild(
421441

422442
const rollup = require('rollup') as typeof Rollup
423443
const rollupOptions: RollupOptions = {
424-
input,
425444
context: 'globalThis',
426445
preserveEntrySignatures: ssr
427446
? 'allow-extension'

packages/vite/src/node/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export async function resolveConfig(
364364

365365
// resolve public base url
366366
const BASE_URL = resolveBaseUrl(config.base, command === 'build', logger)
367-
const resolvedBuildOptions = resolveBuildOptions(config.build)
367+
const resolvedBuildOptions = resolveBuildOptions(resolvedRoot, config.build)
368368

369369
// resolve cache directory
370370
const pkgPath = lookupFile(

0 commit comments

Comments
 (0)