-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
369 lines (308 loc) · 12.3 KB
/
index.ts
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import fs from 'fs'
import path from 'path'
import { minimatch } from 'minimatch'
import { globbySync } from 'globby'
import type { InputOptions } from 'rollup'
import type {
Manifest,
ManifestChunk,
Plugin,
ResolvedConfig,
UserConfig,
} from 'vite'
export interface PluginConfig {
/** Use to map input files to template paths
*
* @default
* {
* '*.{js,ts}': './layout.html'
* }
*/
inputs?: { [key: string]: string } | undefined
/** Base directory for input paths
* @default 'src/'
*/
srcDir?: string | undefined
/** Base directory for output files
* @default 'templates/'
*/
outDir?: string | undefined
}
export default function viteSpxPlugin(pluginConfig?: PluginConfig): Plugin {
const posixCwd = process.cwd().split(path.sep).join(path.posix.sep)
let projectPath = posixCwd.split('ASSETS')?.[1] // TODO: this is the opposite of robust
if (!projectPath) {
console.warn(
`vite-plugin-spx: it doesn't appear this project is installed in an SPX instance. \n PWD: ${posixCwd} \n This build will not work until copied into the correct location`
)
projectPath = `/templates/${posixCwd.split('/').at(-1)}`
console.warn(
`vite-plugin-spx: defaulting project path to: `,
projectPath
)
}
const inputConfig = pluginConfig?.inputs ?? {
'*.{js,ts}': './layout.html',
}
const srcDir = pluginConfig?.srcDir ?? 'src/'
const outDir = pluginConfig?.outDir ?? 'templates/'
const templateDefintionFiles: string[] = []
const inputPatterns = [
...Object.keys(inputConfig).map((matchPath) =>
path.posix.join(srcDir, matchPath)
),
'!**/*-definition.js',
'!**.d.ts',
]
// string array of paths to all input files (always ignore ts declaration files)
const inputs = globbySync(inputPatterns)
if (!inputs || !inputs.length) {
console.error('vite-plugin-spx: No inputs were found! Exiting')
process.exit(1)
} else {
console.log('vite-plugin-spx: Found the following inputs: ', inputs)
}
// now we know which inputs actually exist, lets clean up unused inputConfig entries so we don't load templates we don't need
Object.keys(inputConfig).forEach((matchPath) => {
if (
!inputs.some((input) =>
minimatch(input, path.posix.join(srcDir, matchPath))
)
)
delete inputConfig[matchPath]
})
// map from template paths to file buffers
const templates = {} as { [key: string]: string }
Object.values(inputConfig).forEach((templatePath) => {
if (templates[templatePath]) return // skip if already read
const fullPath = path.posix.join(process.cwd(), templatePath)
templates[templatePath] = fs.readFileSync(fullPath, 'utf-8')
})
let config: ResolvedConfig
let dSrvProtocol: string
let dSrvHost: string
let assetManifest: Manifest
let resolvedInputOptions: InputOptions
// take the template html and inject script and css assets into <head>, along with the SPXGCTemplateDefinition from the alongside .json
function injectAssetsTags(html: string, entry: string) {
const tags = []
const entryFileName = path.basename(entry, path.extname(entry))
if (config.mode === 'development') {
tags.push(
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
'@vite/client'
)}"></script>`
)
tags.push(
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
entry
)}"></script>`
)
} else if (config.mode === 'production' && assetManifest) {
let entryChunk = assetManifest[entry]
const pathToAssets = path.posix.relative(
path.posix.dirname(entry),
path.posix.join(srcDir, '.vite')
) // get the path from entry's directory to the .vite asset directoryf
function generateCssTags(
chunk: ManifestChunk,
alreadyProcessed: string[] = []
) {
chunk.css?.forEach((cssPath) => {
if (alreadyProcessed.includes(cssPath)) return // de-dupe assets
tags.push(
`<link rel="stylesheet" href="${path.posix.join(
pathToAssets,
cssPath
)}" />`
)
alreadyProcessed.push(cssPath)
})
// recurse
chunk.imports?.forEach((importPath) => {
generateCssTags(assetManifest[importPath], alreadyProcessed)
})
}
generateCssTags(entryChunk)
tags.push(
`<script type="module" src="${path.posix.join(
pathToAssets,
entryChunk.file
)}"></script>`
)
}
try {
let templateDefinitionPath: string
let isJavascript = true
templateDefinitionPath = path.posix.join(
path.dirname(entry),
`${entryFileName}-definition.js`
)
if (!fs.existsSync(templateDefinitionPath)) {
templateDefinitionPath = path.posix.join(
path.dirname(entry),
`${entryFileName}.json`
)
isJavascript = false
if (!fs.existsSync(templateDefinitionPath)) {
throw new Error('No template found')
}
}
const templateDefinition = fs.readFileSync(
templateDefinitionPath,
'utf-8'
)
if (templateDefinition) {
tags.push(
`<script>${
isJavascript ? '' : 'window.SPXGCTemplateDefinition = '
}${templateDefinition}</script>`
)
} else {
throw new Error('No template found')
}
templateDefintionFiles.push(
path.posix.join(posixCwd, templateDefinitionPath)
)
} catch (e) {
console.warn(
`vite-plugin-spx: no SPXGCTemplateDefinition file found for input "${entry}"`
)
}
const newHtml = html.includes('</head>')
? html.replace('</head>', tags.join('\n') + '\n</head>')
: tags.join('\n') + '\n' + html
return newHtml
}
// for each input create an html doc and emit to disk
function generateHTMLFiles() {
let resolvedInputs: string[]
// populate inputs, taking into account "input" can come in 3 forms
if (typeof resolvedInputOptions.input === 'string') {
resolvedInputs = [resolvedInputOptions.input]
} else if (Array.isArray(resolvedInputOptions.input)) {
resolvedInputs = resolvedInputOptions.input
} else {
resolvedInputs = Object.values(resolvedInputOptions.input)
}
const htmlDocs = {} as { [key: string]: string }
// generate string html for each input
resolvedInputs.forEach((inputPath) => {
// find first template that has a match path that this input satisfies
const matchPath = Object.keys(inputConfig).find((matchPath) => {
return minimatch(inputPath, path.posix.join(srcDir, matchPath))
})
const templatePath = inputConfig[matchPath]
const template = templates[templatePath]
// check template was found in the inputConfig and we loaded it from disk, otherwise skip this input
if (!template) {
console.error(
`vite-plugin-spx: No template found to match input "${inputPath}". This probably means the input file was manually specified in the vite rollup config, and the template will not be built.`
)
return
}
// add asset tags to template
const html = injectAssetsTags(
templates[templatePath],
inputPath.replace(/^(\.\/)/, '')
)
const relativePath = path.dirname(path.relative(srcDir, inputPath)) // get the path from the input's directory to srcDir
const name = path.basename(inputPath, path.extname(inputPath))
const filePath = path.join(outDir, relativePath, `${name}.html`)
htmlDocs[filePath] = html
})
// write each html doc to disk
for (const [filePath, htmlDoc] of Object.entries(htmlDocs)) {
const fullFilePath = path.join(process.cwd(), filePath)
const dir = path.dirname(fullFilePath)
try {
fs.mkdirSync(dir, { recursive: true })
} catch (e) {
console.error(
`vite-plugin-spx: Could not create directory ${dir} for input ${filePath}. Skipping...`
)
continue
}
fs.writeFile(fullFilePath, htmlDoc, () => {
console.log(`vite-plugin-spx: Wrote input ${filePath} to disk`)
})
}
}
return {
name: 'spx',
// validate and setup defaults in user's vite config
config: (_config, { mode }): UserConfig => {
const assetsDir = path.posix.join(outDir, '.vite')
return {
build: {
manifest: true,
outDir: assetsDir,
rollupOptions: {
input: inputs,
},
},
base:
mode === 'development'
? projectPath
: path.posix.join(projectPath, assetsDir),
appType: 'mpa',
}
},
configResolved(resolvedConfig: ResolvedConfig) {
// Capture resolved config for use in injectAssets
config = resolvedConfig
},
buildStart(options: InputOptions) {
// capture inputOptions for use in generateHtmlFiles in both dev & prod
resolvedInputOptions = options
},
writeBundle() {
if (!resolvedInputOptions?.input || config.mode !== 'production')
return
try {
// would be nice to not have to read the asset manifest from disk but I don't see another way
// relevant: https://github.com/vitejs/vite/blob/a9dfce38108e796e0de0e3b43ced34d60883cef3/packages/vite/src/node/ssr/ssrManifestPlugin.ts
assetManifest = JSON.parse(
fs
.readFileSync(
path.posix.join(
process.cwd(),
config.build.outDir,
'manifest.json'
)
)
.toString()
)
} catch (err) {
console.error(
"vite-plugin-spx: Failed to load manifest.json from build directory. HTML files won't be generated."
)
return
}
// prod inject
generateHTMLFiles()
},
configureServer(server) {
// we wait until the devserver is actually listening in order to find it's true origin
server.httpServer.on('listening', () => {
dSrvProtocol = server.config.server.https ? 'https' : 'http'
dSrvHost = `${server.config.server.host ?? 'localhost'}:${
server.config.server.port ?? '5173'
}`
// dev inject
generateHTMLFiles()
})
},
handleHotUpdate({ file }) {
if (templateDefintionFiles.includes(file)) {
console.log(
'Template definition changed, regenerating .html: ',
file
)
generateHTMLFiles() // TODO: make this just regenerate the 1 html file
}
},
}
}