mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
feat(ssr): support external true (#10939)
This commit is contained in:
parent
d1b143fd58
commit
e451be224b
@ -2,17 +2,25 @@
|
||||
|
||||
## ssr.external
|
||||
|
||||
- **Type:** `string[]`
|
||||
- **Type:** `string[] | true`
|
||||
- **Related:** [SSR Externals](/guide/ssr#ssr-externals)
|
||||
|
||||
Force externalize dependencies for SSR.
|
||||
Externalize the given dependencies and their transitive dependencies for SSR. By default, all dependencies are externalized except for linked dependencies (for HMR). If you prefer to externalize the linked dependency, you can pass its name to this option.
|
||||
|
||||
If `true`, all dependencies including linked dependencies are externalized.
|
||||
|
||||
Note that the explicitly listed dependencies (using `string[]` type) will always take priority if they're also listed in `ssr.noExternal` (using any type).
|
||||
|
||||
## 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, which they will get bundled in build. By default, only linked dependencies are not externalized (for HMR). If you prefer to externalize the linked dependency, you can pass its name to the `ssr.external` option.
|
||||
|
||||
If `true`, no dependencies are externalized. However, dependencies explicitly listed in `ssr.external` (using `string[]` type) can take priority and still be externalized.
|
||||
|
||||
Note that if both `ssr.noExternal: true` and `ssr.external: true` are configured, `ssr.noExternal` takes priority and no dependencies are externalized.
|
||||
|
||||
## ssr.target
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
// eslint-disable-next-line no-undef
|
||||
module.exports = {
|
||||
hello: () => 'world',
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "@vitejs/cjs-ssr-dep",
|
||||
"private": true,
|
||||
"version": "0.0.0"
|
||||
}
|
8
packages/vite/src/node/ssr/__tests__/package.json
Normal file
8
packages/vite/src/node/ssr/__tests__/package.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "@vitejs/unit-ssr",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"@vitejs/cjs-ssr-dep": "link:./fixtures/cjs-ssr-dep"
|
||||
}
|
||||
}
|
29
packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts
Normal file
29
packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import type { SSROptions } from '..'
|
||||
import { resolveConfig } from '../../config'
|
||||
import { createIsConfiguredAsSsrExternal } from '../ssrExternal'
|
||||
|
||||
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)
|
||||
}
|
@ -6,7 +6,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
|
||||
|
@ -87,34 +87,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, importer?: string) => {
|
||||
const { ssr } = config
|
||||
if (ssr) {
|
||||
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, importer)
|
||||
}
|
||||
if (
|
||||
// A package name in ssr.external externalizes every
|
||||
// externalizable package entry
|
||||
ssr.external?.includes(pkgName)
|
||||
) {
|
||||
return isExternalizable(id, importer, 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
|
||||
}
|
||||
return isExternalizable(id, importer)
|
||||
const pkgName = getNpmPackageName(id)
|
||||
if (!pkgName) {
|
||||
return isExternalizable(id, importer)
|
||||
}
|
||||
if (
|
||||
// A package name in ssr.external externalizes every
|
||||
// externalizable package entry
|
||||
ssr.external !== true &&
|
||||
ssr.external?.includes(pkgName)
|
||||
) {
|
||||
return isExternalizable(id, importer, true)
|
||||
}
|
||||
if (typeof noExternal === 'boolean') {
|
||||
return !noExternal
|
||||
}
|
||||
if (noExternalFilter && !noExternalFilter(pkgName)) {
|
||||
return false
|
||||
}
|
||||
// If `ssr.external: true`, all will be externalized by default, regardless if
|
||||
// it's a linked package
|
||||
return isExternalizable(id, importer, ssr.external === true)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -424,6 +424,32 @@ importers:
|
||||
specifier: ^8.15.0
|
||||
version: 8.15.0
|
||||
|
||||
packages/vite/src/node/__tests__/packages/module: {}
|
||||
|
||||
packages/vite/src/node/__tests__/packages/name: {}
|
||||
|
||||
packages/vite/src/node/__tests__/packages/noname: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/lerna/nested: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/none/nested: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/pnpm: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/pnpm/nested: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/yarn: {}
|
||||
|
||||
packages/vite/src/node/server/__tests__/fixtures/yarn/nested: {}
|
||||
|
||||
packages/vite/src/node/ssr/__tests__:
|
||||
dependencies:
|
||||
'@vitejs/cjs-ssr-dep':
|
||||
specifier: link:./fixtures/cjs-ssr-dep
|
||||
version: link:fixtures/cjs-ssr-dep
|
||||
|
||||
packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep: {}
|
||||
|
||||
playground:
|
||||
devDependencies:
|
||||
convert-source-map:
|
||||
@ -1896,7 +1922,6 @@ packages:
|
||||
/@babel/highlight@7.23.4:
|
||||
resolution: {integrity: sha512-acGdbYSfp2WheJoJm/EBBBLh/ID8KDc64ISZ9DYtBmC8/Q204PZJLHyzeB5qMzJ5trcOkybd78M4x2KWsUq++A==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
requiresBuild: true
|
||||
dependencies:
|
||||
'@babel/helper-validator-identifier': 7.22.20
|
||||
chalk: 2.4.2
|
||||
|
@ -1,3 +1,4 @@
|
||||
packages:
|
||||
- 'packages/*'
|
||||
- 'playground/**'
|
||||
- 'packages/**/__tests__/**'
|
||||
|
Loading…
Reference in New Issue
Block a user