Skip to content

Commit

Permalink
fix(ssr): skip dedupe require in esm (vitejs#5714)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and aleclarson committed Dec 29, 2021
1 parent fcd84e9 commit aefb07c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 6 additions & 3 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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[]`
Expand Down Expand Up @@ -358,9 +362,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
Expand Down
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/ssrRequireHook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import MagicString from 'magic-string'
import { ResolvedConfig } from '..'
import { Plugin } from '../plugin'
import { arraify } from '../utils'

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 {
Expand Down Expand Up @@ -62,3 +67,10 @@ export function hookNodeResolve(
Module._resolveFilename = resolveFilename
}
}

function isBuildOutputEsm(config: ResolvedConfig) {
const outputs = arraify(config.build.rollupOptions?.output)
return outputs.some(
(output) => output?.format === 'es' || output?.format === 'esm'
)
}

0 comments on commit aefb07c

Please sign in to comment.