/
ssrExternal.ts
224 lines (199 loc) · 6.21 KB
/
ssrExternal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import fs from 'fs'
import path from 'path'
import { createRequire } from 'module'
import { createFilter } from '@rollup/pluginutils'
import type { InternalResolveOptions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import {
createDebugger,
isDefined,
lookupFile,
normalizePath,
resolveFrom
} from '../utils'
import type { Logger, ResolvedConfig } from '..'
const debug = createDebugger('vite:ssr-external')
/**
* Converts "parent > child" syntax to just "child"
*/
export function stripNesting(packages: string[]): string[] {
return packages.map((s) => {
const arr = s.split('>')
return arr[arr.length - 1].trim()
})
}
/**
* Heuristics for determining whether a dependency should be externalized for
* server-side rendering.
*/
export function resolveSSRExternal(
config: ResolvedConfig,
knownImports: string[]
): string[] {
// strip nesting since knownImports may be passed in from optimizeDeps which
// supports a "parent > child" syntax
knownImports = stripNesting(knownImports)
const ssrConfig = config.ssr
if (ssrConfig?.noExternal === true) {
return []
}
const ssrExternals: Set<string> = new Set()
const seen: Set<string> = new Set()
ssrConfig?.external?.forEach((id) => {
ssrExternals.add(id)
seen.add(id)
})
collectExternals(
config.root,
config.resolve.preserveSymlinks,
ssrExternals,
seen,
config.logger
)
const importedDeps = knownImports.map(getNpmPackageName).filter(isDefined)
for (const dep of importedDeps) {
// Assume external if not yet seen
// At this point, the project root and any linked packages have had their dependencies checked,
// so we can safely mark any knownImports not yet seen as external. They are guaranteed to be
// dependencies of packages in node_modules.
if (!seen.has(dep)) {
ssrExternals.add(dep)
}
}
// ensure `vite/dynamic-import-polyfill` is bundled (issue #1865)
ssrExternals.delete('vite')
let externals = [...ssrExternals]
if (ssrConfig?.noExternal) {
externals = externals.filter(
createFilter(undefined, ssrConfig.noExternal, { resolve: false })
)
}
return externals
}
const CJS_CONTENT_RE =
/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(|\bObject\.(defineProperty|defineProperties|assign)\s*\(\s*exports\b/
// TODO: use import()
const _require = createRequire(import.meta.url)
// do we need to do this ahead of time or could we do it lazily?
function collectExternals(
root: string,
preserveSymlinks: boolean | undefined,
ssrExternals: Set<string>,
seen: Set<string>,
logger: Logger
) {
const rootPkgContent = lookupFile(root, ['package.json'])
if (!rootPkgContent) {
return
}
const rootPkg = JSON.parse(rootPkgContent)
const deps = {
...rootPkg.devDependencies,
...rootPkg.dependencies
}
const resolveOptions: InternalResolveOptions = {
root,
preserveSymlinks,
isProduction: false,
isBuild: true
}
const depsToTrace = new Set<string>()
for (const id in deps) {
if (seen.has(id)) continue
seen.add(id)
let esmEntry: string | undefined
let requireEntry: string
try {
esmEntry = tryNodeResolve(
id,
undefined,
resolveOptions,
true, // we set `targetWeb` to `true` to get the ESM entry
undefined,
true
)?.id
// normalizePath required for windows. tryNodeResolve uses normalizePath
// which returns with '/', require.resolve returns with '\\'
requireEntry = normalizePath(_require.resolve(id, { paths: [root] }))
} catch (e) {
try {
// no main entry, but deep imports may be allowed
const pkgPath = resolveFrom(`${id}/package.json`, root)
if (pkgPath.includes('node_modules')) {
ssrExternals.add(id)
} else {
depsToTrace.add(path.dirname(pkgPath))
}
continue
} catch {}
// resolve failed, assume include
debug(`Failed to resolve entries for package "${id}"\n`, e)
continue
}
// no esm entry but has require entry
if (!esmEntry) {
ssrExternals.add(id)
}
// trace the dependencies of linked packages
else if (!esmEntry.includes('node_modules')) {
const pkgPath = resolveFrom(`${id}/package.json`, root)
depsToTrace.add(path.dirname(pkgPath))
}
// has separate esm/require entry, assume require entry is cjs
else if (esmEntry !== requireEntry) {
ssrExternals.add(id)
}
// if we're externalizing ESM and CJS should basically just always do it?
// or are there others like SystemJS / AMD that we'd need to handle?
// for now, we'll just leave this as is
else if (/\.m?js$/.test(esmEntry)) {
const pkgPath = resolveFrom(`${id}/package.json`, root)
const pkgContent = fs.readFileSync(pkgPath, 'utf-8')
if (!pkgContent) {
continue
}
const pkg = JSON.parse(pkgContent)
if (pkg.type === 'module' || esmEntry.endsWith('.mjs')) {
ssrExternals.add(id)
continue
}
// check if the entry is cjs
const content = fs.readFileSync(esmEntry, 'utf-8')
if (CJS_CONTENT_RE.test(content)) {
ssrExternals.add(id)
continue
}
logger.warn(
`${id} doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.`
)
}
}
for (const depRoot of depsToTrace) {
collectExternals(depRoot, preserveSymlinks, ssrExternals, seen, logger)
}
}
export function shouldExternalizeForSSR(
id: string,
externals: string[]
): boolean {
const should = externals.some((e) => {
if (id === e) {
return true
}
// deep imports, check ext before externalizing - only externalize
// extension-less imports and explicit .js imports
if (id.startsWith(e + '/') && (!path.extname(id) || id.endsWith('.js'))) {
return true
}
})
return should
}
function getNpmPackageName(importPath: string): string | null {
const parts = importPath.split('/')
if (parts[0].startsWith('@')) {
if (!parts[1]) return null
return `${parts[0]}/${parts[1]}`
} else {
return parts[0]
}
}