Repository navigation
Expand file tree
/
Copy pathpatch-vite-config.ts
More file actions
98 lines (90 loc) · 3.07 KB
/
Copy pathpatch-vite-config.ts
File metadata and controls
98 lines (90 loc) · 3.07 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
/**
* Extend the pinned Vite resolver without replacing its config-loading pipeline.
* Keep this transform free of dependencies: branding also runs before installation.
*/
const upstreamSignature = `export async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
defaultMode = 'development',
defaultNodeEnv = 'development',
isPreview = false,
/** @internal */
patchConfig?: (config: ResolvedConfig) => void,
/** @internal */
patchPlugins?: (resolvedPlugins: Plugin[]) => void,
): Promise<ResolvedConfig> {`;
const patchedSignature = upstreamSignature.replace(
'): Promise<ResolvedConfig> {',
` /** @internal */
internalOptions?: {
rootBase?: string
},
): Promise<ResolvedConfig> {`,
);
const upstreamLoadResult = ` if (loadResult) {
config = mergeConfig(loadResult.config, config)
configFile = loadResult.path
configFileDependencies = loadResult.dependencies
}`;
const patchedLoadResult = upstreamLoadResult.replace(
'\n }',
`
if (internalOptions?.rootBase !== undefined && inlineConfig.root == null) {
config.root = path.resolve(internalOptions.rootBase, config.root ?? '')
}
}`,
);
function patchError(detail: string): Error {
return new Error(
`[patch-vite-config] ${detail} in vite/packages/vite/src/node/config.ts. ` +
'Check the Vite pin in packages/tools/.upstream-versions.json and update patch-vite-config.ts.',
);
}
function count(source: string, anchor: string): number {
return source.split(anchor).length - 1;
}
function patchState(source: string): 'upstream' | 'patched' {
const upstreamSignatures = count(source, upstreamSignature);
const patchedSignatures = count(source, patchedSignature);
const upstreamLoads = count(source, upstreamLoadResult);
const patchedLoads = count(source, patchedLoadResult);
if (
upstreamSignatures === 1 &&
upstreamLoads === 1 &&
patchedSignatures === 0 &&
patchedLoads === 0 &&
!/\b(?:internalOptions|rootBase)\b/.test(source)
) {
return 'upstream';
}
if (
patchedSignatures === 1 &&
patchedLoads === 1 &&
upstreamSignatures === 0 &&
upstreamLoads === 0 &&
source.match(/\binternalOptions\b/g)?.length === 3 &&
source.match(/\brootBase\b/g)?.length === 3
) {
return 'patched';
}
throw patchError(
'Expected one complete upstream or patched resolver; found changed, duplicate, or partial patch anchors',
);
}
/** Validate the complete patch before a build can produce a core that ignores it. */
export function assertViteConfigPatched(source: string): void {
if (patchState(source) !== 'patched') {
throw patchError('The root base resolution patch has not been applied');
}
}
/** Apply both edits together; reject upstream drift and incomplete prior edits. */
export function patchViteConfig(source: string): string {
if (patchState(source) === 'patched') {
return source;
}
const patched = source
.replace(upstreamSignature, patchedSignature)
.replace(upstreamLoadResult, patchedLoadResult);
assertViteConfigPatched(patched);
return patched;
}