diff --git a/docs/config/index.md b/docs/config/index.md index d2495dadbbf2ff..f89581fc922932 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -172,6 +172,10 @@ export default defineConfig(async ({ command, mode }) => { If you have duplicated copies of the same dependency in your app (likely due to hoisting or linked packages in monorepos), use this option to force Vite to always resolve listed dependencies to the same copy (from project root). + :::warning SSR + ESM + For SSR builds, deduplication does not work for ESM build outputs configured from `build.rollupOptions.output`. A workaround is to use CJS build outputs until ESM has better plugin support for module loading. + ::: + ### resolve.conditions - **Type:** `string[]` @@ -361,9 +365,8 @@ export default defineConfig(async ({ command, mode }) => { Env variables starts with `envPrefix` will be exposed to your client source code via import.meta.env. -:::warning SECURITY NOTES - -- `envPrefix` should not be set as `''`, which will expose all your env variables and cause unexpected leaking of of sensitive information. Vite will throw error when detecting `''`. + :::warning SECURITY NOTES + `envPrefix` should not be set as `''`, which will expose all your env variables and cause unexpected leaking of of sensitive information. Vite will throw error when detecting `''`. ::: ## Server Options diff --git a/packages/vite/src/node/plugins/ssrRequireHook.ts b/packages/vite/src/node/plugins/ssrRequireHook.ts index c1d24ca40d5f07..b2b0e442790283 100644 --- a/packages/vite/src/node/plugins/ssrRequireHook.ts +++ b/packages/vite/src/node/plugins/ssrRequireHook.ts @@ -1,6 +1,7 @@ import MagicString from 'magic-string' import { ResolvedConfig } from '..' import { Plugin } from '../plugin' +import { arraify } from '../utils' /** * This plugin hooks into Node's module resolution algorithm at runtime, @@ -8,7 +9,11 @@ import { Plugin } from '../plugin' * in development. */ export function ssrRequireHookPlugin(config: ResolvedConfig): Plugin | null { - if (config.command !== 'build' || !config.resolve.dedupe?.length) { + if ( + config.command !== 'build' || + !config.resolve.dedupe?.length || + isBuildOutputEsm(config) + ) { return null } return { @@ -67,3 +72,10 @@ export function hookNodeResolve( Module._resolveFilename = prevResolver } } + +function isBuildOutputEsm(config: ResolvedConfig) { + const outputs = arraify(config.build.rollupOptions?.output) + return outputs.some( + (output) => output?.format === 'es' || output?.format === 'esm' + ) +}