|
1 | | -import { readFileSync } from 'node:fs' |
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { fileURLToPath } from 'node:url' |
2 | 3 | import { defineConfig } from 'vite' |
3 | 4 | import react from '@vitejs/plugin-react' |
4 | 5 | import istanbul from 'vite-plugin-istanbul' |
5 | 6 | import * as path from 'path' |
6 | 7 |
|
7 | | -const nodeEnv = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process |
8 | | - ?.env |
| 8 | +const sharedBuildModuleUrl = [ |
| 9 | + new URL('./build/spaVersionMetadata.mjs', import.meta.url), |
| 10 | + new URL('../build/spaVersionMetadata.mjs', import.meta.url), |
| 11 | + new URL('../../build/spaVersionMetadata.mjs', import.meta.url) |
| 12 | +].find((candidate) => existsSync(fileURLToPath(candidate))) |
9 | 13 |
|
10 | | -function readTextFile(filePath: string): string | undefined { |
11 | | - try { |
12 | | - return readFileSync(filePath, 'utf8').trim() || undefined |
13 | | - } catch { |
14 | | - return undefined |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -function resolveSpaVersionDisplay({ |
19 | | - packageVersion, |
20 | | - commitSha, |
21 | | - exactTag, |
22 | | - refName, |
23 | | - refType |
24 | | -}: { |
25 | | - packageVersion?: string |
26 | | - commitSha?: string |
27 | | - exactTag?: string |
28 | | - refName?: string |
29 | | - refType?: string |
30 | | -}): string { |
31 | | - const releaseTag = refType === 'tag' ? refName : exactTag |
32 | | - const normalizedReleaseTag = releaseTag?.replace(/^refs\/tags\//, '').replace(/^v/, '') |
33 | | - |
34 | | - if (packageVersion && normalizedReleaseTag === packageVersion) { |
35 | | - return packageVersion |
36 | | - } |
37 | | - |
38 | | - if (commitSha) { |
39 | | - return commitSha |
40 | | - } |
41 | | - |
42 | | - return packageVersion ?? 'unknown' |
43 | | -} |
44 | | - |
45 | | -function resolveProjectRoot(configDir: string): string { |
46 | | - const candidates = [configDir, path.resolve(configDir, '..'), path.resolve(configDir, '../..')] |
47 | | - |
48 | | - const projectRoot = candidates.find((candidate) => { |
49 | | - return Boolean(readTextFile(path.join(candidate, 'package.json'))) && Boolean(readTextFile(path.join(candidate, '.git'))) |
50 | | - }) |
51 | | - |
52 | | - return projectRoot ?? configDir |
53 | | -} |
54 | | - |
55 | | -function readPackageVersion(projectRoot: string): string | undefined { |
56 | | - try { |
57 | | - const packageJson = JSON.parse(readFileSync(path.join(projectRoot, 'package.json'), 'utf8')) as { |
58 | | - version?: string |
59 | | - } |
60 | | - |
61 | | - return packageJson.version |
62 | | - } catch { |
63 | | - return undefined |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -function resolveGitDir(projectRoot: string): string | undefined { |
68 | | - const dotGitPath = path.join(projectRoot, '.git') |
69 | | - const dotGitContent = readTextFile(dotGitPath) |
70 | | - |
71 | | - if (dotGitContent?.startsWith('gitdir: ')) { |
72 | | - return path.resolve(projectRoot, dotGitContent.slice('gitdir: '.length)) |
73 | | - } |
74 | | - |
75 | | - return dotGitContent ? undefined : dotGitPath |
| 14 | +if (!sharedBuildModuleUrl) { |
| 15 | + throw new Error('Could not locate build/spaVersionMetadata.mjs') |
76 | 16 | } |
77 | 17 |
|
78 | | -function readPackedRef(gitDir: string, ref: string): string | undefined { |
79 | | - const packedRefs = readTextFile(path.join(gitDir, 'packed-refs')) |
80 | | - |
81 | | - if (!packedRefs) { |
82 | | - return undefined |
83 | | - } |
84 | | - |
85 | | - for (const line of packedRefs.split('\n')) { |
86 | | - if (!line || line.startsWith('#') || line.startsWith('^')) { |
87 | | - continue |
88 | | - } |
89 | | - |
90 | | - const [sha, packedRef] = line.split(' ') |
91 | | - if (packedRef === ref) { |
92 | | - return sha |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - return undefined |
97 | | -} |
98 | | - |
99 | | -function readGitHeadInfo(projectRoot: string): { commitSha?: string; exactTag?: string } { |
100 | | - const gitDir = resolveGitDir(projectRoot) |
101 | | - |
102 | | - if (!gitDir) { |
103 | | - return {} |
104 | | - } |
105 | | - |
106 | | - const head = readTextFile(path.join(gitDir, 'HEAD')) |
107 | | - |
108 | | - if (!head) { |
109 | | - return {} |
110 | | - } |
111 | | - |
112 | | - if (!head.startsWith('ref: ')) { |
113 | | - return { commitSha: head.slice(0, 9) } |
114 | | - } |
115 | | - |
116 | | - const ref = head.slice('ref: '.length) |
117 | | - const commitSha = readTextFile(path.join(gitDir, ref)) ?? readPackedRef(gitDir, ref) |
118 | | - const exactTag = ref.startsWith('refs/tags/') ? ref.replace('refs/tags/', '') : undefined |
119 | | - |
120 | | - return { |
121 | | - commitSha: commitSha?.slice(0, 9), |
122 | | - exactTag |
123 | | - } |
| 18 | +const { createSpaVersionDefines, resolveProjectRoot } = (await import(sharedBuildModuleUrl.href)) as { |
| 19 | + createSpaVersionDefines: (projectRoot: string) => Record<string, string> |
| 20 | + resolveProjectRoot: (configDir: string) => string |
124 | 21 | } |
125 | 22 |
|
126 | 23 | const projectRoot = resolveProjectRoot(__dirname) |
127 | | -const packageVersion = readPackageVersion(projectRoot) |
128 | | -const gitHeadInfo = readGitHeadInfo(projectRoot) |
129 | | -const shortCommitSha = nodeEnv?.GITHUB_SHA?.slice(0, 9) ?? gitHeadInfo.commitSha |
130 | | -const exactTag = |
131 | | - (nodeEnv?.GITHUB_REF_TYPE === 'tag' ? nodeEnv.GITHUB_REF_NAME : undefined) ?? gitHeadInfo.exactTag |
132 | | -const spaDisplayVersion = resolveSpaVersionDisplay({ |
133 | | - packageVersion, |
134 | | - commitSha: shortCommitSha, |
135 | | - exactTag, |
136 | | - refName: nodeEnv?.GITHUB_REF_NAME, |
137 | | - refType: nodeEnv?.GITHUB_REF_TYPE |
138 | | -}) |
139 | 24 |
|
140 | 25 | export default defineConfig({ |
141 | 26 | root: projectRoot, |
142 | | - define: { |
143 | | - 'import.meta.env.VITE_APP_VERSION': JSON.stringify(packageVersion), |
144 | | - 'import.meta.env.VITE_COMMIT_SHA_SHORT': JSON.stringify(shortCommitSha), |
145 | | - 'import.meta.env.VITE_GIT_EXACT_TAG': JSON.stringify(exactTag), |
146 | | - 'import.meta.env.VITE_GITHUB_REF_NAME': JSON.stringify(nodeEnv?.GITHUB_REF_NAME), |
147 | | - 'import.meta.env.VITE_GITHUB_REF_TYPE': JSON.stringify(nodeEnv?.GITHUB_REF_TYPE), |
148 | | - 'import.meta.env.VITE_SPA_DISPLAY_VERSION': JSON.stringify(spaDisplayVersion) |
149 | | - }, |
| 27 | + define: createSpaVersionDefines(projectRoot), |
150 | 28 | plugins: [ |
151 | 29 | react(), |
152 | 30 | istanbul({ |
|
0 commit comments