From a8718c8b5553205f866469650b1ef5b77f374a55 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 17 Nov 2021 02:38:15 +0800 Subject: [PATCH 1/3] fix(ssr): skip dedupe require in esm --- packages/vite/src/node/plugins/ssrRequireHook.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/ssrRequireHook.ts b/packages/vite/src/node/plugins/ssrRequireHook.ts index c1d24ca40d5f07..eb61a2b817403f 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,13 @@ 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 || + arraify(config.build.rollupOptions?.output).find( + (output) => output?.format === 'es' || output?.format === 'esm' + ) + ) { return null } return { From a91d417fb9d0e96b6fbb266ab6c3f3f14be50ae0 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 17 Nov 2021 11:50:37 +0800 Subject: [PATCH 2/3] refactor(ssr): update require hook --- packages/vite/src/node/plugins/ssrRequireHook.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/ssrRequireHook.ts b/packages/vite/src/node/plugins/ssrRequireHook.ts index eb61a2b817403f..b2b0e442790283 100644 --- a/packages/vite/src/node/plugins/ssrRequireHook.ts +++ b/packages/vite/src/node/plugins/ssrRequireHook.ts @@ -12,9 +12,7 @@ export function ssrRequireHookPlugin(config: ResolvedConfig): Plugin | null { if ( config.command !== 'build' || !config.resolve.dedupe?.length || - arraify(config.build.rollupOptions?.output).find( - (output) => output?.format === 'es' || output?.format === 'esm' - ) + isBuildOutputEsm(config) ) { return null } @@ -74,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' + ) +} From 7d0e9d5f5169fe5d0baef52b58510b4c5c1983ac Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 17 Nov 2021 12:01:03 +0800 Subject: [PATCH 3/3] docs: update dedupe --- docs/config/index.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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