From 7a1c95cc4460a0683d832457323a00dd1423adaf Mon Sep 17 00:00:00 2001 From: patak-dev Date: Tue, 14 Jun 2022 22:43:11 +0200 Subject: [PATCH 01/13] fix: allow cache overlap in parallel builds --- packages/vite/src/node/optimizer/index.ts | 6 +++++- playground/assets/vite.config-relative-base.js | 3 +-- playground/assets/vite.config.js | 3 +-- playground/worker/vite.config-es.js | 3 +-- playground/worker/vite.config-relative-base.js | 3 +-- playground/worker/vite.config-sourcemap.js | 3 --- playground/worker/vite.config.js | 3 +-- 7 files changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 8160ee0b0d83a0..d995a6e3d89164 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -620,7 +620,11 @@ export function getOptimizedDepPath( function getDepsCacheSuffix(config: ResolvedConfig): string { let suffix = '' if (config.command === 'build') { - suffix += '_build' + // Differentiate build caches depending on outDir to allow parallel builds + const { outDir } = config.build + const buildId = + outDir.length > 8 || outDir.includes('/') ? getHash(outDir) : outDir + suffix += `_build-${buildId}` if (config.build.ssr) { suffix += '_ssr' } diff --git a/playground/assets/vite.config-relative-base.js b/playground/assets/vite.config-relative-base.js index 12c3132f79a9b0..ae09766c0768ac 100644 --- a/playground/assets/vite.config-relative-base.js +++ b/playground/assets/vite.config-relative-base.js @@ -22,6 +22,5 @@ module.exports = { }, testConfig: { baseRoute: '/relative-base/' - }, - cacheDir: 'node_modules/.vite/relative-base' + } } diff --git a/playground/assets/vite.config.js b/playground/assets/vite.config.js index 23bd11908130cd..c9d821ae3d73ee 100644 --- a/playground/assets/vite.config.js +++ b/playground/assets/vite.config.js @@ -17,6 +17,5 @@ module.exports = { assetsInlineLimit: 8192, // 8kb manifest: true, watch: {} - }, - cacheDir: 'node_modules/.vite/foo' + } } diff --git a/playground/worker/vite.config-es.js b/playground/worker/vite.config-es.js index 0d28d241ca8d42..6d6704de0bc213 100644 --- a/playground/worker/vite.config-es.js +++ b/playground/worker/vite.config-es.js @@ -38,6 +38,5 @@ module.exports = vite.defineConfig({ } } } - ], - cacheDir: 'node_modules/.vite/es' + ] }) diff --git a/playground/worker/vite.config-relative-base.js b/playground/worker/vite.config-relative-base.js index 64c2b7983fb527..8002883ca4abf1 100644 --- a/playground/worker/vite.config-relative-base.js +++ b/playground/worker/vite.config-relative-base.js @@ -40,6 +40,5 @@ module.exports = vite.defineConfig({ } } } - ], - cacheDir: 'node_modules/.vite/relative-base' + ] }) diff --git a/playground/worker/vite.config-sourcemap.js b/playground/worker/vite.config-sourcemap.js index a84d8e6699af14..c4cf61f6dea586 100644 --- a/playground/worker/vite.config-sourcemap.js +++ b/playground/worker/vite.config-sourcemap.js @@ -21,9 +21,6 @@ module.exports = vite.defineConfig((sourcemap) => { } } }, - cacheDir: `node_modules/.vite/iife-${ - typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap - }`, build: { outDir: `dist/iife-${ typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap diff --git a/playground/worker/vite.config.js b/playground/worker/vite.config.js index f937372c6e0012..d62d6c4f6d6d36 100644 --- a/playground/worker/vite.config.js +++ b/playground/worker/vite.config.js @@ -23,6 +23,5 @@ module.exports = vite.defineConfig({ entryFileNames: 'assets/[name].js' } } - }, - cacheDir: 'node_modules/.vite/iife' + } }) From 430dd5e3746a2dc42e085ee4490a914f504ebc65 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 16 Jun 2022 19:45:55 +0200 Subject: [PATCH 02/13] feat: experimental.buildRollupPluginCommonjs --- docs/guide/migration.md | 2 +- packages/vite/src/node/build.ts | 2 +- packages/vite/src/node/config.ts | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 9885dec7117742..774f12bb1640d0 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -41,7 +41,7 @@ To get back the v2 strategy, you can use [`optimizeDeps.devScan`](../config/dep- 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. -If you need to get back to the v2 strategy, you can use [`optimizeDeps.disabled: 'build'`](../config/dep-optimization-options.md#optimizedepsdisabled). +If you need to get back to the v2 strategy, you can use [`experimental.buildRollupPluginCommonjs: true`](../config/dep-optimization-options.md#experimental-buildrollupplugincommonjs). ## SSR Changes diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index c3d59d88a0549b..cefb16ffcc3d05 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -306,7 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): { pre: [ ...(options.watch ? [ensureWatchPlugin()] : []), watchPackageDataPlugin(config), - ...(!isDepsOptimizerEnabled(config) + ...(config.experimental?.buildRollupPluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []), dataURIPlugin(), diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 3b733fc4032f80..ac36b69752ec36 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -228,6 +228,13 @@ export interface ExperimentalOptions { * @default false */ importGlobRestoreExtension?: boolean + /** + * Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs` + * + * @experimental + * @default false + */ + buildRollupPluginCommonjs?: boolean } export interface ResolveWorkerOptions { @@ -522,6 +529,15 @@ export async function resolveConfig( spa: config.spa ?? true } + if (resolved.experimental?.buildRollupPluginCommonjs) { + const optimizerDisabled = resolved.optimizeDeps.disabled + if (!optimizerDisabled) { + resolved.optimizeDeps.disabled = 'build' + } else if (optimizerDisabled === 'dev') { + resolved.optimizeDeps.disabled = true // Also disabled during build + } + } + // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example). // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls. // So we need to separate the worker plugin from the plugin that vite needs to run. From 470438f52631c9b2541b76e0632d42268c366d9a Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 16 Jun 2022 19:52:06 +0200 Subject: [PATCH 03/13] feat: config.experimental.devDepsScanner --- docs/guide/migration.md | 2 +- packages/vite/src/node/config.ts | 7 +++++++ packages/vite/src/node/optimizer/index.ts | 7 ------- packages/vite/src/node/optimizer/optimizer.ts | 2 +- packages/vite/src/node/plugins/importAnalysis.ts | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 774f12bb1640d0..27ac73a7771e13 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -35,7 +35,7 @@ Vite's default dev server host is now `localhost`. You can use [`server.host`](. Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM and to reduce the number of modules the browser needs to request. In v3, the default strategy to discover and batch dependencies has changed. Vite no longer pre-scans user code with esbuild to get an initial list of dependencies on cold start. Instead, it delays the first dependency optimization run until every imported user module on load is processed. -To get back the v2 strategy, you can use [`optimizeDeps.devScan`](../config/dep-optimization-options.md#optimizedepsdevscan). +To get back the v2 strategy, you can use [`experimental.devDepsScanner`](../config/dep-optimization-options.md#optimizedepsdevscan). ## Build Changes diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ac36b69752ec36..c4b0d77e35e47c 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -228,6 +228,13 @@ export interface ExperimentalOptions { * @default false */ importGlobRestoreExtension?: boolean + /** + * Revert vite dev to the v2.9 strategy. Enable esbuild based deps scanner. + * + * @experimental + * @default false + */ + devDepsScanner?: boolean /** * Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs` * diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index d995a6e3d89164..80fc4260512c8c 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -72,13 +72,6 @@ export interface DepOptimizationOptions { * vite project root. This will overwrite default entries inference. */ entries?: string | string[] - /** - * Enable esbuild based scan phase, to get back to the optimized deps discovery - * strategy used in Vite v2 - * @default false - * @experimental - */ - devScan?: boolean /** * Force optimize listed dependencies (must be resolvable import paths, * cannot be globs). diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 5dbccae6a73db3..5183cd8edeefa5 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -50,7 +50,7 @@ export async function initDepsOptimizer( const { logger } = config const isBuild = config.command === 'build' - const scan = config.command !== 'build' && config.optimizeDeps.devScan + const scan = config.command !== 'build' && config.experimental?.devDepsScanner const sessionTimestamp = Date.now().toString() diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index b4f5fbfa442407..49fc66dab76bbc 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -623,7 +623,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // Unexpected error, log the issue but avoid an unhandled exception config.logger.error(e.message) }) - if (depsOptimizer && !config.optimizeDeps.devScan) { + if (depsOptimizer && !config.experimental?.devDepsScanner) { depsOptimizer.delayDepsOptimizerUntil(id, () => request) } }) From 44db36f4b72938b3ec3c4d42a0d0e151b0a25a74 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 16 Jun 2022 20:01:47 +0200 Subject: [PATCH 04/13] feat: config.experimental.buildSsrCjsExternalHeuristics --- docs/guide/migration.md | 2 +- packages/vite/src/node/build.ts | 2 +- packages/vite/src/node/config.ts | 7 +++++++ packages/vite/src/node/plugins/importAnalysis.ts | 2 +- packages/vite/src/node/ssr/ssrExternal.ts | 4 ++-- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 27ac73a7771e13..ac93003e537787 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -47,7 +47,7 @@ If you need to get back to the v2 strategy, you can use [`experimental.buildRoll 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#ssrnoexternal) to control what dependencies to include in the SSR bundle. -If using ESM for SSR isn't possible in your project, you can set `ssr.format: 'cjs'` to generate a CJS bundle. In this case, the same externalization strategy of Vite v2 will be used. +If using ESM for SSR isn't possible in your project, you can set `experimental.buildSsrCjsExternalHeuristics: true` to generate a CJS bundle using the same externalization strategy of Vite v2. ## General Changes diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index cefb16ffcc3d05..cbdacab0f2a6fc 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -398,7 +398,7 @@ async function doBuild( // In CJS, we can pass the externals to rollup as is. In ESM, we need to // do it in the resolve plugin so we can add the resolved extension for // deep node_modules imports - if (ssr && config.ssr?.format === 'cjs') { + if (ssr && config.experimental?.buildSsrCjsExternalHeuristics) { external = await cjsSsrResolveExternal(config, userExternal) } diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index c4b0d77e35e47c..1dcc8d543cd550 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -242,6 +242,13 @@ export interface ExperimentalOptions { * @default false */ buildRollupPluginCommonjs?: boolean + /** + * Revert vite build --ssr to the v2.9 strategy. Use CJS SSR build and v2.9 externalization heuristics + * + * @experimental + * @default false + */ + buildSsrCjsExternalHeuristics?: boolean } export interface ResolveWorkerOptions { diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 49fc66dab76bbc..603c3207a3dfa8 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -372,7 +372,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } // skip ssr external if (ssr) { - if (config.ssr?.format === 'cjs') { + if (config.experimental?.buildSsrCjsExternalHeuristics) { if (cjsShouldExternalizeForSSR(specifier, server._ssrExternals)) { continue } diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/ssr/ssrExternal.ts index de0dd5bab031e4..daad169ff6fc2f 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/ssr/ssrExternal.ts @@ -167,8 +167,8 @@ function createIsSsrExternal( } } -// When ssr.format is 'cjs', this function is used reverting to the Vite 2.9 -// SSR externalization heuristics +// When config.experimental.buildSsrCjsExternalHeuristics is enabled, this function +// is used reverting to the Vite 2.9 SSR externalization heuristics function cjsSsrCollectExternals( root: string, preserveSymlinks: boolean | undefined, From 7ad8c06186f33896cf7ae21e9ef862831d98e64e Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 16 Jun 2022 20:08:12 +0200 Subject: [PATCH 05/13] chore: mark new options as deprecated --- docs/guide/migration.md | 18 +++++++++++++++--- packages/vite/src/node/config.ts | 3 +++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index ac93003e537787..7db64afd92335f 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -27,7 +27,19 @@ A small fraction of users will now require using [@vitejs/plugin-legacy](https:/ - `build.polyfillDynamicImport` (use [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) for browsers without dynamic import support) - `optimizeDeps.keepNames` (switch to [`optimizeDeps.esbuildOptions.keepNames`](../config/dep-optimization-options.md#optimizedepsesbuildoptions)) -## Dev Server Changes +## Optimized deps and SSR changes + +These are the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, options have been added to revert to the Vite v2 strategies. + +:::warning +These options are marked as experimental and deprecated. They may be removed in a future v3 minor. + +- `experimental.devDepsScanner` +- `experimental.buildRollupPluginCommonjs` +- `experimental.buildSsrCjsExternalHeuristics` + ::: + +### Dev Server Changes 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. @@ -37,13 +49,13 @@ Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM an To get back the v2 strategy, you can use [`experimental.devDepsScanner`](../config/dep-optimization-options.md#optimizedepsdevscan). -## Build Changes +### Build Changes 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. If you need to get back to the v2 strategy, you can use [`experimental.buildRollupPluginCommonjs: true`](../config/dep-optimization-options.md#experimental-buildrollupplugincommonjs). -## SSR Changes +### SSR Changes 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#ssrnoexternal) to control what dependencies to include in the SSR bundle. diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1dcc8d543cd550..cea98eb165cf78 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -232,6 +232,7 @@ export interface ExperimentalOptions { * Revert vite dev to the v2.9 strategy. Enable esbuild based deps scanner. * * @experimental + * @deprecated * @default false */ devDepsScanner?: boolean @@ -239,6 +240,7 @@ export interface ExperimentalOptions { * Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs` * * @experimental + * @deprecated * @default false */ buildRollupPluginCommonjs?: boolean @@ -246,6 +248,7 @@ export interface ExperimentalOptions { * Revert vite build --ssr to the v2.9 strategy. Use CJS SSR build and v2.9 externalization heuristics * * @experimental + * @deprecated * @default false */ buildSsrCjsExternalHeuristics?: boolean From 11345363dbe989e107d49ca328bf09b45b0d9ecb Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 17 Jun 2022 09:43:31 +0200 Subject: [PATCH 06/13] feat: experimental to legacy options --- docs/guide/migration.md | 18 +++++++++--------- packages/vite/src/node/build.ts | 4 ++-- packages/vite/src/node/config.ts | 14 ++++++++++++-- packages/vite/src/node/optimizer/optimizer.ts | 2 +- .../vite/src/node/plugins/importAnalysis.ts | 4 ++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 7db64afd92335f..7fdcf4cf49ef1f 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -27,16 +27,16 @@ A small fraction of users will now require using [@vitejs/plugin-legacy](https:/ - `build.polyfillDynamicImport` (use [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) for browsers without dynamic import support) - `optimizeDeps.keepNames` (switch to [`optimizeDeps.esbuildOptions.keepNames`](../config/dep-optimization-options.md#optimizedepsesbuildoptions)) -## Optimized deps and SSR changes +## Achitecture changes and legacy Options -These are the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, options have been added to revert to the Vite v2 strategies. +This section describes the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, lecagy options have been added to revert to the Vite v2 strategies. :::warning -These options are marked as experimental and deprecated. They may be removed in a future v3 minor. +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. -- `experimental.devDepsScanner` -- `experimental.buildRollupPluginCommonjs` -- `experimental.buildSsrCjsExternalHeuristics` +- `legacy.devDepsScanner` +- `legacy.buildRollupPluginCommonjs` +- `legacy.buildSsrCjsExternalHeuristics` ::: ### Dev Server Changes @@ -47,19 +47,19 @@ Vite's default dev server host is now `localhost`. You can use [`server.host`](. Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM and to reduce the number of modules the browser needs to request. In v3, the default strategy to discover and batch dependencies has changed. Vite no longer pre-scans user code with esbuild to get an initial list of dependencies on cold start. Instead, it delays the first dependency optimization run until every imported user module on load is processed. -To get back the v2 strategy, you can use [`experimental.devDepsScanner`](../config/dep-optimization-options.md#optimizedepsdevscan). +To get back the v2 strategy, you can use `experimental.devDepsScanner`. ### Build Changes 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. -If you need to get back to the v2 strategy, you can use [`experimental.buildRollupPluginCommonjs: true`](../config/dep-optimization-options.md#experimental-buildrollupplugincommonjs). +If you need to get back to the v2 strategy, you can use `legacy.buildRollupPluginCommonjs`. ### SSR Changes 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#ssrnoexternal) to control what dependencies to include in the SSR bundle. -If using ESM for SSR isn't possible in your project, you can set `experimental.buildSsrCjsExternalHeuristics: true` to generate a CJS bundle using the same externalization strategy of Vite v2. +If using ESM for SSR isn't possible in your project, you can set `legacy.buildSsrCjsExternalHeuristics` to generate a CJS bundle using the same externalization strategy of Vite v2. ## General Changes diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index cbdacab0f2a6fc..dcacd542d49e7e 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -306,7 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): { pre: [ ...(options.watch ? [ensureWatchPlugin()] : []), watchPackageDataPlugin(config), - ...(config.experimental?.buildRollupPluginCommonjs + ...(config.legacy?.buildRollupPluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []), dataURIPlugin(), @@ -398,7 +398,7 @@ async function doBuild( // In CJS, we can pass the externals to rollup as is. In ESM, we need to // do it in the resolve plugin so we can add the resolved extension for // deep node_modules imports - if (ssr && config.experimental?.buildSsrCjsExternalHeuristics) { + if (ssr && config.legacy?.buildSsrCjsExternalHeuristics) { external = await cjsSsrResolveExternal(config, userExternal) } diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index cea98eb165cf78..aa78e2a20c19f4 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -162,11 +162,18 @@ export interface UserConfig { /** * Experimental features * - * Features under this field are addressed to be changed that might NOT follow semver. + * Features under this field could change in the future and might NOT follow semver. * Please be careful and always pin Vite's version when using them. * @experimental */ experimental?: ExperimentalOptions + /** + * Legacy options + * + * Features under this field only follow semver for patches, they could be removed in a + * future minor version. Please always pin Vite's version to a minor when using them. + */ + legacy?: LegacyOptions /** * Log level. * Default: 'info' @@ -228,6 +235,9 @@ export interface ExperimentalOptions { * @default false */ importGlobRestoreExtension?: boolean +} + +export interface LegacyOptions { /** * Revert vite dev to the v2.9 strategy. Enable esbuild based deps scanner. * @@ -546,7 +556,7 @@ export async function resolveConfig( spa: config.spa ?? true } - if (resolved.experimental?.buildRollupPluginCommonjs) { + if (resolved.legacy?.buildRollupPluginCommonjs) { const optimizerDisabled = resolved.optimizeDeps.disabled if (!optimizerDisabled) { resolved.optimizeDeps.disabled = 'build' diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 5183cd8edeefa5..8f6e7de61b2845 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -50,7 +50,7 @@ export async function initDepsOptimizer( const { logger } = config const isBuild = config.command === 'build' - const scan = config.command !== 'build' && config.experimental?.devDepsScanner + const scan = config.command !== 'build' && config.legacy?.devDepsScanner const sessionTimestamp = Date.now().toString() diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 603c3207a3dfa8..2f2dfb7217151e 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -372,7 +372,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } // skip ssr external if (ssr) { - if (config.experimental?.buildSsrCjsExternalHeuristics) { + if (config.legacy?.buildSsrCjsExternalHeuristics) { if (cjsShouldExternalizeForSSR(specifier, server._ssrExternals)) { continue } @@ -623,7 +623,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // Unexpected error, log the issue but avoid an unhandled exception config.logger.error(e.message) }) - if (depsOptimizer && !config.experimental?.devDepsScanner) { + if (depsOptimizer && !config.legacy?.devDepsScanner) { depsOptimizer.delayDepsOptimizerUntil(id, () => request) } }) From 9ddfab6ee4d460073ab9dda20501c4b62cbe05e3 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 17 Jun 2022 12:22:12 +0200 Subject: [PATCH 07/13] chore: spacing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- docs/guide/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 7fdcf4cf49ef1f..ee36713ce7398a 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -37,7 +37,7 @@ These options are marked as experimental and deprecated. They may be removed in - `legacy.devDepsScanner` - `legacy.buildRollupPluginCommonjs` - `legacy.buildSsrCjsExternalHeuristics` - ::: +::: ### Dev Server Changes From e8c086d072c013d38bb9cbff9fe158efbf057910 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 17 Jun 2022 12:22:45 +0200 Subject: [PATCH 08/13] chore: thanks sapphi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- docs/guide/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index ee36713ce7398a..fd4f0da09c72a7 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -47,7 +47,7 @@ Vite's default dev server host is now `localhost`. You can use [`server.host`](. Vite optimizes dependencies with esbuild to both convert CJS-only deps to ESM and to reduce the number of modules the browser needs to request. In v3, the default strategy to discover and batch dependencies has changed. Vite no longer pre-scans user code with esbuild to get an initial list of dependencies on cold start. Instead, it delays the first dependency optimization run until every imported user module on load is processed. -To get back the v2 strategy, you can use `experimental.devDepsScanner`. +To get back the v2 strategy, you can use `legacy.devDepsScanner`. ### Build Changes From 9ba395021fea9d7f1e31126cc9c9c13d897764d0 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 17 Jun 2022 12:31:07 +0200 Subject: [PATCH 09/13] chore: format --- docs/guide/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index fd4f0da09c72a7..7d5d36511a9ae2 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -37,7 +37,7 @@ These options are marked as experimental and deprecated. They may be removed in - `legacy.devDepsScanner` - `legacy.buildRollupPluginCommonjs` - `legacy.buildSsrCjsExternalHeuristics` -::: + ::: ### Dev Server Changes From 4b4c89553bd5ffdb1e128d9f0f6a7abce1ee3598 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 17 Jun 2022 12:43:48 +0200 Subject: [PATCH 10/13] chore: update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- docs/guide/migration.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 7d5d36511a9ae2..eabff3f6b71dbe 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -37,7 +37,8 @@ These options are marked as experimental and deprecated. They may be removed in - `legacy.devDepsScanner` - `legacy.buildRollupPluginCommonjs` - `legacy.buildSsrCjsExternalHeuristics` - ::: + +::: ### Dev Server Changes From 165fba6ca85be64c5319a9fc1d4e60ebabcc733b Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 17 Jun 2022 18:30:50 +0200 Subject: [PATCH 11/13] chore: fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- docs/guide/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/migration.md b/docs/guide/migration.md index eabff3f6b71dbe..51f9d1a9ef8eef 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -29,7 +29,7 @@ A small fraction of users will now require using [@vitejs/plugin-legacy](https:/ ## Achitecture changes and legacy Options -This section describes the biggest architecture changes in Vite v3. To allow projects to migrate from v2 in case of a compat issue, lecagy options have been added to revert to the Vite v2 strategies. +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. :::warning 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. From 0528fba59bb12274c6651e4a433dd362062c982a Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 17 Jun 2022 18:44:20 +0200 Subject: [PATCH 12/13] chore: revert unrelated changes --- packages/vite/src/node/optimizer/index.ts | 6 +----- playground/assets/vite.config-relative-base.js | 3 ++- playground/assets/vite.config.js | 3 ++- playground/worker/vite.config-es.js | 3 ++- playground/worker/vite.config-relative-base.js | 3 ++- playground/worker/vite.config-sourcemap.js | 3 +++ playground/worker/vite.config.js | 3 ++- 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 80fc4260512c8c..78cc0c28fd57a0 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -613,11 +613,7 @@ export function getOptimizedDepPath( function getDepsCacheSuffix(config: ResolvedConfig): string { let suffix = '' if (config.command === 'build') { - // Differentiate build caches depending on outDir to allow parallel builds - const { outDir } = config.build - const buildId = - outDir.length > 8 || outDir.includes('/') ? getHash(outDir) : outDir - suffix += `_build-${buildId}` + suffix += '_build' if (config.build.ssr) { suffix += '_ssr' } diff --git a/playground/assets/vite.config-relative-base.js b/playground/assets/vite.config-relative-base.js index ae09766c0768ac..12c3132f79a9b0 100644 --- a/playground/assets/vite.config-relative-base.js +++ b/playground/assets/vite.config-relative-base.js @@ -22,5 +22,6 @@ module.exports = { }, testConfig: { baseRoute: '/relative-base/' - } + }, + cacheDir: 'node_modules/.vite/relative-base' } diff --git a/playground/assets/vite.config.js b/playground/assets/vite.config.js index c9d821ae3d73ee..23bd11908130cd 100644 --- a/playground/assets/vite.config.js +++ b/playground/assets/vite.config.js @@ -17,5 +17,6 @@ module.exports = { assetsInlineLimit: 8192, // 8kb manifest: true, watch: {} - } + }, + cacheDir: 'node_modules/.vite/foo' } diff --git a/playground/worker/vite.config-es.js b/playground/worker/vite.config-es.js index 6d6704de0bc213..0d28d241ca8d42 100644 --- a/playground/worker/vite.config-es.js +++ b/playground/worker/vite.config-es.js @@ -38,5 +38,6 @@ module.exports = vite.defineConfig({ } } } - ] + ], + cacheDir: 'node_modules/.vite/es' }) diff --git a/playground/worker/vite.config-relative-base.js b/playground/worker/vite.config-relative-base.js index 8002883ca4abf1..64c2b7983fb527 100644 --- a/playground/worker/vite.config-relative-base.js +++ b/playground/worker/vite.config-relative-base.js @@ -40,5 +40,6 @@ module.exports = vite.defineConfig({ } } } - ] + ], + cacheDir: 'node_modules/.vite/relative-base' }) diff --git a/playground/worker/vite.config-sourcemap.js b/playground/worker/vite.config-sourcemap.js index c4cf61f6dea586..a84d8e6699af14 100644 --- a/playground/worker/vite.config-sourcemap.js +++ b/playground/worker/vite.config-sourcemap.js @@ -21,6 +21,9 @@ module.exports = vite.defineConfig((sourcemap) => { } } }, + cacheDir: `node_modules/.vite/iife-${ + typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap + }`, build: { outDir: `dist/iife-${ typeof sourcemap === 'boolean' ? 'sourcemap' : 'sourcemap-' + sourcemap diff --git a/playground/worker/vite.config.js b/playground/worker/vite.config.js index d62d6c4f6d6d36..f937372c6e0012 100644 --- a/playground/worker/vite.config.js +++ b/playground/worker/vite.config.js @@ -23,5 +23,6 @@ module.exports = vite.defineConfig({ entryFileNames: 'assets/[name].js' } } - } + }, + cacheDir: 'node_modules/.vite/iife' }) From d22f8d74783d4d7a3dc934d20a6fc90e5c38fc7d Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 17 Jun 2022 18:59:51 +0200 Subject: [PATCH 13/13] fix: force ssr.format 'cjs' for buildSsrCjsExternalHeuristics --- packages/vite/src/node/config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index c2d877b292e3e6..98c2386eff1379 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -501,7 +501,11 @@ export async function resolveConfig( : '' const server = resolveServerOptions(resolvedRoot, config.server, logger) - const ssr = resolveSSROptions(config.ssr) + let ssr = resolveSSROptions(config.ssr) + if (config.legacy?.buildSsrCjsExternalHeuristics) { + if (ssr) ssr.format = 'cjs' + else ssr = { target: 'node', format: 'cjs' } + } const optimizeDeps = config.optimizeDeps || {}