Skip to content

Commit 9beae37

Browse files
authored
feat: add input option (#22642)
1 parent 78accc4 commit 9beae37

13 files changed

Lines changed: 549 additions & 23 deletions

File tree

‎docs/config/build-options.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ Note that this option requires [`import.meta.resolve` support](https://caniuse.c
164164

165165
Directly customize the underlying Rolldown bundle. This is the same as options that can be exported from a Rolldown config file and will be merged with Vite's internal Rolldown options. See [Rolldown options docs](https://rolldown.rs/reference/) for more details.
166166

167+
Instead of `build.rolldownOptions.input`, it is recommended to set the top-level [`input`](/config/shared-options#input) option, because it will be used in dev as well. If `build.rolldownOptions.input` is set, it overrides the top-level `input` option for build only.
168+
167169
## build.rollupOptions
168170

169171
- **Type:** `RolldownOptions`
@@ -180,10 +182,10 @@ Whether to transform dynamic imports with variables.
180182

181183
## build.lib
182184

183-
- **Type:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string), cssFileName?: string }`
185+
- **Type:** `{ entry?: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string), cssFileName?: string }`
184186
- **Related:** [Library Mode](/guide/build#library-mode)
185187

186-
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used.
188+
Build as a library. `entry` defaults to the top-level [`input`](/config/shared-options#input) option, and one of them is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used.
187189

188190
`fileName` is the name of the package file output, which defaults to the `"name"` in `package.json`. It can also be defined as a function taking the `format` and `entryName` as arguments, and returning the file name.
189191

@@ -280,7 +282,7 @@ When the value is a string, it will be used as the manifest file path relative t
280282
- **Default:** `false`
281283
- **Related:** [Server-Side Rendering](/guide/ssr)
282284

283-
Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or `true`, which requires specifying the SSR entry via `rolldownOptions.input`.
285+
Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or `true`, which requires specifying the SSR entry via [`input`](/config/shared-options#input) or `build.rolldownOptions.input`.
284286

285287
## build.emitAssets
286288

‎docs/config/dep-optimization-options.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Unless noted, the options in this section are only applied to the dependency opt
88

99
- **Type:** `string | string[]`
1010

11-
By default, Vite will crawl all your `.html` files to detect dependencies that need to be pre-bundled (ignoring `node_modules`, `build.outDir`, `__tests__` and `coverage`). If `build.rolldownOptions.input` is specified, Vite will crawl those entry points instead.
11+
By default, Vite will crawl all your `.html` files to detect dependencies that need to be pre-bundled (ignoring `node_modules`, `build.outDir`, `__tests__` and `coverage`). If the top-level [`input`](/config/shared-options#input) or `build.rolldownOptions.input` is specified, Vite will crawl those entry points instead.
1212

1313
If neither of these fit your needs, you can specify custom entries using this option - the value should be a [`tinyglobby` pattern](https://superchupu.dev/tinyglobby/comparison) or array of patterns that are relative from Vite project root. This will overwrite default entries inference. Only `node_modules` and `build.outDir` folders will be ignored by default when `optimizeDeps.entries` is explicitly defined. If other folders need to be ignored, you can use an ignore pattern as part of the entries list, marked with an initial `!`. `node_modules` will not be ignored for patterns that explicitly include the string `node_modules`.
1414

‎docs/config/shared-options.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ Specifying this in config will override the default mode for **both serve and bu
3434

3535
See [Env Variables and Modes](/guide/env-and-mode) for more details.
3636

37+
## input <NonInheritBadge />
38+
39+
- **Type:** `string | string[] | { [entryAlias: string]: string }`
40+
41+
Entry points of your application, resolved relative to the project root. This works as the default value for [`build.rolldownOptions.input`](/config/build-options#build-rolldownoptions), [`build.lib.entry`](/config/build-options#build-lib), [`build.ssr`](/config/build-options#build-ssr) (if `true`), and [`optimizeDeps.entries`](/config/dep-optimization-options#optimizedeps-entries) when those are not set explicitly.
42+
43+
This is useful when your application does not use an `index.html` entry, so you only need to declare the entry once instead of repeating it across the options above.
44+
45+
```js twoslash [vite.config.js]
46+
import { defineConfig } from 'vite'
47+
48+
export default defineConfig({
49+
input: 'src/main.ts',
50+
})
51+
```
52+
3753
## define
3854

3955
- **Type:** `Record<string, any>`

‎docs/guide/backend-integration.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ If you need a custom integration, you can follow the steps in this guide to conf
1212
import { defineConfig } from 'vite'
1313
// ---cut---
1414
export default defineConfig({
15+
// overwrite default .html entry
16+
input: '/path/to/main.js',
1517
server: {
1618
cors: {
1719
// the origin you will be accessing via browser
@@ -21,10 +23,6 @@ If you need a custom integration, you can follow the steps in this guide to conf
2123
build: {
2224
// generate .vite/manifest.json in outDir
2325
manifest: true,
24-
rolldownOptions: {
25-
// overwrite default .html entry
26-
input: '/path/to/main.js',
27-
},
2826
},
2927
})
3028
```

‎docs/guide/build.md‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,9 @@ import { dirname, resolve } from 'node:path'
121121
import { defineConfig } from 'vite'
122122

123123
export default defineConfig({
124-
build: {
125-
rolldownOptions: {
126-
input: {
127-
main: resolve(import.meta.dirname, 'index.html'),
128-
nested: resolve(import.meta.dirname, 'nested/index.html'),
129-
},
130-
},
124+
input: {
125+
main: resolve(import.meta.dirname, 'index.html'),
126+
nested: resolve(import.meta.dirname, 'nested/index.html'),
131127
},
132128
})
133129
```

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import type {
1313
} from 'rolldown'
1414
import type { LibraryFormats, LibraryOptions } from '../build'
1515
import {
16+
ChunkMetadataMap,
1617
build,
1718
createBuilder,
1819
onRollupLog,
1920
resolveBuildOutputs,
2021
resolveLibFilename,
22+
resolveRolldownOptions,
2123
} from '../build'
2224
import type { Logger } from '../logger'
2325
import { createLogger } from '../logger'
@@ -81,6 +83,35 @@ describe('build', () => {
8183
assertOutputHashContentChange(result[0], result[1])
8284
})
8385

86+
test('top-level input is used as the default build entry', async () => {
87+
const result = (await build({
88+
root: resolve(dirname, 'packages/build-project'),
89+
logLevel: 'silent',
90+
input: 'top-level-entry.js',
91+
build: {
92+
write: false,
93+
},
94+
plugins: [
95+
{
96+
name: 'test',
97+
resolveId(id) {
98+
if (id.replace(/\\/g, '/').endsWith('top-level-entry.js')) {
99+
return '\0top-level-entry.js'
100+
}
101+
},
102+
load(id) {
103+
if (id === '\0top-level-entry.js') {
104+
return `console.log('from-top-level-input')`
105+
}
106+
},
107+
},
108+
],
109+
})) as RolldownOutput
110+
const chunk = result.output.find((o) => o.type === 'chunk')
111+
expect(chunk?.fileName).toContain('top-level-entry')
112+
expect(chunk?.code).toContain('from-top-level-input')
113+
})
114+
84115
test('file hash should change when pure css chunk changes', async () => {
85116
const buildProject = async (cssColor: string) => {
86117
return (await build({
@@ -378,6 +409,74 @@ describe('resolveBuildOutputs', () => {
378409
/Entries in "build\.rolldownOptions\.output" must specify "name"/,
379410
)
380411
})
412+
413+
describe('input resolution in resolveRolldownOptions', () => {
414+
const buildProjectRoot = resolve(dirname, 'packages/build-project')
415+
416+
test('build.rolldownOptions.input takes precedence over top-level input', async () => {
417+
const builder = await createBuilder({
418+
root: buildProjectRoot,
419+
logLevel: 'silent',
420+
input: 'top-level-entry.js',
421+
build: {
422+
rolldownOptions: { input: 'explicit-entry.js' },
423+
},
424+
})
425+
const options = resolveRolldownOptions(
426+
builder.environments.client,
427+
new ChunkMetadataMap(),
428+
)
429+
expect(options.input).toBe('explicit-entry.js')
430+
})
431+
432+
test('falls back to index.html when no input is set', async () => {
433+
const builder = await createBuilder({
434+
root: buildProjectRoot,
435+
logLevel: 'silent',
436+
})
437+
const options = resolveRolldownOptions(
438+
builder.environments.client,
439+
new ChunkMetadataMap(),
440+
)
441+
expect(options.input).toBe(resolve(buildProjectRoot, 'index.html'))
442+
})
443+
444+
test('build.ssr string entry takes precedence over input', async () => {
445+
const builder = await createBuilder({
446+
root: buildProjectRoot,
447+
logLevel: 'silent',
448+
environments: {
449+
ssr: {
450+
input: 'env-input.js',
451+
build: { ssr: 'ssr-entry.js' },
452+
},
453+
},
454+
})
455+
const options = resolveRolldownOptions(
456+
builder.environments.ssr,
457+
new ChunkMetadataMap(),
458+
)
459+
expect(options.input).toBe(resolve(buildProjectRoot, 'ssr-entry.js'))
460+
})
461+
462+
test('throws when build.lib is set without entry or top-level input', async () => {
463+
const builder = await createBuilder({
464+
root: buildProjectRoot,
465+
logLevel: 'silent',
466+
build: {
467+
lib: { name: 'MyLib', formats: ['es'] },
468+
},
469+
})
470+
expect(() =>
471+
resolveRolldownOptions(
472+
builder.environments.client,
473+
new ChunkMetadataMap(),
474+
),
475+
).toThrow(
476+
/Either "build\.lib\.entry" or the top-level "input" option is required/,
477+
)
478+
})
479+
})
381480
})
382481

383482
describe('resolveLibFilename', () => {

0 commit comments

Comments
 (0)