From 5b6b01693720290e8998b2613f0dcb2d699ee84f Mon Sep 17 00:00:00 2001 From: Nurettin Kaya Date: Thu, 11 Nov 2021 05:39:59 -0800 Subject: [PATCH] fix(build): resolve `rollupOptions.input` paths (#5601) --- packages/plugin-legacy/index.js | 2 +- .../vite/src/node/__tests__/build.spec.ts | 70 +++++++++++++++++++ packages/vite/src/node/build.ts | 55 ++++++++++----- packages/vite/src/node/config.ts | 2 +- 4 files changed, 109 insertions(+), 20 deletions(-) diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index bfa6ee491bcbf6..2267cd9cf82b42 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -596,7 +596,7 @@ async function buildPolyfillChunk( bundle[polyfillChunk.name] = polyfillChunk } -const polyfillId = 'vite/legacy-polyfills' +const polyfillId = '\0vite/legacy-polyfills' /** * @param {Set} imports diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index b3ef37e64fd28e..b745c38aa44790 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -1,5 +1,6 @@ import { resolveLibFilename } from '../build' import { resolve } from 'path' +import { resolveConfig } from '..' describe('resolveLibFilename', () => { test('custom filename function', () => { @@ -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')) + }) +}) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index b9ccf666b53550..a374bc9908f353 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -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, @@ -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 @@ -362,6 +396,8 @@ async function doBuild( ): Promise { 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 @@ -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 @@ -421,7 +441,6 @@ async function doBuild( const rollup = require('rollup') as typeof Rollup const rollupOptions: RollupOptions = { - input, context: 'globalThis', preserveEntrySignatures: ssr ? 'allow-extension' diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 51a9ffac7f3bb6..c627fc5c502886 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -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(