-
Notifications
You must be signed in to change notification settings - Fork 7
/
manifest.go
173 lines (148 loc) · 3.93 KB
/
manifest.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
package vite
import (
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
)
// Manifest file as written by vite build, as described in the [Vite Manifest].
//
// It is required for backend integration as described in
// [Vite Backend Integration].
//
// [Vite Manifest]: https://vitejs.dev/guide/api-plugin.html#manifest
// [Vite Backend Integration]: https://vitejs.dev/guide/backend-integration.html
type Manifest map[string]*Chunk
// A Chunk is a single entry in the manifest.
type Chunk struct {
File string `json:"file"`
Name string `json:"name"`
Src string `json:"src"`
CSS []string `json:"css"`
IsDynamicEntry bool `json:"isDynamicEntry"`
IsEntry bool `json:"isEntry"`
Imports []string `json:"imports"`
DynamicImports []string `json:"dynamicImports"`
}
// ParseManifest parses the manifest file.
func ParseManifest(r io.Reader) (*Manifest, error) {
var m Manifest
if err := json.NewDecoder(r).Decode(&m); err != nil {
return nil, err
}
return &m, nil
}
// GetEntryPoint returns the entry point from the Vite manifest.
func (m Manifest) GetEntryPoint() *Chunk {
for _, chunk := range m {
if chunk.IsEntry {
return chunk
}
}
return nil
}
// GetEntryPoints returns the entry points from the manifest.
func (m Manifest) GetEntryPoints() []*Chunk {
var entryPoints []*Chunk
for _, chunk := range m {
if chunk.IsEntry {
entryPoints = append(entryPoints, chunk)
}
}
return entryPoints
}
// GetChunk returns the chunk with the given name from the manifest.
//
// The name is the name of the source file.
func (m Manifest) GetChunk(name string) (*Chunk, bool) {
chunk, ok := m[name]
return chunk, ok
}
// PluginReactPreamble returns the script tag that should be injected into the
// HTML to enable React Fast Refresh.
func PluginReactPreamble(server string) string {
url, _ := url.JoinPath(server, "/@react-refresh")
return fmt.Sprintf(`<script type="module">
import RefreshRuntime from '%s'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>`, url)
}
// GenerateCSS generates the CSS links for the given chunk.
//
// The name is the name of the source file, e.g. "src/main.tsx".
func (m Manifest) GenerateCSS(name string) string {
var sb strings.Builder
seen := make(map[string]bool)
var addCSS func(string)
addCSS = func(name string) {
if seen[name] {
return
}
seen[name] = true
chunk, ok := m[name]
if !ok {
return
}
for _, css := range chunk.CSS {
sb.WriteString(`<link rel="stylesheet" href="`)
sb.WriteString("/")
sb.WriteString(css)
sb.WriteString(`">`)
}
for _, imp := range chunk.Imports {
addCSS(imp)
}
}
addCSS(name)
return sb.String()
}
// GenerateModules generates the module scripts for the given chunk.
//
// The name is the name of the source file, e.g. "src/main.tsx".
func (m Manifest) GenerateModules(name string) string {
chunk, ok := m[name]
if !ok {
return ""
}
var sb strings.Builder
if chunk.File != "" {
sb.WriteString(`<script type="module" src="`)
sb.WriteString("/")
sb.WriteString(chunk.File)
sb.WriteString(`"></script>`)
}
return sb.String()
}
// GeneratePreloadModules generates the preload modules for the given chunk.
//
// The name is the name of the source file, e.g. "src/main.tsx".
func (m Manifest) GeneratePreloadModules(name string) string {
var sb strings.Builder
seen := make(map[string]bool)
var addModulePreload func(string)
addModulePreload = func(name string) {
if seen[name] {
return
}
seen[name] = true
chunk, ok := m[name]
if !ok {
return
}
if chunk.File != "" {
sb.WriteString(`<link rel="modulepreload" href="`)
sb.WriteString("/")
sb.WriteString(chunk.File)
sb.WriteString(`">`)
}
for _, imp := range chunk.Imports {
addModulePreload(imp)
}
}
addModulePreload(name)
return sb.String()
}