-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvite.config.js
More file actions
166 lines (152 loc) · 5.08 KB
/
vite.config.js
File metadata and controls
166 lines (152 loc) · 5.08 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
156
157
158
159
160
161
162
163
164
165
166
import path from 'node:path'
import process from 'node:process'
import fs from 'node:fs'
import { defineConfig } from 'vite'
import { plugin as mdPlugin, Mode } from 'vite-plugin-markdown'
import { parse as parseYaml } from 'yaml'
// Read and parse the CV markdown file
const cvPath = path.resolve('src/cv.md')
const cvContent = fs.readFileSync(cvPath, 'utf-8')
// Extract YAML frontmatter - matches content between opening and closing '---' delimiters
const frontMatterMatch = cvContent.match(/^---\n([\s\S]*?)\n---/)
const cvData = frontMatterMatch ? parseYaml(frontMatterMatch[1]) : {}
/**
* Creates a sanitized base path from a repository or project name.
* Used for configuring Vite's base URL in different deployment environments.
*
* @param {string} name - Repository or project name to convert into a path
* @returns {string} Sanitized path starting and ending with '/', or just '/' if invalid input
* @example
* createBasePath('my-repo') // returns '/my-repo/'
* createBasePath('my/repo') // returns '/myrepo/'
* createBasePath('') // returns '/'
*/
const createBasePath = (name) => {
if (!name) return '/'
// Remove any special characters that might cause issues in URLs
const sanitizedName = name.replace(/[^\w-]/g, '')
return sanitizedName ? `/${sanitizedName}/` : '/'
}
/**
* Determines the appropriate base path for Vite based on the deployment environment.
* Supports GitHub Actions and GitLab CI deployments, with a fallback for local development.
*
* Environment variables used:
* - GITHUB_ACTIONS: Present when running in GitHub Actions
* - GITHUB_REPOSITORY: Format "username/repo-name"
* - CI_PROJECT_PATH: GitLab CI project path
*
* References:
* https://vite.dev/guide/static-deploy.html#github-pages
* https://vite.dev/guide/static-deploy.html#gitlab-pages-and-gitlab-ci
* https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
* https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
*
* @returns {string} Base path for Vite configuration
* @example
* // In GitHub Actions with GITHUB_REPOSITORY="user/my-repo"
* getBasePathFromEnv() // returns '/my-repo/'
*
* // In GitLab CI with CI_PROJECT_PATH="group/my-project"
* getBasePathFromEnv() // returns '/my-project/'
*
* // In local development
* getBasePathFromEnv() // returns '/'
*/
const getBasePathFromEnv = () => {
if (process.env.GITHUB_ACTIONS && process.env.GITHUB_REPOSITORY) {
const [, repoName] = process.env.GITHUB_REPOSITORY.split('/')
return createBasePath(repoName)
}
if (process.env.CI_PROJECT_PATH) {
const projectName = process.env.CI_PROJECT_PATH.split('/').pop()
return createBasePath(projectName)
}
return '/'
}
/**
* Determines the full base URL for the deployed application.
* Handles multiple deployment scenarios:
* - GitHub Pages
* - GitLab Pages
* - Custom domain from package.json
* - Vercel
* - Netlify
* - Local development
*
* @returns {string} The complete base URL for the deployed application
*/
function getBaseUrl() {
// Development environment
if (process.env.NODE_ENV === 'development') {
return 'http://localhost:5173/'
}
// GitHub Pages
if (process.env.GITHUB_ACTIONS) {
return `https://${process.env.GITHUB_REPOSITORY_OWNER}.github.io${getBasePathFromEnv()}`
}
// GitLab Pages
if (process.env.CI_PROJECT_PATH) {
return `https://${process.env.CI_PROJECT_ROOT_NAMESPACE}.gitlab.io${getBasePathFromEnv()}`
}
// Vercel
if (process.env.VERCEL_URL) {
return `https://${process.env.VERCEL_URL}`
}
// Netlify
if (process.env.NETLIFY) {
return process.env.URL || `https://${process.env.NETLIFY_SITE_NAME}.netlify.app`
}
// Try to get URL from package.json homepage
try {
const pkg = JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf-8'))
if (pkg.homepage) {
// Ensure homepage ends with a trailing slash
return pkg.homepage.endsWith('/') ? pkg.homepage : `${pkg.homepage}/`
}
} catch (error) {
console.warn('Could not read package.json homepage:', error.message)
}
// Production fallback - should be overridden by deployment platform or package.json
console.warn('Could not determine base URL. Meta tags may not work correctly.')
return '/'
}
export default defineConfig({
base: getBasePathFromEnv(),
server: {
open: process.env.NODE_ENV === 'development' ? 'index.html' : false,
},
root: 'src',
publicDir: '../public',
build: {
outDir: '../dist',
emptyOutDir: true,
},
resolve: {
alias: { '/src': path.resolve(process.cwd(), 'src') },
},
plugins: [
mdPlugin({
mode: [Mode.MARKDOWN],
}),
{
name: 'html-transform',
transformIndexHtml(html) {
return html
.replace(/%__CV_DATA__\.(\w+)%/g, (_, key) => cvData[key])
.replace(/%__BASE_URL__%/g, getBaseUrl())
},
},
],
test: {
include: ['../tests/**/*.test.js'],
coverage: {
all: true,
provider: 'v8',
reportsDirectory: '../coverage',
},
environment: 'jsdom',
globals: true,
watch: false,
},
})