-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathutils.ts
More file actions
155 lines (146 loc) · 4.14 KB
/
Copy pathutils.ts
File metadata and controls
155 lines (146 loc) · 4.14 KB
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
import { createHash } from 'node:crypto'
import path from 'node:path'
import { exactRegex } from '@rolldown/pluginutils'
import {
normalizePath,
type Plugin,
type ResolvedConfig,
type Rollup,
} from 'vite'
export function sortObject<T extends object>(o: T) {
return Object.fromEntries(
Object.entries(o).sort(([a], [b]) => a.localeCompare(b)),
) as T
}
// Rethrow transform error through `this.error` with `error.pos`
export function withRollupError<F extends (...args: any[]) => any>(
ctx: Rollup.TransformPluginContext,
f: F,
): F {
function processError(e: any): never {
if (e && typeof e === 'object' && typeof e.pos === 'number') {
return ctx.error(e, e.pos)
}
throw e
}
return function (this: any, ...args: any[]) {
try {
const result = f.apply(this, args)
if (result instanceof Promise) {
return result.catch((e: any) => processError(e))
}
return result
} catch (e: any) {
processError(e)
}
} as F
}
export function createVirtualPlugin(
name: string,
load: Plugin['load'],
): Plugin {
const virtualId = 'virtual:' + name
const resolvedId = '\0' + virtualId
return {
name: `rsc:virtual-${name}`,
resolveId: {
filter: { id: exactRegex(virtualId) },
handler(source) {
if (source === virtualId) {
return resolvedId
}
},
},
load: {
filter: { id: exactRegex(resolvedId) },
handler(id, options) {
if (id === resolvedId) {
return (load as Function).apply(this, [id, options])
}
},
},
}
}
export function normalizeRelativePath(s: string): string {
s = normalizePath(s)
return s[0] === '.' ? s : './' + s
}
export function getEntrySource(
config: Pick<ResolvedConfig, 'build'>,
name?: string,
): string {
const input = config.build.rollupOptions.input
if (!name) {
return getFallbackRollupEntry(input).source
}
if (
typeof input === 'object' &&
!Array.isArray(input) &&
name in input &&
typeof input[name] === 'string'
) {
return input[name]
}
throw new Error(
`[vite-rsc:getEntrySource] expected 'build.rollupOptions.input' to be an object with a '${name}' property that is a string, but got ${JSON.stringify(input)}`,
)
}
export function getFallbackRollupEntry(
input: Rollup.InputOptions['input'] = {},
): {
name: string
source: string
} {
const inputEntries = Object.entries(normalizeRollupOptionsInput(input))
if (inputEntries.length === 1) {
const [name, source] = inputEntries[0]!
return { name, source }
}
throw new Error(
`[vite-rsc] cannot determine fallback entry name from multiple entries, please specify the entry name explicitly`,
)
}
// normalize to object form
// https://rollupjs.org/configuration-options/#input
// https://rollupjs.org/configuration-options/#output-entryfilenames
export function normalizeRollupOptionsInput(
input: Rollup.InputOptions['input'] = {},
): Record<string, string> {
if (typeof input === 'string') {
input = [input]
}
if (Array.isArray(input)) {
return Object.fromEntries(
input.map((file) => [
path.basename(file).slice(0, -path.extname(file).length),
file,
]),
)
}
return input
}
export function hashString(v: string): string {
return createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
}
// normalize server entry exports to align with server runtimes
// https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/
// https://srvx.h3.dev/guide
// https://vercel.com/docs/functions/functions-api-reference?framework=other#fetch-web-standard
// https://github.com/jacob-ebey/rsbuild-rsc-playground/blob/eb1a54afa49cbc5ff93c315744d7754d5ed63498/plugin/fetch-server.ts#L59-L79
export function getFetchHandlerExport(exports: object): any {
if ('default' in exports) {
const default_ = exports.default
if (
default_ &&
typeof default_ === 'object' &&
'fetch' in default_ &&
typeof default_.fetch === 'function'
) {
return default_.fetch
}
if (typeof default_ === 'function') {
return default_
}
}
throw new Error('Invalid server handler entry')
}