vite 2.6.0-beta.0 → 2.6.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of vite might be problematic. Click here for more details.
- package/CHANGELOG.md +66 -0
- package/LICENSE.md +161 -3011
- package/bin/vite.js +2 -1
- package/dist/node/chunks/{dep-91aa74e8.js → dep-1e0a75a8.js} +58 -32
- package/dist/node/chunks/dep-1e0a75a8.js.map +1 -0
- package/dist/node/chunks/{dep-e36486f6.js → dep-ac1b4bf9.js} +1 -1
- package/dist/node/chunks/dep-ac1b4bf9.js.map +1 -0
- package/dist/node/chunks/{dep-c7e510f9.js → dep-ba6b30a0.js} +27538 -4953
- package/dist/node/chunks/dep-ba6b30a0.js.map +1 -0
- package/dist/node/chunks/{dep-11213a75.js → dep-c4cf6e92.js} +19 -6
- package/dist/node/chunks/dep-c4cf6e92.js.map +1 -0
- package/dist/node/chunks/{dep-eb6ef720.js → dep-d574094c.js} +18 -5
- package/dist/node/chunks/dep-d574094c.js.map +1 -0
- package/dist/node/chunks/{dep-0d2f9464.js → dep-e39b05d6.js} +18 -5
- package/dist/node/chunks/dep-e39b05d6.js.map +1 -0
- package/dist/node/cli.js +60 -23992
- package/dist/node/cli.js.map +1 -1
- package/dist/node/index.d.ts +26 -8
- package/dist/node/index.js +18 -15
- package/dist/node/index.js.map +1 -1
- package/dist/node/terser.js +102 -55
- package/package.json +28 -13
- package/types/package.json +3 -0
- package/api-extractor.json +0 -54
- package/dist/node/chunks/dep-0d2f9464.js.map +0 -1
- package/dist/node/chunks/dep-11213a75.js.map +0 -1
- package/dist/node/chunks/dep-91aa74e8.js.map +0 -1
- package/dist/node/chunks/dep-c7e510f9.js.map +0 -1
- package/dist/node/chunks/dep-e36486f6.js.map +0 -1
- package/dist/node/chunks/dep-eb6ef720.js.map +0 -1
- package/dist/node/terser.js.map +0 -1
- package/rollup.config.js +0 -389
- package/scripts/patchTypes.js +0 -70
- package/src/node/__tests__/asset.spec.ts +0 -156
- package/src/node/__tests__/build.spec.ts +0 -67
- package/src/node/__tests__/config.spec.ts +0 -166
- package/src/node/__tests__/packages/name/package.json +0 -3
- package/src/node/__tests__/packages/noname/package.json +0 -1
- package/src/node/__tests__/plugins/css.spec.ts +0 -116
- package/src/node/__tests__/scan.spec.ts +0 -118
- package/src/node/__tests__/utils.spec.ts +0 -40
- package/src/node/build.ts +0 -783
- package/src/node/cli.ts +0 -239
- package/src/node/config.ts +0 -1033
- package/src/node/constants.ts +0 -87
- package/src/node/importGlob.ts +0 -173
- package/src/node/index.ts +0 -88
- package/src/node/logger.ts +0 -167
- package/src/node/optimizer/esbuildDepPlugin.ts +0 -216
- package/src/node/optimizer/index.ts +0 -410
- package/src/node/optimizer/registerMissing.ts +0 -102
- package/src/node/optimizer/scan.ts +0 -457
- package/src/node/plugin.ts +0 -138
- package/src/node/plugins/asset.ts +0 -365
- package/src/node/plugins/assetImportMetaUrl.ts +0 -99
- package/src/node/plugins/clientInjections.ts +0 -72
- package/src/node/plugins/css.ts +0 -1279
- package/src/node/plugins/dataUri.ts +0 -64
- package/src/node/plugins/define.ts +0 -107
- package/src/node/plugins/esbuild.ts +0 -280
- package/src/node/plugins/html.ts +0 -673
- package/src/node/plugins/importAnalysis.ts +0 -614
- package/src/node/plugins/importAnalysisBuild.ts +0 -334
- package/src/node/plugins/index.ts +0 -69
- package/src/node/plugins/json.ts +0 -75
- package/src/node/plugins/loadFallback.ts +0 -19
- package/src/node/plugins/manifest.ts +0 -123
- package/src/node/plugins/modulePreloadPolyfill.ts +0 -100
- package/src/node/plugins/preAlias.ts +0 -22
- package/src/node/plugins/reporter.ts +0 -244
- package/src/node/plugins/resolve.ts +0 -925
- package/src/node/plugins/terser.ts +0 -40
- package/src/node/plugins/wasm.ts +0 -72
- package/src/node/plugins/worker.ts +0 -117
- package/src/node/preview.ts +0 -82
- package/src/node/server/__tests__/fixtures/none/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/pnpm-workspace.yaml +0 -0
- package/src/node/server/__tests__/fixtures/yarn/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/yarn/package.json +0 -6
- package/src/node/server/__tests__/search-root.spec.ts +0 -31
- package/src/node/server/hmr.ts +0 -489
- package/src/node/server/http.ts +0 -198
- package/src/node/server/index.ts +0 -705
- package/src/node/server/middlewares/base.ts +0 -52
- package/src/node/server/middlewares/error.ts +0 -98
- package/src/node/server/middlewares/indexHtml.ts +0 -170
- package/src/node/server/middlewares/proxy.ts +0 -124
- package/src/node/server/middlewares/spaFallback.ts +0 -32
- package/src/node/server/middlewares/static.ts +0 -153
- package/src/node/server/middlewares/time.ts +0 -18
- package/src/node/server/middlewares/transform.ts +0 -196
- package/src/node/server/moduleGraph.ts +0 -200
- package/src/node/server/openBrowser.ts +0 -101
- package/src/node/server/pluginContainer.ts +0 -546
- package/src/node/server/searchRoot.ts +0 -70
- package/src/node/server/send.ts +0 -54
- package/src/node/server/sourcemap.ts +0 -54
- package/src/node/server/transformRequest.ts +0 -168
- package/src/node/server/ws.ts +0 -131
- package/src/node/ssr/__tests__/ssrTransform.spec.ts +0 -309
- package/src/node/ssr/ssrExternal.ts +0 -161
- package/src/node/ssr/ssrManifestPlugin.ts +0 -53
- package/src/node/ssr/ssrModuleLoader.ts +0 -214
- package/src/node/ssr/ssrStacktrace.ts +0 -75
- package/src/node/ssr/ssrTransform.ts +0 -452
- package/src/node/tsconfig.json +0 -14
- package/src/node/utils.ts +0 -565
- package/tsconfig.base.json +0 -11
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { tryNodeResolve, InternalResolveOptions } from '../plugins/resolve'
|
|
4
|
-
import {
|
|
5
|
-
createDebugger,
|
|
6
|
-
isDefined,
|
|
7
|
-
lookupFile,
|
|
8
|
-
normalizePath,
|
|
9
|
-
resolveFrom,
|
|
10
|
-
unique
|
|
11
|
-
} from '../utils'
|
|
12
|
-
import { ResolvedConfig } from '..'
|
|
13
|
-
import { createFilter } from '@rollup/pluginutils'
|
|
14
|
-
|
|
15
|
-
const debug = createDebugger('vite:ssr-external')
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Heuristics for determining whether a dependency should be externalized for
|
|
19
|
-
* server-side rendering.
|
|
20
|
-
*
|
|
21
|
-
* TODO right now externals are imported using require(), we probably need to
|
|
22
|
-
* rework this when more libraries ship native ESM distributions for Node.
|
|
23
|
-
*/
|
|
24
|
-
export function resolveSSRExternal(
|
|
25
|
-
config: ResolvedConfig,
|
|
26
|
-
knownImports: string[],
|
|
27
|
-
ssrExternals: Set<string> = new Set(),
|
|
28
|
-
seen: Set<string> = new Set()
|
|
29
|
-
): string[] {
|
|
30
|
-
if (config.ssr?.noExternal === true) {
|
|
31
|
-
return []
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const { root } = config
|
|
35
|
-
const pkgContent = lookupFile(root, ['package.json'])
|
|
36
|
-
if (!pkgContent) {
|
|
37
|
-
return []
|
|
38
|
-
}
|
|
39
|
-
const pkg = JSON.parse(pkgContent)
|
|
40
|
-
const importedDeps = knownImports.map(getNpmPackageName).filter(isDefined)
|
|
41
|
-
const deps = unique([
|
|
42
|
-
...importedDeps,
|
|
43
|
-
...Object.keys(pkg.devDependencies || {}),
|
|
44
|
-
...Object.keys(pkg.dependencies || {})
|
|
45
|
-
])
|
|
46
|
-
|
|
47
|
-
const resolveOptions: InternalResolveOptions = {
|
|
48
|
-
root,
|
|
49
|
-
isProduction: false,
|
|
50
|
-
isBuild: true
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const depsToTrace = new Set<string>()
|
|
54
|
-
|
|
55
|
-
for (const id of deps) {
|
|
56
|
-
if (seen.has(id)) {
|
|
57
|
-
continue
|
|
58
|
-
}
|
|
59
|
-
seen.add(id)
|
|
60
|
-
|
|
61
|
-
let entry: string | undefined
|
|
62
|
-
let requireEntry: string
|
|
63
|
-
try {
|
|
64
|
-
entry = tryNodeResolve(
|
|
65
|
-
id,
|
|
66
|
-
undefined,
|
|
67
|
-
resolveOptions,
|
|
68
|
-
true,
|
|
69
|
-
undefined,
|
|
70
|
-
true
|
|
71
|
-
)?.id
|
|
72
|
-
// normalizePath required for windows. tryNodeResolve uses normalizePath
|
|
73
|
-
// which returns with '/', require.resolve returns with '\\'
|
|
74
|
-
requireEntry = normalizePath(require.resolve(id, { paths: [root] }))
|
|
75
|
-
} catch (e) {
|
|
76
|
-
// resolve failed, assume include
|
|
77
|
-
debug(`Failed to resolve entries for package "${id}"\n`, e)
|
|
78
|
-
continue
|
|
79
|
-
}
|
|
80
|
-
if (!entry) {
|
|
81
|
-
// no esm entry but has require entry (is this even possible?)
|
|
82
|
-
ssrExternals.add(id)
|
|
83
|
-
continue
|
|
84
|
-
}
|
|
85
|
-
if (!entry.includes('node_modules')) {
|
|
86
|
-
// entry is not a node dep, possibly linked - don't externalize
|
|
87
|
-
// instead, trace its dependencies.
|
|
88
|
-
depsToTrace.add(id)
|
|
89
|
-
continue
|
|
90
|
-
}
|
|
91
|
-
if (entry !== requireEntry) {
|
|
92
|
-
// has separate esm/require entry, assume require entry is cjs
|
|
93
|
-
ssrExternals.add(id)
|
|
94
|
-
} else {
|
|
95
|
-
// node resolve and esm resolve resolves to the same file.
|
|
96
|
-
if (!/\.m?js$/.test(entry)) {
|
|
97
|
-
// entry is not js, cannot externalize
|
|
98
|
-
continue
|
|
99
|
-
}
|
|
100
|
-
// check if the entry is cjs
|
|
101
|
-
const content = fs.readFileSync(entry, 'utf-8')
|
|
102
|
-
if (/\bmodule\.exports\b|\bexports[.\[]|\brequire\s*\(/.test(content)) {
|
|
103
|
-
ssrExternals.add(id)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
for (const id of depsToTrace) {
|
|
109
|
-
const depRoot = path.dirname(
|
|
110
|
-
resolveFrom(`${id}/package.json`, root, !!config.resolve.preserveSymlinks)
|
|
111
|
-
)
|
|
112
|
-
resolveSSRExternal(
|
|
113
|
-
{
|
|
114
|
-
...config,
|
|
115
|
-
root: depRoot
|
|
116
|
-
},
|
|
117
|
-
knownImports,
|
|
118
|
-
ssrExternals,
|
|
119
|
-
seen
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (config.ssr?.external) {
|
|
124
|
-
config.ssr.external.forEach((id) => ssrExternals.add(id))
|
|
125
|
-
}
|
|
126
|
-
let externals = [...ssrExternals]
|
|
127
|
-
if (config.ssr?.noExternal) {
|
|
128
|
-
const filter = createFilter(undefined, config.ssr.noExternal, {
|
|
129
|
-
resolve: false
|
|
130
|
-
})
|
|
131
|
-
externals = externals.filter((id) => filter(id))
|
|
132
|
-
}
|
|
133
|
-
return externals.filter((id) => id !== 'vite')
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function shouldExternalizeForSSR(
|
|
137
|
-
id: string,
|
|
138
|
-
externals: string[]
|
|
139
|
-
): boolean {
|
|
140
|
-
const should = externals.some((e) => {
|
|
141
|
-
if (id === e) {
|
|
142
|
-
return true
|
|
143
|
-
}
|
|
144
|
-
// deep imports, check ext before externalizing - only externalize
|
|
145
|
-
// extension-less imports and explicit .js imports
|
|
146
|
-
if (id.startsWith(e + '/') && (!path.extname(id) || id.endsWith('.js'))) {
|
|
147
|
-
return true
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
return should
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function getNpmPackageName(importPath: string): string | null {
|
|
154
|
-
const parts = importPath.split('/')
|
|
155
|
-
if (parts[0].startsWith('@')) {
|
|
156
|
-
if (!parts[1]) return null
|
|
157
|
-
return `${parts[0]}/${parts[1]}`
|
|
158
|
-
} else {
|
|
159
|
-
return parts[0]
|
|
160
|
-
}
|
|
161
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { relative } from 'path'
|
|
2
|
-
import { normalizePath } from '@rollup/pluginutils'
|
|
3
|
-
import { ResolvedConfig } from '..'
|
|
4
|
-
import { Plugin } from '../plugin'
|
|
5
|
-
import { chunkToEmittedCssFileMap } from '../plugins/css'
|
|
6
|
-
import { chunkToEmittedAssetsMap } from '../plugins/asset'
|
|
7
|
-
|
|
8
|
-
export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
|
|
9
|
-
// module id => preload assets mapping
|
|
10
|
-
const ssrManifest: Record<string, string[]> = {}
|
|
11
|
-
const base = config.base
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
name: 'vite:ssr-manifest',
|
|
15
|
-
generateBundle(_options, bundle) {
|
|
16
|
-
for (const file in bundle) {
|
|
17
|
-
const chunk = bundle[file]
|
|
18
|
-
if (chunk.type === 'chunk') {
|
|
19
|
-
// links for certain entry chunks are already generated in static HTML
|
|
20
|
-
// in those cases we only need to record info for non-entry chunks
|
|
21
|
-
const cssFiles = chunk.isEntry
|
|
22
|
-
? null
|
|
23
|
-
: chunkToEmittedCssFileMap.get(chunk)
|
|
24
|
-
const assetFiles = chunkToEmittedAssetsMap.get(chunk)
|
|
25
|
-
for (const id in chunk.modules) {
|
|
26
|
-
const normalizedId = normalizePath(relative(config.root, id))
|
|
27
|
-
const mappedChunks =
|
|
28
|
-
ssrManifest[normalizedId] || (ssrManifest[normalizedId] = [])
|
|
29
|
-
if (!chunk.isEntry) {
|
|
30
|
-
mappedChunks.push(base + chunk.fileName)
|
|
31
|
-
}
|
|
32
|
-
if (cssFiles) {
|
|
33
|
-
cssFiles.forEach((file) => {
|
|
34
|
-
mappedChunks.push(base + file)
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
if (assetFiles) {
|
|
38
|
-
assetFiles.forEach((file) => {
|
|
39
|
-
mappedChunks.push(base + file)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.emitFile({
|
|
47
|
-
fileName: 'ssr-manifest.json',
|
|
48
|
-
type: 'asset',
|
|
49
|
-
source: JSON.stringify(ssrManifest, null, 2)
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { ViteDevServer } from '..'
|
|
4
|
-
import { cleanUrl, resolveFrom, unwrapId } from '../utils'
|
|
5
|
-
import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace'
|
|
6
|
-
import {
|
|
7
|
-
ssrExportAllKey,
|
|
8
|
-
ssrModuleExportsKey,
|
|
9
|
-
ssrImportKey,
|
|
10
|
-
ssrImportMetaKey,
|
|
11
|
-
ssrDynamicImportKey
|
|
12
|
-
} from './ssrTransform'
|
|
13
|
-
import { transformRequest } from '../server/transformRequest'
|
|
14
|
-
|
|
15
|
-
interface SSRContext {
|
|
16
|
-
global: NodeJS.Global
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
type SSRModule = Record<string, any>
|
|
20
|
-
|
|
21
|
-
const pendingModules = new Map<string, Promise<SSRModule>>()
|
|
22
|
-
const pendingImports = new Map<string, string[]>()
|
|
23
|
-
|
|
24
|
-
export async function ssrLoadModule(
|
|
25
|
-
url: string,
|
|
26
|
-
server: ViteDevServer,
|
|
27
|
-
context: SSRContext = { global },
|
|
28
|
-
urlStack: string[] = []
|
|
29
|
-
): Promise<SSRModule> {
|
|
30
|
-
url = unwrapId(url)
|
|
31
|
-
|
|
32
|
-
// when we instantiate multiple dependency modules in parallel, they may
|
|
33
|
-
// point to shared modules. We need to avoid duplicate instantiation attempts
|
|
34
|
-
// by register every module as pending synchronously so that all subsequent
|
|
35
|
-
// request to that module are simply waiting on the same promise.
|
|
36
|
-
const pending = pendingModules.get(url)
|
|
37
|
-
if (pending) {
|
|
38
|
-
return pending
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const modulePromise = instantiateModule(url, server, context, urlStack)
|
|
42
|
-
pendingModules.set(url, modulePromise)
|
|
43
|
-
modulePromise
|
|
44
|
-
.catch(() => {
|
|
45
|
-
pendingImports.delete(url)
|
|
46
|
-
})
|
|
47
|
-
.finally(() => {
|
|
48
|
-
pendingModules.delete(url)
|
|
49
|
-
})
|
|
50
|
-
return modulePromise
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function instantiateModule(
|
|
54
|
-
url: string,
|
|
55
|
-
server: ViteDevServer,
|
|
56
|
-
context: SSRContext = { global },
|
|
57
|
-
urlStack: string[] = []
|
|
58
|
-
): Promise<SSRModule> {
|
|
59
|
-
const { moduleGraph } = server
|
|
60
|
-
const mod = await moduleGraph.ensureEntryFromUrl(url)
|
|
61
|
-
|
|
62
|
-
if (mod.ssrModule) {
|
|
63
|
-
return mod.ssrModule
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const result =
|
|
67
|
-
mod.ssrTransformResult ||
|
|
68
|
-
(await transformRequest(url, server, { ssr: true }))
|
|
69
|
-
if (!result) {
|
|
70
|
-
// TODO more info? is this even necessary?
|
|
71
|
-
throw new Error(`failed to load module for ssr: ${url}`)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const ssrModule = {
|
|
75
|
-
[Symbol.toStringTag]: 'Module'
|
|
76
|
-
}
|
|
77
|
-
Object.defineProperty(ssrModule, '__esModule', { value: true })
|
|
78
|
-
|
|
79
|
-
// Tolerate circular imports by ensuring the module can be
|
|
80
|
-
// referenced before it's been instantiated.
|
|
81
|
-
mod.ssrModule = ssrModule
|
|
82
|
-
|
|
83
|
-
const ssrImportMeta = { url }
|
|
84
|
-
|
|
85
|
-
urlStack = urlStack.concat(url)
|
|
86
|
-
const isCircular = (url: string) => urlStack.includes(url)
|
|
87
|
-
|
|
88
|
-
// Since dynamic imports can happen in parallel, we need to
|
|
89
|
-
// account for multiple pending deps and duplicate imports.
|
|
90
|
-
const pendingDeps: string[] = []
|
|
91
|
-
|
|
92
|
-
const ssrImport = async (dep: string) => {
|
|
93
|
-
if (dep[0] !== '.' && dep[0] !== '/') {
|
|
94
|
-
return nodeRequire(
|
|
95
|
-
dep,
|
|
96
|
-
mod.file,
|
|
97
|
-
server.config.root,
|
|
98
|
-
!!server.config.resolve.preserveSymlinks
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
dep = unwrapId(dep)
|
|
102
|
-
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
|
|
103
|
-
pendingDeps.push(dep)
|
|
104
|
-
if (pendingDeps.length === 1) {
|
|
105
|
-
pendingImports.set(url, pendingDeps)
|
|
106
|
-
}
|
|
107
|
-
await ssrLoadModule(dep, server, context, urlStack)
|
|
108
|
-
if (pendingDeps.length === 1) {
|
|
109
|
-
pendingImports.delete(url)
|
|
110
|
-
} else {
|
|
111
|
-
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const ssrDynamicImport = (dep: string) => {
|
|
118
|
-
// #3087 dynamic import vars is ignored at rewrite import path,
|
|
119
|
-
// so here need process relative path
|
|
120
|
-
if (dep[0] === '.') {
|
|
121
|
-
dep = path.posix.resolve(path.dirname(url), dep)
|
|
122
|
-
}
|
|
123
|
-
return ssrImport(dep)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function ssrExportAll(sourceModule: any) {
|
|
127
|
-
for (const key in sourceModule) {
|
|
128
|
-
if (key !== 'default') {
|
|
129
|
-
Object.defineProperty(ssrModule, key, {
|
|
130
|
-
enumerable: true,
|
|
131
|
-
configurable: true,
|
|
132
|
-
get() {
|
|
133
|
-
return sourceModule[key]
|
|
134
|
-
}
|
|
135
|
-
})
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
142
|
-
const AsyncFunction = async function () {}.constructor as typeof Function
|
|
143
|
-
const initModule = new AsyncFunction(
|
|
144
|
-
`global`,
|
|
145
|
-
ssrModuleExportsKey,
|
|
146
|
-
ssrImportMetaKey,
|
|
147
|
-
ssrImportKey,
|
|
148
|
-
ssrDynamicImportKey,
|
|
149
|
-
ssrExportAllKey,
|
|
150
|
-
result.code + `\n//# sourceURL=${mod.url}`
|
|
151
|
-
)
|
|
152
|
-
await initModule(
|
|
153
|
-
context.global,
|
|
154
|
-
ssrModule,
|
|
155
|
-
ssrImportMeta,
|
|
156
|
-
ssrImport,
|
|
157
|
-
ssrDynamicImport,
|
|
158
|
-
ssrExportAll
|
|
159
|
-
)
|
|
160
|
-
} catch (e) {
|
|
161
|
-
const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph)
|
|
162
|
-
rebindErrorStacktrace(e, stacktrace)
|
|
163
|
-
server.config.logger.error(
|
|
164
|
-
`Error when evaluating SSR module ${url}:\n${stacktrace}`,
|
|
165
|
-
{
|
|
166
|
-
timestamp: true,
|
|
167
|
-
clear: server.config.clearScreen,
|
|
168
|
-
error: e
|
|
169
|
-
}
|
|
170
|
-
)
|
|
171
|
-
throw e
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return Object.freeze(ssrModule)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function nodeRequire(
|
|
178
|
-
id: string,
|
|
179
|
-
importer: string | null,
|
|
180
|
-
root: string,
|
|
181
|
-
preserveSymlinks: boolean
|
|
182
|
-
) {
|
|
183
|
-
const mod = require(resolve(id, importer, root, preserveSymlinks))
|
|
184
|
-
const defaultExport = mod.__esModule ? mod.default : mod
|
|
185
|
-
// rollup-style default import interop for cjs
|
|
186
|
-
return new Proxy(mod, {
|
|
187
|
-
get(mod, prop) {
|
|
188
|
-
if (prop === 'default') return defaultExport
|
|
189
|
-
return mod[prop]
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const resolveCache = new Map<string, string>()
|
|
195
|
-
|
|
196
|
-
function resolve(
|
|
197
|
-
id: string,
|
|
198
|
-
importer: string | null,
|
|
199
|
-
root: string,
|
|
200
|
-
preserveSymlinks: boolean
|
|
201
|
-
) {
|
|
202
|
-
const key = id + importer + root
|
|
203
|
-
const cached = resolveCache.get(key)
|
|
204
|
-
if (cached) {
|
|
205
|
-
return cached
|
|
206
|
-
}
|
|
207
|
-
const resolveDir =
|
|
208
|
-
importer && fs.existsSync(cleanUrl(importer))
|
|
209
|
-
? path.dirname(importer)
|
|
210
|
-
: root
|
|
211
|
-
const resolved = resolveFrom(id, resolveDir, preserveSymlinks, true)
|
|
212
|
-
resolveCache.set(key, resolved)
|
|
213
|
-
return resolved
|
|
214
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { SourceMapConsumer, RawSourceMap } from 'source-map'
|
|
2
|
-
import { ModuleGraph } from '../server/moduleGraph'
|
|
3
|
-
|
|
4
|
-
let offset: number
|
|
5
|
-
try {
|
|
6
|
-
new Function('throw new Error(1)')()
|
|
7
|
-
} catch (e) {
|
|
8
|
-
// in Node 12, stack traces account for the function wrapper.
|
|
9
|
-
// in Node 13 and later, the function wrapper adds two lines,
|
|
10
|
-
// which must be subtracted to generate a valid mapping
|
|
11
|
-
const match = /:(\d+):\d+\)$/.exec(e.stack.split('\n')[1])
|
|
12
|
-
offset = match ? +match[1] - 1 : 0
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function ssrRewriteStacktrace(
|
|
16
|
-
stack: string,
|
|
17
|
-
moduleGraph: ModuleGraph
|
|
18
|
-
): string {
|
|
19
|
-
return stack
|
|
20
|
-
.split('\n')
|
|
21
|
-
.map((line) => {
|
|
22
|
-
return line.replace(
|
|
23
|
-
/^ {4}at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?)\)?/,
|
|
24
|
-
(input, varName, url, line, column) => {
|
|
25
|
-
if (!url) return input
|
|
26
|
-
|
|
27
|
-
const mod = moduleGraph.urlToModuleMap.get(url)
|
|
28
|
-
const rawSourceMap = mod?.ssrTransformResult?.map
|
|
29
|
-
|
|
30
|
-
if (!rawSourceMap) {
|
|
31
|
-
return input
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const consumer = new SourceMapConsumer(
|
|
35
|
-
rawSourceMap as unknown as RawSourceMap
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
const pos = consumer.originalPositionFor({
|
|
39
|
-
line: Number(line) - offset,
|
|
40
|
-
column: Number(column),
|
|
41
|
-
bias: SourceMapConsumer.LEAST_UPPER_BOUND
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
if (!pos.source) {
|
|
45
|
-
return input
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const source = `${pos.source}:${pos.line || 0}:${pos.column || 0}`
|
|
49
|
-
if (!varName || varName === 'eval') {
|
|
50
|
-
return ` at ${source}`
|
|
51
|
-
} else {
|
|
52
|
-
return ` at ${varName} (${source})`
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
)
|
|
56
|
-
})
|
|
57
|
-
.join('\n')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function rebindErrorStacktrace(e: Error, stacktrace: string): void {
|
|
61
|
-
const { configurable, writable } = Object.getOwnPropertyDescriptor(
|
|
62
|
-
e,
|
|
63
|
-
'stack'
|
|
64
|
-
)!
|
|
65
|
-
if (configurable) {
|
|
66
|
-
Object.defineProperty(e, 'stack', {
|
|
67
|
-
value: stacktrace,
|
|
68
|
-
enumerable: true,
|
|
69
|
-
configurable: true,
|
|
70
|
-
writable: true
|
|
71
|
-
})
|
|
72
|
-
} else if (writable) {
|
|
73
|
-
e.stack = stacktrace
|
|
74
|
-
}
|
|
75
|
-
}
|