-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite-plugin-file.js
137 lines (120 loc) · 3.87 KB
/
vite-plugin-file.js
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
const prefix = "\x00file:"
const suffix = "?file"
export default function viteFilePlugin() {
/** @type {import('vite').Plugin} */
const plugin = {
name: "vite-plugin-file",
enforce: "pre",
async resolveId(source, importer, options) {
if (source.endsWith(suffix)) {
/** @type {{ id: string | null | undefined }} */
const { id } = Object(
await this.resolve(source.slice(0, -suffix.length), importer, {
skipSelf: true,
...Object(options),
}),
)
if (id) {
return prefix + id + suffix
}
}
},
load(id) {
if (id.startsWith(prefix) && id.endsWith(suffix)) {
const path = id.slice(prefix.length, -suffix.length)
const name = path.split("/").pop()
const extn = path.replace(/.*(\.[^.]+)$/, "$1").toLowerCase()
const type = contentTypes[extn] || null
const code = [
`import { createReadStream, promises } from "node:fs"`,
`import { Readable } from "node:stream"`,
// internal properties
`const path = ${JSON.stringify(path)}`,
// properties
`export const name = ${JSON.stringify(name)}`,
`export const type = ${JSON.stringify(type)}`,
`export const { mtimeMs: lastModified, size } = await promises.stat(path).catch(() => ({ mtimeMs: Date.now(), size: 0 })) // ""`,
// computed properties
`export const headers = new Headers({ "content-type": type, "content-length": size.toString(), "last-modified": new Date(lastModified).toUTCString() })`,
// methods
`export const arrayBuffer = () => promises.readFile(path).then(buffer => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)) // ""`,
`export const blob = () => arrayBuffer().then(arrayBuffer => new Blob([arrayBuffer], { type })) // ""`,
`export const file = () => arrayBuffer().then(arrayBuffer => new File([arrayBuffer], name, { type, lastModified })) // ""`,
`export const json = () => text().then(JSON.parse) // ""`,
`export const response = () => new Response(stream(), { headers }) // ""`,
`export const stream = () => Readable.toWeb(createReadStream(path)) // ""`,
`export const text = () => promises.readFile(path, "utf-8")`,
// default export
`export default { name, type, size, lastModified, headers, arrayBuffer, blob, file, json, response, stream, text } // ""`,
].join("\n")
return {
code,
}
}
},
}
return plugin
}
/** @type {Record<string, string>} */
const contentTypes = {
// text
".css": "text/css",
".csv": "text/csv",
".html": "text/html",
".js": "text/javascript",
".md": "text/markdown",
".txt": "text/plain",
".xml": "text/xml",
".yaml": "text/yaml",
".yml": "text/yaml",
// images
".avif": "image/avif",
".gif": "image/gif",
".heic": "image/heic",
".ico": "image/x-icon",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".jxl": "image/jxl",
".png": "image/png",
".svg": "image/svg+xml",
".tif": "image/tiff",
".tiff": "image/tiff",
".webp": "image/webp",
// video
".mkv": "video/x-matroska",
".mov": "video/quicktime",
".mp4": "video/mp4",
".webm": "video/webm",
// audio
".aac": "audio/aac",
".flac": "audio/flac",
".m4a": "audio/x-m4a",
".mp3": "audio/mpeg",
".ogg": "audio/ogg",
".opus": "audio/opus",
".wav": "audio/wav",
".weba": "audio/webm",
// model
".glb": "model/gltf-binary",
".gltf": "model/gltf+json",
".stl": "model/stl",
// fonts
".otf": "font/otf",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
// application data
".7z": "application/x-7z-compressed",
".apk": "application/vnd.android.package-archive",
".br": "application/x-brotli",
".deb": "application/vnd.debian.binary-package",
".gz": "application/gzip",
".json": "application/json",
".jsonld": "application/ld+json",
".pdf": "application/pdf",
".rpm": "application/x-rpm",
".tar": "application/x-tar",
".wasm": "application/wasm",
".zip": "application/zip",
".zst": "application/zstd",
}