Skip to content

Commit 1699ab8

Browse files
committed
chore: merge main
2 parents 383a060 + 33d6177 commit 1699ab8

File tree

15 files changed

+107
-73
lines changed

15 files changed

+107
-73
lines changed

docs/guide/migration.md

+9-14
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,12 @@ A small fraction of users will now require using [@vitejs/plugin-legacy](https:/
3131

3232
This section describes the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, legacy options have been added to revert to the Vite v2 strategies.
3333

34-
:::warning
35-
These options are marked as experimental and deprecated. They may be removed in a future v3 minor without respecting semver. Please pin the Vite version when using them.
36-
37-
- `legacy.buildRollupPluginCommonjs`
38-
- `legacy.buildSsrCjsExternalHeuristics`
39-
40-
:::
41-
4234
### Dev Server Changes
4335

4436
Vite's default dev server port is now 5173. You can use [`server.port`](../config/server-options.md#server-port) to set it to 3000.
4537

4638
Vite's default dev server host is now `localhost`. You can use [`server.host`](../config/server-options.md#server-host) to set it to `127.0.0.1`.
4739

48-
### Build Changes
49-
50-
In v3, Vite uses esbuild to optimize dependencies by default. Doing so, it removes one of the most significant differences between dev and prod present in v2. Because esbuild converts CJS-only dependencies to ESM, [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is no longer used.
51-
52-
If you need to get back to the v2 strategy, you can use `legacy.buildRollupPluginCommonjs`.
53-
5440
### SSR Changes
5541

5642
Vite v3 uses ESM for the SSR build by default. When using ESM, the [SSR externalization heuristics](https://vitejs.dev/guide/ssr.html#ssr-externals) are no longer needed. By default, all dependencies are externalized. You can use [`ssr.noExternal`](../config/ssr-options.md#ssr-noexternal) to control what dependencies to include in the SSR bundle.
@@ -114,6 +100,15 @@ export default {
114100
}
115101
```
116102

103+
## Experimental
104+
105+
### Using esbuild deps optimization at build time
106+
107+
In v3, Vite allows the use of esbuild to optimize dependencies during build time. If enabled, it removes one of the most significant differences between dev and prod present in v2. [`@rollupjs/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs) is no longer needed in this case since esbuild converts CJS-only dependencies to ESM.
108+
109+
If you want to try this build strategy, you can use `optimizeDeps.disabled: false` (the default in v3 is `disabled: 'build'`). `@rollup/plugin-commonjs`
110+
can be removed by passing `build.commonjsOptions: { include: [] }`
111+
117112
## Advanced
118113

119114
There are some changes which only affects plugin/tool creators.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test": "run-s test-unit test-serve test-build",
2222
"test-serve": "vitest run -c vitest.config.e2e.ts",
2323
"test-build": "cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
24-
"test-build-legacy-cjs": "cross-env VITE_TEST_LEGACY_CJS_PLUGIN=1 pnpm test-build",
24+
"test-build-without-plugin-commonjs": "cross-env VITE_TEST_WITHOUT_PLUGIN_COMMONJS=1 pnpm test-build",
2525
"test-unit": "vitest run",
2626
"test-docs": "pnpm run docs-build",
2727
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",

packages/vite/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 3.0.0-beta.8 (2022-07-08)
2+
3+
* refactor: opt-in optimizeDeps during build and SSR (#8965) ([f8c8cf2](https://github.com/vitejs/vite/commit/f8c8cf2)), closes [#8965](https://github.com/vitejs/vite/issues/8965)
4+
* fix: cjs interop export names local clash, fix #8950 (#8953) ([2185f72](https://github.com/vitejs/vite/commit/2185f72)), closes [#8950](https://github.com/vitejs/vite/issues/8950) [#8953](https://github.com/vitejs/vite/issues/8953)
5+
* fix: handle context resolve options (#8966) ([57c6c15](https://github.com/vitejs/vite/commit/57c6c15)), closes [#8966](https://github.com/vitejs/vite/issues/8966)
6+
* fix: re-encode url to prevent fs.allow bypass (fixes #8498) (#8979) ([b835699](https://github.com/vitejs/vite/commit/b835699)), closes [#8498](https://github.com/vitejs/vite/issues/8498) [#8979](https://github.com/vitejs/vite/issues/8979)
7+
* fix(scan): detect import .ts as .js (#8969) ([752af6c](https://github.com/vitejs/vite/commit/752af6c)), closes [#8969](https://github.com/vitejs/vite/issues/8969)
8+
* refactor!: move basic ssl setup to external plugin, fix #8532 (#8961) ([5c6cf5a](https://github.com/vitejs/vite/commit/5c6cf5a)), closes [#8532](https://github.com/vitejs/vite/issues/8532) [#8961](https://github.com/vitejs/vite/issues/8961)
9+
10+
11+
112
## 3.0.0-beta.7 (2022-07-06)
213

314
* fix: ssrBuild is optional, avoid breaking VitePress (#8912) ([722f514](https://github.com/vitejs/vite/commit/722f514)), closes [#8912](https://github.com/vitejs/vite/issues/8912)

packages/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite",
3-
"version": "3.0.0-beta.7",
3+
"version": "3.0.0-beta.8",
44
"type": "module",
55
"license": "MIT",
66
"author": "Evan You",

packages/vite/src/node/build.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,15 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
297297
post: Plugin[]
298298
} {
299299
const options = config.build
300-
300+
const { commonjsOptions } = options
301+
const usePluginCommonjs =
302+
!Array.isArray(commonjsOptions?.include) ||
303+
commonjsOptions?.include.length !== 0
301304
return {
302305
pre: [
303306
...(options.watch ? [ensureWatchPlugin()] : []),
304307
watchPackageDataPlugin(config),
305-
...(config.legacy?.buildRollupPluginCommonjs
306-
? [commonjsPlugin(options.commonjsOptions)]
307-
: []),
308+
...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []),
308309
dataURIPlugin(),
309310
assetImportMetaUrlPlugin(config),
310311
...(options.rollupOptions.plugins

packages/vite/src/node/config.ts

+10-29
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ export interface ExperimentalOptions {
280280
}
281281

282282
export interface LegacyOptions {
283-
/**
284-
* Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs`
285-
*
286-
* @experimental
287-
* @deprecated
288-
* @default false
289-
*/
290-
buildRollupPluginCommonjs?: boolean
291283
/**
292284
* Revert vite build --ssr to the v2.9 strategy. Use CJS SSR build and v2.9 externalization heuristics
293285
*
@@ -571,11 +563,14 @@ export async function resolveConfig(
571563

572564
const optimizeDeps = config.optimizeDeps || {}
573565

574-
if (process.env.VITE_TEST_LEGACY_CJS_PLUGIN) {
575-
config.legacy = {
576-
...config.legacy,
577-
buildRollupPluginCommonjs: true
578-
}
566+
if (process.env.VITE_TEST_WITHOUT_PLUGIN_COMMONJS) {
567+
config.build ??= {}
568+
config.build.commonjsOptions = { include: [] }
569+
config.optimizeDeps ??= {}
570+
config.optimizeDeps.disabled = false
571+
config.ssr ??= {}
572+
config.ssr.optimizeDeps ??= {}
573+
config.ssr.optimizeDeps.disabled = false
579574
}
580575

581576
const BASE_URL = resolvedBase
@@ -616,14 +611,15 @@ export async function resolveConfig(
616611
packageCache: new Map(),
617612
createResolver,
618613
optimizeDeps: {
614+
disabled: 'build',
619615
...optimizeDeps,
620616
esbuildOptions: {
621617
preserveSymlinks: config.resolve?.preserveSymlinks,
622618
...optimizeDeps.esbuildOptions
623619
}
624620
},
625621
worker: resolvedWorkerOptions,
626-
appType: config.appType ?? middlewareMode === 'ssr' ? 'custom' : 'spa',
622+
appType: config.appType ?? (middlewareMode === 'ssr' ? 'custom' : 'spa'),
627623
experimental: {
628624
importGlobRestoreExtension: false,
629625
hmrPartialAccept: false,
@@ -661,21 +657,6 @@ export async function resolveConfig(
661657
)
662658
}
663659

664-
if (resolved.legacy?.buildRollupPluginCommonjs) {
665-
const optimizerDisabled = resolved.optimizeDeps.disabled
666-
if (!optimizerDisabled) {
667-
resolved.optimizeDeps.disabled = 'build'
668-
} else if (optimizerDisabled === 'dev') {
669-
resolved.optimizeDeps.disabled = true // Also disabled during build
670-
}
671-
const ssrOptimizerDisabled = resolved.ssr.optimizeDeps.disabled
672-
if (!ssrOptimizerDisabled) {
673-
resolved.ssr.optimizeDeps.disabled = 'build'
674-
} else if (ssrOptimizerDisabled === 'dev') {
675-
resolved.ssr.optimizeDeps.disabled = true // Also disabled during build
676-
}
677-
}
678-
679660
// Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
680661
// And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
681662
// So we need to separate the worker plugin from the plugin that vite needs to run.

packages/vite/src/node/optimizer/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ export async function runOptimizeDeps(
564564
rollupOptionsExternal.some((ext) => typeof ext !== 'string')
565565
) {
566566
throw new Error(
567-
`[vite] 'build.rollupOptions.external' can only be an array of strings or a string.\n` +
568-
`You can turn on 'legacy.buildRollupPluginCommonjs' to support more advanced options.`
567+
`[vite] 'build.rollupOptions.external' can only be an array of strings or a string when using esbuild optimization at build time.`
569568
)
570569
}
571570
external.push(...(rollupOptionsExternal as string[]))

packages/vite/src/node/server/middlewares/static.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function serveStaticMiddleware(
109109
}
110110

111111
if (redirected) {
112-
req.url = redirected
112+
req.url = encodeURIComponent(redirected)
113113
}
114114

115115
serve(req, res, next)
@@ -144,7 +144,7 @@ export function serveRawFsMiddleware(
144144
url = url.slice(FS_PREFIX.length)
145145
if (isWindows) url = url.replace(/^[A-Z]:/i, '')
146146

147-
req.url = url
147+
req.url = encodeURIComponent(url)
148148
serveFromRoot(req, res, next)
149149
} else {
150150
next()

packages/vite/src/node/ssr/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function resolveSSROptions(
6161
target,
6262
...ssr,
6363
optimizeDeps: {
64+
disabled: true,
6465
...optimizeDeps,
6566
esbuildOptions: {
6667
preserveSymlinks,

playground/fs-serve/__tests__/fs-serve.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ describe.runIf(isServe)('main', () => {
4242
expect(await page.textContent('.unsafe-fetch-8498-status')).toBe('403')
4343
})
4444

45+
test('unsafe fetch with special characters 2 (#8498)', async () => {
46+
expect(await page.textContent('.unsafe-fetch-8498-2')).toMatch('')
47+
expect(await page.textContent('.unsafe-fetch-8498-2-status')).toBe('404')
48+
})
49+
4550
test('safe fs fetch', async () => {
4651
expect(await page.textContent('.safe-fs-fetch')).toBe(stringified)
4752
expect(await page.textContent('.safe-fs-fetch-status')).toBe('200')
@@ -64,6 +69,11 @@ describe.runIf(isServe)('main', () => {
6469
expect(await page.textContent('.unsafe-fs-fetch-8498-status')).toBe('403')
6570
})
6671

72+
test('unsafe fs fetch with special characters 2 (#8498)', async () => {
73+
expect(await page.textContent('.unsafe-fs-fetch-8498-2')).toBe('')
74+
expect(await page.textContent('.unsafe-fs-fetch-8498-2-status')).toBe('404')
75+
})
76+
6777
test('nested entry', async () => {
6878
expect(await page.textContent('.nested-entry')).toBe('foobar')
6979
})

playground/fs-serve/root/src/index.html

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ <h2>Unsafe Fetch</h2>
1919
<pre class="unsafe-fetch"></pre>
2020
<pre class="unsafe-fetch-8498-status"></pre>
2121
<pre class="unsafe-fetch-8498"></pre>
22+
<pre class="unsafe-fetch-8498-2-status"></pre>
23+
<pre class="unsafe-fetch-8498-2"></pre>
2224

2325
<h2>Safe /@fs/ Fetch</h2>
2426
<pre class="safe-fs-fetch-status"></pre>
@@ -31,6 +33,8 @@ <h2>Unsafe /@fs/ Fetch</h2>
3133
<pre class="unsafe-fs-fetch"></pre>
3234
<pre class="unsafe-fs-fetch-8498-status"></pre>
3335
<pre class="unsafe-fs-fetch-8498"></pre>
36+
<pre class="unsafe-fs-fetch-8498-2-status"></pre>
37+
<pre class="unsafe-fs-fetch-8498-2"></pre>
3438

3539
<h2>Nested Entry</h2>
3640
<pre class="nested-entry"></pre>
@@ -100,6 +104,19 @@ <h2>Denied</h2>
100104
console.error(e)
101105
})
102106

107+
// outside of allowed dir with special characters 2 #8498
108+
fetch('/src/%252e%252e%252funsafe%252etxt')
109+
.then((r) => {
110+
text('.unsafe-fetch-8498-2-status', r.status)
111+
return r.text()
112+
})
113+
.then((data) => {
114+
text('.unsafe-fetch-8498-2', data)
115+
})
116+
.catch((e) => {
117+
console.error(e)
118+
})
119+
103120
// imported before, should be treated as safe
104121
fetch('/@fs/' + ROOT + '/safe.json')
105122
.then((r) => {
@@ -133,6 +150,18 @@ <h2>Denied</h2>
133150
text('.unsafe-fs-fetch-8498', JSON.stringify(data))
134151
})
135152

153+
// outside root with special characters 2 #8498
154+
fetch(
155+
'/@fs/' + ROOT + '/root/src/%252e%252e%252f%252e%252e%252funsafe%252ejson'
156+
)
157+
.then((r) => {
158+
text('.unsafe-fs-fetch-8498-2-status', r.status)
159+
return r.json()
160+
})
161+
.then((data) => {
162+
text('.unsafe-fs-fetch-8498-2', JSON.stringify(data))
163+
})
164+
136165
// not imported before, inside root with special characters, treated as safe
137166
fetch(
138167
'/@fs/' +
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
build: {
5+
commonjsOptions: {
6+
include: []
7+
}
8+
},
9+
optimizeDeps: {
10+
disabled: false
11+
}
12+
})

playground/optimize-deps/__tests__/optimize-deps.spec.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,11 @@ test('vue + vuex', async () => {
105105

106106
// When we use the Rollup CommonJS plugin instead of esbuild prebundling,
107107
// the esbuild plugins won't apply to dependencies
108-
test.skipIf(isBuild && process.env.VITE_TEST_LEGACY_CJS_PLUGIN)(
109-
'esbuild-plugin',
110-
async () => {
111-
expect(await page.textContent('.esbuild-plugin')).toMatch(
112-
`Hello from an esbuild plugin`
113-
)
114-
}
115-
)
108+
test('esbuild-plugin', async () => {
109+
expect(await page.textContent('.esbuild-plugin')).toMatch(
110+
`Hello from an esbuild plugin`
111+
)
112+
})
116113

117114
test('import from hidden dir', async () => {
118115
expect(await page.textContent('.hidden-dir')).toBe('hello!')

playground/optimize-deps/vite.config.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ module.exports = {
1414
'node:url': 'url'
1515
}
1616
},
17-
1817
optimizeDeps: {
18+
disabled: false,
1919
include: [
2020
'dep-linked-include',
2121
'nested-exclude > nested-include',
@@ -44,7 +44,11 @@ module.exports = {
4444

4545
build: {
4646
// to make tests faster
47-
minify: false
47+
minify: false,
48+
// Avoid @rollup/plugin-commonjs
49+
commonjsOptions: {
50+
include: []
51+
}
4852
},
4953

5054
plugins: [

playground/ssr-deps/server.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ export async function createServer(root = process.cwd(), hmrPort) {
3636
appType: 'custom',
3737
ssr: {
3838
noExternal: ['no-external-cjs', 'import-builtin-cjs', 'no-external-css'],
39-
external: ['nested-external']
40-
},
41-
optimizeDeps: {
42-
include: [
43-
'no-external-cjs',
44-
'import-builtin-cjs',
45-
'optimized-with-nested-external',
46-
'optimized-cjs-with-nested-external'
47-
],
48-
exclude: ['nested-external']
39+
external: ['nested-external'],
40+
optimizeDeps: {
41+
disabled: 'build'
42+
}
4943
}
5044
})
5145
// use vite's connect instance as middleware

0 commit comments

Comments
 (0)