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

feat(ssr): support external true #10939

Merged
merged 9 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module.exports = defineConfig({
}
},
{
files: ['playground/**'],
files: ['playground/**', '**/__tests__/**'],
bluwy marked this conversation as resolved.
Show resolved Hide resolved
rules: {
'node/no-extraneous-import': 'off',
'node/no-extraneous-require': 'off',
Expand Down
6 changes: 3 additions & 3 deletions docs/config/ssr-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

## ssr.external

- **Type:** `string[]`
- **Type:** `string[] | true`
- **Related:** [SSR Externals](/guide/ssr#ssr-externals)

Force externalize dependencies for SSR.
Externalize dependencies for SSR. By default, all dependencies are externalized, except linked dependencies for HMR. This can opted-out by adding the dependency to this option, or set `true` to force externalize all dependencies.
bluwy marked this conversation as resolved.
Show resolved Hide resolved

## ssr.noExternal

- **Type:** `string | RegExp | (string | RegExp)[] | true`
- **Related:** [SSR Externals](/guide/ssr#ssr-externals)

Prevent listed dependencies from being externalized for SSR. If `true`, no dependencies are externalized.
Prevent listed dependencies from being externalized for SSR. If `true`, no dependencies are externalized. If a dependency is explicitly defined in `ssr.external`, it will take priority and be externalized along with its transitive dependencies.
bluwy marked this conversation as resolved.
Show resolved Hide resolved

## ssr.target

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
hello: () => 'world'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitejs/cjs-ssr-dep",
"private": true,
"version": "0.0.0"
}
8 changes: 8 additions & 0 deletions packages/vite/src/node/ssr/__tests__/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@vitejs/unit-ssr",
"private": true,
"version": "0.0.0",
"dependencies": {
"@vitejs/cjs-ssr-dep": "link:./fixtures/cjs-ssr-dep"
}
}
31 changes: 29 additions & 2 deletions packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { expect, test } from 'vitest'
import { stripNesting } from '../ssrExternal'
import { fileURLToPath } from 'node:url'
import { describe, expect, test } from 'vitest'
import type { SSROptions } from '..'
import { resolveConfig } from '../../config'
import { createIsConfiguredAsSsrExternal, stripNesting } from '../ssrExternal'

test('stripNesting', async () => {
expect(stripNesting(['c', 'p1>c1', 'p2 > c2'])).toEqual(['c', 'c1', 'c2'])
})

describe('createIsConfiguredAsSsrExternal', () => {
test('default', async () => {
const isExternal = await createIsExternal()
expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(false)
})

test('force external', async () => {
const isExternal = await createIsExternal({ external: true })
expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(true)
})
})

async function createIsExternal(ssrConfig?: SSROptions) {
const resolvedConfig = await resolveConfig(
{
configFile: false,
root: fileURLToPath(new URL('./', import.meta.url)),
ssr: ssrConfig
},
'serve'
)
return createIsConfiguredAsSsrExternal(resolvedConfig)
}
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type SsrDepOptimizationOptions = DepOptimizationConfig

export interface SSROptions {
noExternal?: string | RegExp | (string | RegExp)[] | true
external?: string[]
external?: string[] | true
/**
* Define the target for the ssr build. The browser field in package.json
* is ignored for node but used if webworker is the target
Expand Down
65 changes: 34 additions & 31 deletions packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export function cjsSsrResolveExternals(

const ssrExternals: Set<string> = new Set()
const seen: Set<string> = new Set()
ssrConfig?.external?.forEach((id) => {
ssrExternals.add(id)
seen.add(id)
})
if (Array.isArray(ssrConfig?.external)) {
ssrConfig.external.forEach((id) => {
ssrExternals.add(id)
seen.add(id)
})
}

cjsSsrCollectExternals(
config.root,
Expand Down Expand Up @@ -156,34 +158,35 @@ export function createIsConfiguredAsSsrExternal(
// Returns true if it is configured as external, false if it is filtered
// by noExternal and undefined if it isn't affected by the explicit config
return (id: string) => {
const { ssr } = config
if (ssr) {
Comment on lines -90 to -91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed const { ssr } = config as it's already define above. Removed if (ssr) { as it's always true.

if (
// If this id is defined as external, force it as external
// Note that individual package entries are allowed in ssr.external
ssr.external?.includes(id)
) {
return true
}
const pkgName = getNpmPackageName(id)
if (!pkgName) {
return isExternalizable(id)
}
if (
// A package name in ssr.external externalizes every
// externalizable package entry
ssr.external?.includes(pkgName)
) {
return isExternalizable(id, true)
}
if (typeof noExternal === 'boolean') {
return !noExternal
}
if (noExternalFilter && !noExternalFilter(pkgName)) {
return false
}
if (
// If this id is defined as external, force it as external
// Note that individual package entries are allowed in ssr.external
ssr.external !== true &&
ssr.external?.includes(id)
) {
return true
}
const pkgName = getNpmPackageName(id)
if (!pkgName) {
return isExternalizable(id)
}
if (
// A package name in ssr.external externalizes every
// externalizable package entry
ssr.external !== true &&
ssr.external?.includes(pkgName)
) {
return isExternalizable(id, true)
}
if (typeof noExternal === 'boolean') {
return !noExternal
}
if (noExternalFilter && !noExternalFilter(pkgName)) {
return false
}
return isExternalizable(id)
// If `ssr.external: true`, all will be externalized by default, regardless if
// it's a linked package
return isExternalizable(id, ssr.external === true)
}
}

Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- 'packages/*'
- 'playground/**'
- '**/__tests__/**'