Skip to content

Commit 9a9a173

Browse files
committed
fix(dev): read html template every time
1 parent 2064e04 commit 9a9a173

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

vite.config.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ const cur = import.meta.dirname
77

88
const hierarchyP = fsp.readFile(join(cur, 'src', 'hierarchy.json'))
99
const ignoreP = fsp.readFile(join(cur, 'src', 'ignore.json'))
10-
const templateP = fsp.readFile(join(cur, 'src', 'index.html'))
11-
const [hierarchyB, ignoreB, templateB] = await Promise.all([hierarchyP, ignoreP, templateP])
10+
const [hierarchyB, ignoreB] = await Promise.all([hierarchyP, ignoreP])
1211

1312
const hierarchy = JSON.parse(hierarchyB.toString('utf8'))
1413
const ignore = JSON.parse(ignoreB.toString('utf8'))
15-
const template = templateB.toString('utf8')
1614

1715
const regions: string[] = []
1816
for(const regK in hierarchy) {
@@ -37,15 +35,20 @@ function genRegions(curName: string, addHidden: boolean) {
3735
}
3836

3937
type PageObject = {
40-
render: () => string
38+
render: () => Promise<string>
39+
}
40+
41+
async function readTemplate() {
42+
const templateB = await fsp.readFile(join(cur, 'src', 'index.html'))
43+
return templateB.toString('utf8')
4144
}
4245

4346
const pages: Record<string, PageObject> = {}
4447
for(const regK of regions) {
4548
pages[regK.toLowerCase()] = {
46-
render: () => {
49+
render: async() => {
4750
const regName = hierarchy[regK].data.name
48-
const html = template
51+
const html = (await readTemplate())
4952
.replace(
5053
'$$$TITLE$$$',
5154
regName + ' Map — Rain World: The Watcher DLC'
@@ -69,8 +72,8 @@ for(const regK of regions) {
6972
}
7073

7174
pages.index = {
72-
render: () => {
73-
const html = template
75+
render: async() => {
76+
const html = (await readTemplate())
7477
.replace(
7578
'$$$TITLE$$$',
7679
'Map of Rain World: The Watcher DLC'
@@ -152,7 +155,7 @@ function virtualHtml({ pages }: { pages: Record<string, PageObject> }): Plugin {
152155
res.end(Buffer.from(
153156
await server.transformIndexHtml(
154157
req.url,
155-
page.render()
158+
await page.render()
156159
),
157160
'utf8',
158161
))
@@ -165,13 +168,13 @@ function virtualHtml({ pages }: { pages: Record<string, PageObject> }): Plugin {
165168
}
166169
}
167170
},
168-
load: (id) => {
171+
load: async(id) => {
169172
const prefix = curRoot + '/'
170173
if(id.startsWith(prefix) && id.endsWith('.html')) {
171174
const p = pages[id.substring(prefix.length, id.length - '.html'.length)]
172175
if(p) {
173176
return {
174-
code: p.render(),
177+
code: await p.render(),
175178
name: id,
176179
}
177180
}

0 commit comments

Comments
 (0)