Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): resolve rollupOptions.input paths #5601

Merged
merged 11 commits into from
Nov 11, 2021
65 changes: 64 additions & 1 deletion packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolveLibFilename } from '../build'
import { resolveLibFilename, resolvePaths } from '../build'
import { resolve } from 'path'
import { resolveConfig } from '..'

describe('resolveLibFilename', () => {
test('custom filename function', () => {
Expand Down Expand Up @@ -65,3 +66,65 @@ describe('resolveLibFilename', () => {
}).toThrow()
})
})

describe('resolvePaths', () => {
test('resolve build.rollupOptions.input', async () => {
const config = await resolveConfig({
build: {
rollupOptions: {
input: 'packages/noname/index.html'
}
}
}, 'build', 'production')
const { input } = resolvePaths(config, config.build)

expect(input).toBe(resolve('packages/noname/index.html'))
})

test('resolve build.rollupOptions.input[]', async () => {
const config = await resolveConfig({
root: 'packages/noname',
build: {
rollupOptions: {
input: ['index.html']
}
}
}, 'build', 'production')
const { input } = resolvePaths(config, config.build)

const resolved = resolve('packages/noname/index.html')

expect(input).toStrictEqual([resolved])
expect(config.build.rollupOptions.input).toStrictEqual([resolved])
})

test('resolve index.html', async () => {
const config = await resolveConfig({
root: 'packages/noname',
}, 'build', 'production')
const { input } = resolvePaths(config, config.build)

expect(input).toBe(resolve('packages/noname/index.html'))
})

test('resolve build.outdir', async () => {
const config = await resolveConfig({ build: { outDir: 'packages/noname' } }, 'build', 'production')
const { outDir } = resolvePaths(config, config.build)

expect(outDir).toBe(resolve('packages/noname'))
})

test('resolve build.lib.entry', async () => {
const config = await resolveConfig({ build: { lib: { entry: 'packages/noname/index.html' } } }, 'build', 'production')
const { input } = resolvePaths(config, config.build)

expect(input).toBe(resolve('packages/noname/index.html'))
})

test('resolve build.ssr', async () => {
const config = await resolveConfig({ build: { ssr: 'packages/noname/ssr.ts' } }, 'build', 'production')
const { input } = resolvePaths(config, config.build)

expect(input).toBe(resolve('packages/noname/ssr.ts'))
})
})
Empty file.
Empty file.
45 changes: 30 additions & 15 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,35 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
}
}

export function resolvePaths(config: ResolvedConfig, options: ResolvedConfig["build"]) {
const rollupOptions = options.rollupOptions
const libOptions = options.lib
const ssr = !!options.ssr

const resolve = (p: string) => path.resolve(config.root, p)

if (Array.isArray(rollupOptions?.input))
rollupOptions.input = rollupOptions.input.map(input => resolve(input))
sibbng marked this conversation as resolved.
Show resolved Hide resolved

const input = libOptions
? libOptions.entry
: typeof options.ssr === 'string'
? options.ssr
: rollupOptions?.input || 'index.html'

if (ssr && typeof input === 'string' && input.endsWith('.html')) {
throw new Error(
`rollupOptions.input should not be an html file when building for SSR. ` +
`Please specify a dedicated SSR entry.`
)
}

return {
input: typeof input === 'string' ? resolve(input) : input,
outDir: resolve(options.outDir)
}
}

/**
* Track parallel build calls and only stop the esbuild service when all
* builds are done. (#1098)
Expand Down Expand Up @@ -373,21 +402,7 @@ async function doBuild(
)
)

const resolve = (p: string) => path.resolve(config.root, p)
const input = libOptions
? resolve(libOptions.entry)
: typeof options.ssr === 'string'
? resolve(options.ssr)
: options.rollupOptions?.input || resolve('index.html')

if (ssr && typeof input === 'string' && input.endsWith('.html')) {
throw new Error(
`rollupOptions.input should not be an html file when building for SSR. ` +
`Please specify a dedicated SSR entry.`
)
}

const outDir = resolve(options.outDir)
const { input, outDir } = resolvePaths(config, options)

// inject ssr arg to plugin load/transform hooks
const plugins = (
Expand Down