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
2 changes: 1 addition & 1 deletion packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ async function buildPolyfillChunk(
bundle[polyfillChunk.name] = polyfillChunk
}

const polyfillId = 'vite/legacy-polyfills'
const polyfillId = '\0vite/legacy-polyfills'

/**
* @param {Set<string>} imports
Expand Down
70 changes: 70 additions & 0 deletions 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 { resolve } from 'path'
import { resolveConfig } from '..'

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

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

expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
})
test('resolve build.rollupOptions.input{}', async () => {
const config = await resolveConfig({
build: {
rollupOptions: {
input: {
index: 'index.html'
}
}
}
}, 'build', 'production')

expect(config.build.rollupOptions.input['index']).toBe(resolve('index.html'))
})

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

expect(config.build.rollupOptions.input).toStrictEqual([resolve('index.html')])
})

test('resolve index.html', async () => {
const config = await resolveConfig({}, 'build', 'production')

expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
})

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

expect(config.build.outDir).toBe(resolve('outDir'))
})

test('resolve default build.outdir', async () => {
const config = await resolveConfig({}, 'build', 'production')

expect(config.build.outDir).toBe(resolve('dist'))
})

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

expect(config.build.rollupOptions.input).toBe(resolve('index.html'))
})

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

expect(config.build.rollupOptions.input).toBe(resolve('ssr.ts'))
})
})
55 changes: 37 additions & 18 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export type ResolvedBuildOptions = Required<
>
>

export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
export function resolveBuildOptions(root: string, raw?: BuildOptions): ResolvedBuildOptions {
const resolved: ResolvedBuildOptions = {
target: 'modules',
polyfillModulePreload: true,
Expand Down Expand Up @@ -269,6 +269,40 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
}
}

const resolve = (p: string) => p.startsWith('\0') ? p : path.resolve(root, p)

resolved.outDir = resolve(resolved.outDir)

let input

if (raw?.rollupOptions?.input) {
input = Array.isArray(raw.rollupOptions.input)
? raw.rollupOptions.input.map(input => resolve(input))
: typeof raw.rollupOptions.input === 'object'
? Object.assign(
// @ts-ignore
...Object.keys(raw.rollupOptions.input).map(key => ({ [key]: resolve(raw.rollupOptions.input[key]) }))
)
: resolve(raw.rollupOptions.input)
} else {
input = resolve(
raw?.lib
? raw.lib.entry
: typeof raw?.ssr === 'string'
? raw.ssr
: 'index.html'
)
}

if (!!raw?.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.`
)
}

resolved.rollupOptions.input = input

// handle special build targets
if (resolved.target === 'modules') {
// Support browserslist
Expand Down Expand Up @@ -362,6 +396,8 @@ async function doBuild(
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
const config = await resolveConfig(inlineConfig, 'build', 'production')
const options = config.build
const input = options.rollupOptions.input
const outDir = options.outDir
const ssr = !!options.ssr
const libOptions = options.lib

Expand All @@ -373,22 +409,6 @@ 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)

// inject ssr arg to plugin load/transform hooks
const plugins = (
ssr ? config.plugins.map((p) => injectSsrFlagToHooks(p)) : config.plugins
Expand Down Expand Up @@ -421,7 +441,6 @@ async function doBuild(

const rollup = require('rollup') as typeof Rollup
const rollupOptions: RollupOptions = {
input,
context: 'globalThis',
preserveEntrySignatures: ssr
? 'allow-extension'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export async function resolveConfig(

// resolve public base url
const BASE_URL = resolveBaseUrl(config.base, command === 'build', logger)
const resolvedBuildOptions = resolveBuildOptions(config.build)
const resolvedBuildOptions = resolveBuildOptions(resolvedRoot, config.build)

// resolve cache directory
const pkgPath = lookupFile(
Expand Down