-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.go
316 lines (269 loc) · 6.65 KB
/
utils.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package vueglue
import (
"embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"regexp"
)
type PackageJSON struct {
Name string `json:"name"`
Version string `json:"version"`
Type string `json:"type"`
Scripts map[string]string `json:"scripts"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}
type JSAppParams struct {
JSHash string `json:"hash"`
ViteVersion string `json:"vite_version"`
ViteMajorVer string `json:"vite_major_version"`
PackageType string `json:"package_type"`
MajorVer string `json:"major_version,omitempty"`
EntryPoint string `json:"entry_point"`
HasTypeScript bool `json:"has_ts"`
IsVanilla bool `json:"is_vanilla,omitempty"`
VueVersion string `json:"vue_version,omitempty"`
ReactVersion string `json:"react_version,omitempty"`
PreactVersion string `json:"preact_version,omitempty"`
SvelteVersion string `json:"svelte_version,omitempty"`
LitVersion string `json:"lit_version,omitempty"`
}
func (vc *ViteConfig) parsePackageJSON() (*PackageJSON, error) {
// If not set, try and find package.json
path := ""
if _, ok := vc.FS.(embed.FS); ok {
path = vc.JSProjectPath + "/"
}
buf, err := fs.ReadFile(vc.FS, path+"package.json")
if err != nil {
return nil, err
}
content := PackageJSON{}
err = json.Unmarshal(buf, &content)
if err != nil {
return nil, err
}
return &content, nil
}
func analyzePackageJSON(pkgJSON *PackageJSON) *JSAppParams {
semVer := regexp.MustCompile(`^[\^]*((\d+)\.\d+\.\d+)$`)
// parse for a ver; return the full version,
// and the major version. Empty strings if
// the version does not fit our regexp.
getSemVer := func(verStr string) (string, string) {
matches := semVer.FindStringSubmatch(verStr)
var major string
var fullVers string
if matches != nil {
major = matches[2]
fullVers = matches[1]
}
return major, fullVers
}
output := JSAppParams{}
// Is this actually a Vite package.json?
if viteVers, ok := pkgJSON.DevDependencies["vite"]; ok {
major, full := getSemVer(viteVers)
output.ViteMajorVer = major
output.ViteVersion = full
} else {
// Can't do anything with this package.json
return nil
}
// TS?
_, ok := pkgJSON.DevDependencies["typescript"]
if ok {
output.HasTypeScript = true
}
supported := []string{
"vue",
"react",
"preact",
"svelte", // devdep!
"lit", // won't really support
}
var vers string
for _, pkg := range supported {
if pkg == "svelte" {
// special cased because svelte does not put
// any configuration into dependencies.
if sVer, ok := pkgJSON.DevDependencies["svelte"]; ok {
vers = sVer
major, full := getSemVer(vers)
output.PackageType = pkg
output.MajorVer = major
output.SvelteVersion = full
entryPt := "src/main.js"
if output.HasTypeScript {
entryPt = "src/main.ts"
}
output.EntryPoint = entryPt
break
}
} else {
if vers, ok = pkgJSON.Dependencies[pkg]; ok {
output.PackageType = pkg
major, full := getSemVer(vers)
output.MajorVer = major
// handle by category
entryPt := "src/main.js" // most common case
switch pkg {
case "vue":
output.VueVersion = full
if output.HasTypeScript {
entryPt = "src/main.ts"
}
case "react":
output.ReactVersion = full
if output.HasTypeScript {
entryPt = "src/main.tsx"
} else {
entryPt = "src/main.jsx"
}
case "preact":
output.PreactVersion = full
if output.HasTypeScript {
entryPt = "src/main.tsx"
} else {
entryPt = "src/main.jsx"
}
case "lit":
output.LitVersion = full
// we do not set entryPt;
// lit is just too weird.
entryPt = ""
}
// We know as much as we can...
output.EntryPoint = entryPt
break
}
}
}
// If we do not have type, call it Vanilla
if output.PackageType == "" {
output.IsVanilla = true
output.PackageType = "vanilla"
// Vite choses entry points anyway. For some
// very odd reason, the JS project is flat,
// and the TS project puts files in src/
// Why? Good question.
if output.HasTypeScript {
output.EntryPoint = "src/main.ts"
} else {
output.EntryPoint = "main.js"
}
}
// ViteVer
// TS
// script "build": "tsc && vite build",
// OR: DD "typescript": "^4.6.4"
// Vanilla
// NO DD
// entry like Vue
// Vanilla TS
// script + DD
// VueVer
// deps "vue": "^3.2.37"
// src/main.js, src/main.ts
// ReactVer
// deps "react": "^18.2.0"
// src/main.jsx
// React TS
// src/main.tsx
// Preact
// dep "preact": "^10.9.0"
// TS: script + DD
// entry as react
// SvelteVer
// devdeps: "svelte": "^3.49.0",
// entry: as Vue
// SvelteTSVer
// NO build step info
// DD: "typescript": "^4.6.4"
// Lit
// Weird structure!
// deps "lit": "^2.2.7"
// TS script + DD
// weird entry src/my-element.ts
return &output
}
func (vc *ViteConfig) getViteVersion() (string, error) {
// If it's set, use it.
if vc.ViteVersion != "" {
return vc.ViteVersion, nil
}
if vc.DevDefaults == nil {
return "", errors.New("not Vite project")
}
vc.ViteVersion = vc.DevDefaults.ViteMajorVer
return vc.DevDefaults.ViteMajorVer, nil
}
func (vc *ViteConfig) SetDevelopmentDefaults() error {
// Make sure we can find package.json:
if vc.JSProjectPath == "" {
vc.JSProjectPath = "frontend"
}
pkgJSON, err := vc.parsePackageJSON()
if err != nil {
return err
}
defaults := analyzePackageJSON(pkgJSON)
if defaults == nil {
return errors.New("invalid configuration")
}
vc.DevDefaults = defaults
version, err := vc.getViteVersion()
if err != nil {
vc.ViteVersion = DEFAULT_VITE_VERSION
version = vc.ViteVersion
}
// Check for anything already set, and if not set,
// use the defaults if they are not set.
if vc.Platform == "" {
vc.Platform = defaults.PackageType
}
if vc.EntryPoint == "" {
vc.EntryPoint = defaults.EntryPoint
}
if vc.URLPrefix == "" {
// Vite default
vc.URLPrefix = "/src/"
}
if vc.DevServerPort == "" {
if version == "2" {
vc.DevServerPort = DEFAULT_PORT_V2
} else {
vc.DevServerPort = DEFAULT_PORT_V3
}
}
if vc.DevServerDomain == "" {
vc.DevServerDomain = "localhost"
}
return nil
}
func (vc *ViteConfig) SetProductionDefaults() error {
if vc.JSProjectPath == "" {
vc.JSProjectPath = "frontend"
}
if vc.AssetsPath == "" {
vc.AssetsPath = "dist"
}
if vc.URLPrefix == "" {
vc.URLPrefix = "/assets/"
}
return nil
}
func (vc *ViteConfig) buildDevServerBaseURL() string {
protocol := "http"
if vc.HTTPS {
protocol = "https"
}
return fmt.Sprintf(
"%s://%s:%s",
protocol,
vc.DevServerDomain,
vc.DevServerPort,
)
}