Skip to content

Commit d002e7d

Browse files
sapphi-redbluwypatak-dev
authored
feat(resolve)!: allow removing conditions (#18395)
Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: patak <[email protected]>
1 parent 887ce8b commit d002e7d

File tree

23 files changed

+158
-197
lines changed

23 files changed

+158
-197
lines changed

docs/config/shared-options.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ For SSR builds, deduplication does not work for ESM build outputs configured fro
117117
## resolve.conditions
118118
119119
- **Type:** `string[]`
120+
- **Default:** `['module', 'browser', 'development|production']`
120121
121122
Additional allowed conditions when resolving [Conditional Exports](https://nodejs.org/api/packages.html#packages_conditional_exports) from a package.
122123
@@ -135,7 +136,9 @@ A package with conditional exports may have the following `exports` field in its
135136

136137
Here, `import` and `require` are "conditions". Conditions can be nested and should be specified from most specific to least specific.
137138

138-
Vite has a list of "allowed conditions" and will match the first condition that is in the allowed list. The default allowed conditions are: `import`, `module`, `browser`, `default`, and `production/development` based on current mode. The `resolve.conditions` config option allows specifying additional allowed conditions.
139+
`development|production` is a special value that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`. It is replaced with `production` when `process.env.NODE_ENV === 'production'` and `development` otherwise.
140+
141+
Note that `import`, `require`, `default` conditions are always applied if the requirements are met.
139142

140143
:::warning Resolving subpath exports
141144
Export keys ending with "/" is deprecated by Node and may not work well. Please contact the package author to use [`*` subpath patterns](https://nodejs.org/api/packages.html#package-entry-points) instead.

docs/config/ssr-options.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ Build target for the SSR server.
3434
## ssr.resolve.conditions
3535

3636
- **Type:** `string[]`
37+
- **Default:** `['module', 'node', 'development|production']` (`['module', 'browser', 'development|production']` for `ssr.target === 'webworker'`)
3738
- **Related:** [Resolve Conditions](./shared-options.md#resolve-conditions)
3839

39-
Defaults to the root [`resolve.conditions`](./shared-options.md#resolve-conditions).
40-
4140
These conditions are used in the plugin pipeline, and only affect non-externalized dependencies during the SSR build. Use `ssr.resolve.externalConditions` to affect externalized imports.
4241

4342
## ssr.resolve.externalConditions
4443

4544
- **Type:** `string[]`
46-
- **Default:** `[]`
45+
- **Default:** `['node']`
4746

4847
Conditions that are used during ssr import (including `ssrLoadModule`) of externalized dependencies.

docs/guide/migration.md

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ The experimental Vite Runtime API evolved into the Module Runner API, released i
1212

1313
## General Changes
1414

15+
### Default value for `resolve.conditions`
16+
17+
This change does not affect users that did not configure [`resolve.conditions`](/config/shared-options#resolve-conditions) / [`ssr.resolve.conditions`](/config/ssr-options#ssr-resolve-conditions) / [`ssr.resolve.externalConditions`](/config/ssr-options#ssr-resolve-externalconditions).
18+
19+
In Vite 5, the default value for `resolve.conditions` was `[]` and some conditions were added internally. The default value for `ssr.resolve.conditions` was the value of `resolve.conditions`.
20+
21+
From Vite 6, some of the conditions are no longer added internally and need to be included in the config values.
22+
The conditions that are no longer added internally for
23+
24+
- `resolve.conditions` are `['module', 'browser', 'development|production']`
25+
- `ssr.resolve.conditions` are `['module', 'node', 'development|production']`
26+
27+
The default values for those options are updated to the corresponding values and `ssr.resolve.conditions` no longer uses `resolve.conditions` as the default value. Note that `development|production` is a special variable that is replaced with `production` or `development` depending on the value of `process.env.NODE_ENV`.
28+
29+
If you specified a custom value for `resolve.conditions` or `ssr.resolve.conditions`, you need to update it to include the new conditions.
30+
For example, if you previously specified `['custom']` for `resolve.conditions`, you need to specify `['custom', 'module', 'browser', 'development|production']` instead.
31+
1532
### JSON stringify
1633

1734
In Vite 5, when [`json.stringify: true`](/config/shared-options#json-stringify) is set, [`json.namedExports`](/config/shared-options#json-namedexports) was disabled.

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

+14-80
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('custom environment conditions', () => {
2121
ws: false,
2222
},
2323
environments: {
24-
// no web / default
24+
// default
2525
ssr: {
2626
resolve: {
2727
noExternal,
@@ -36,9 +36,8 @@ describe('custom environment conditions', () => {
3636
},
3737
},
3838
},
39-
// web / worker
39+
// worker
4040
worker: {
41-
webCompatible: true,
4241
resolve: {
4342
noExternal,
4443
conditions: ['worker'],
@@ -54,9 +53,8 @@ describe('custom environment conditions', () => {
5453
},
5554
},
5655
},
57-
// web / custom1
56+
// custom1
5857
custom1: {
59-
webCompatible: true,
6058
resolve: {
6159
noExternal,
6260
conditions: ['custom1'],
@@ -72,54 +70,17 @@ describe('custom environment conditions', () => {
7270
},
7371
},
7472
},
75-
// no web / custom2
76-
custom2: {
77-
webCompatible: false,
73+
// same as custom1
74+
custom1_2: {
7875
resolve: {
7976
noExternal,
80-
conditions: ['custom2'],
81-
externalConditions: ['custom2'],
82-
},
83-
build: {
84-
outDir: path.join(
85-
import.meta.dirname,
86-
'fixtures/test-dep-conditions/dist/custom2',
87-
),
88-
rollupOptions: {
89-
input: { index: '@vitejs/test-dep-conditions' },
90-
},
91-
},
92-
},
93-
// no web / custom3
94-
custom3: {
95-
webCompatible: false,
96-
resolve: {
97-
noExternal,
98-
conditions: ['custom3'],
99-
externalConditions: ['custom3'],
100-
},
101-
build: {
102-
outDir: path.join(
103-
import.meta.dirname,
104-
'fixtures/test-dep-conditions/dist/custom3',
105-
),
106-
rollupOptions: {
107-
input: { index: '@vitejs/test-dep-conditions' },
108-
},
109-
},
110-
},
111-
// same as custom3
112-
custom3_2: {
113-
webCompatible: false,
114-
resolve: {
115-
noExternal,
116-
conditions: ['custom3'],
117-
externalConditions: ['custom3'],
77+
conditions: ['custom1'],
78+
externalConditions: ['custom1'],
11879
},
11980
build: {
12081
outDir: path.join(
12182
import.meta.dirname,
122-
'fixtures/test-dep-conditions/dist/custom3_2',
83+
'fixtures/test-dep-conditions/dist/custom1_2',
12384
),
12485
rollupOptions: {
12586
input: { index: '@vitejs/test-dep-conditions' },
@@ -135,14 +96,7 @@ describe('custom environment conditions', () => {
13596
onTestFinished(() => server.close())
13697

13798
const results: Record<string, unknown> = {}
138-
for (const key of [
139-
'ssr',
140-
'worker',
141-
'custom1',
142-
'custom2',
143-
'custom3',
144-
'custom3_2',
145-
]) {
99+
for (const key of ['ssr', 'worker', 'custom1', 'custom1_2']) {
146100
const runner = createServerModuleRunner(server.environments[key], {
147101
hmr: {
148102
logger: false,
@@ -155,9 +109,7 @@ describe('custom environment conditions', () => {
155109
expect(results).toMatchInlineSnapshot(`
156110
{
157111
"custom1": "index.custom1.js",
158-
"custom2": "index.custom2.js",
159-
"custom3": "index.custom3.js",
160-
"custom3_2": "index.custom3.js",
112+
"custom1_2": "index.custom1.js",
161113
"ssr": "index.default.js",
162114
"worker": "index.worker.js",
163115
}
@@ -169,14 +121,7 @@ describe('custom environment conditions', () => {
169121
onTestFinished(() => server.close())
170122

171123
const results: Record<string, unknown> = {}
172-
for (const key of [
173-
'ssr',
174-
'worker',
175-
'custom1',
176-
'custom2',
177-
'custom3',
178-
'custom3_2',
179-
]) {
124+
for (const key of ['ssr', 'worker', 'custom1', 'custom1_2']) {
180125
const runner = createServerModuleRunner(server.environments[key], {
181126
hmr: {
182127
logger: false,
@@ -191,9 +136,7 @@ describe('custom environment conditions', () => {
191136
expect(results).toMatchInlineSnapshot(`
192137
{
193138
"custom1": "index.custom1.js",
194-
"custom2": "index.custom2.js",
195-
"custom3": "index.custom3.js",
196-
"custom3_2": "index.custom3.js",
139+
"custom1_2": "index.custom1.js",
197140
"ssr": "index.default.js",
198141
"worker": "index.worker.js",
199142
}
@@ -222,14 +165,7 @@ describe('custom environment conditions', () => {
222165
test('build', async () => {
223166
const builder = await createBuilder(getConfig({ noExternal: true }))
224167
const results: Record<string, unknown> = {}
225-
for (const key of [
226-
'ssr',
227-
'worker',
228-
'custom1',
229-
'custom2',
230-
'custom3',
231-
'custom3_2',
232-
]) {
168+
for (const key of ['ssr', 'worker', 'custom1', 'custom1_2']) {
233169
const output = await builder.build(builder.environments[key])
234170
const chunk = (output as RollupOutput).output[0]
235171
const mod = await import(
@@ -245,9 +181,7 @@ describe('custom environment conditions', () => {
245181
expect(results).toMatchInlineSnapshot(`
246182
{
247183
"custom1": "index.custom1.js",
248-
"custom2": "index.custom2.js",
249-
"custom3": "index.custom3.js",
250-
"custom3_2": "index.custom3.js",
184+
"custom1_2": "index.custom1.js",
251185
"ssr": "index.default.js",
252186
"worker": "index.worker.js",
253187
}

packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.custom2.js

-1
This file was deleted.

packages/vite/src/node/__tests__/fixtures/test-dep-conditions/index.custom3.js

-1
This file was deleted.

packages/vite/src/node/__tests__/fixtures/test-dep-conditions/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
".": {
77
"style": "./index.css",
88
"custom1": "./index.custom1.js",
9-
"custom2": "./index.custom2.js",
10-
"custom3": "./index.custom3.js",
119
"worker": "./index.worker.js",
1210
"browser": "./index.browser.js",
1311
"default": "./index.default.js"

packages/vite/src/node/baseEnvironment.ts

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export function getDefaultResolvedEnvironmentOptions(
1717
define: config.define,
1818
resolve: config.resolve,
1919
consumer: 'server',
20-
webCompatible: false,
2120
optimizeDeps: config.optimizeDeps,
2221
dev: config.dev,
2322
build: config.build,

packages/vite/src/node/build.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ export function resolveBuildEnvironmentOptions(
348348
raw: BuildEnvironmentOptions,
349349
logger: Logger,
350350
consumer: 'client' | 'server' | undefined,
351+
// Backward compatibility
352+
isSsrTargetWebworkerEnvironment?: boolean,
351353
): ResolvedBuildEnvironmentOptions {
352354
const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload
353355
const { polyfillModulePreload, ...rest } = raw
@@ -444,6 +446,19 @@ export function resolveBuildEnvironmentOptions(
444446
resolved.cssMinify = consumer === 'server' ? 'esbuild' : !!resolved.minify
445447
}
446448

449+
if (isSsrTargetWebworkerEnvironment) {
450+
resolved.rollupOptions ??= {}
451+
resolved.rollupOptions.output ??= {}
452+
const output = resolved.rollupOptions.output
453+
for (const out of arraify(output)) {
454+
out.entryFileNames ??= `[name].js`
455+
out.chunkFileNames ??= `[name]-[hash].js`
456+
const input = resolved.rollupOptions.input
457+
out.inlineDynamicImports ??=
458+
!input || typeof input === 'string' || Object.keys(input).length === 1
459+
}
460+
}
461+
447462
return resolved
448463
}
449464

@@ -692,7 +707,7 @@ async function buildEnvironment(
692707

693708
const format = output.format || 'es'
694709
const jsExt =
695-
!environment.config.webCompatible || libOptions
710+
environment.config.consumer === 'server' || libOptions
696711
? resolveOutputJsExtension(
697712
format,
698713
findNearestPackageData(root, packageCache)?.data.type,
@@ -730,11 +745,7 @@ async function buildEnvironment(
730745
? `[name].[ext]`
731746
: path.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
732747
inlineDynamicImports:
733-
output.format === 'umd' ||
734-
output.format === 'iife' ||
735-
(environment.config.consumer === 'server' &&
736-
environment.config.webCompatible &&
737-
(typeof input === 'string' || Object.keys(input).length === 1)),
748+
output.format === 'umd' || output.format === 'iife',
738749
...output,
739750
}
740751
}

0 commit comments

Comments
 (0)