Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): skip dedupe require in esm #5714

Merged
merged 3 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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[]`
Expand Down Expand Up @@ -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 `''`.
bluwy marked this conversation as resolved.
Show resolved Hide resolved
:::

## 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,14 +1,19 @@
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,
* so that SSR builds can benefit from `resolve.dedupe` like they do
* 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 {
Expand Down Expand Up @@ -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'
)
}