vike-lite 1.15.0 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__internal/vite/defaultServerEntry.mjs +83 -0
- package/dist/vite.mjs +12 -10
- package/package.json +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { renderPage } from 'vike-lite/server'
|
|
2
|
+
import { createServer } from 'node:http'
|
|
3
|
+
import { Readable } from 'node:stream'
|
|
4
|
+
import { pipeline } from 'node:stream/promises'
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import fsPromises from 'node:fs/promises'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
10
|
+
const clientDir = path.resolve(__dirname, '../client')
|
|
11
|
+
const MIME_TYPES = {
|
|
12
|
+
'.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8',
|
|
13
|
+
'.mjs': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8',
|
|
14
|
+
'.json': 'application/json; charset=utf-8', '.xml': 'application/xml',
|
|
15
|
+
'.txt': 'text/plain; charset=utf-8', '.map': 'application/json; charset=utf-8',
|
|
16
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
|
|
17
|
+
'.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
|
|
18
|
+
'.webp': 'image/webp', '.avif': 'image/avif', '.woff': 'font/woff',
|
|
19
|
+
'.woff2': 'font/woff2', '.ttf': 'font/ttf', '.otf': 'font/otf',
|
|
20
|
+
'.mp4': 'video/mp4', '.webm': 'video/webm', '.mp3': 'audio/mpeg',
|
|
21
|
+
'.pdf': 'application/pdf', '.wasm': 'application/wasm',
|
|
22
|
+
'.webmanifest': 'application/manifest+json'
|
|
23
|
+
}
|
|
24
|
+
const server = createServer(async (req, res) => {
|
|
25
|
+
try {
|
|
26
|
+
const urlObj = new URL(req.url || '/', 'http://' + (req.headers.host || 'localhost'))
|
|
27
|
+
const pathname = urlObj.pathname
|
|
28
|
+
let fileUrl = pathname
|
|
29
|
+
const { BASE_URL } = import.meta.env
|
|
30
|
+
if (BASE_URL !== '/' && pathname.startsWith(BASE_URL)) fileUrl = '/' + pathname.slice(BASE_URL.length)
|
|
31
|
+
if (fileUrl !== '/') {
|
|
32
|
+
const filePath = path.resolve(clientDir, '.' + fileUrl)
|
|
33
|
+
if (!filePath.startsWith(clientDir)) {
|
|
34
|
+
res.statusCode = 403
|
|
35
|
+
res.end('Forbidden')
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const stat = await fsPromises.stat(filePath)
|
|
40
|
+
if (stat.isFile()) {
|
|
41
|
+
const ext = path.extname(filePath).toLowerCase()
|
|
42
|
+
const mimeType = MIME_TYPES[ext] || 'application/octet-stream'
|
|
43
|
+
res.setHeader('Content-Type', mimeType)
|
|
44
|
+
res.setHeader(
|
|
45
|
+
'Cache-Control',
|
|
46
|
+
fileUrl.startsWith('/assets/') ? 'public, max-age=31536000, immutable' : 'public, max-age=0, must-revalidate'
|
|
47
|
+
)
|
|
48
|
+
fs.createReadStream(filePath).pipe(res)
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
} catch { }
|
|
52
|
+
}
|
|
53
|
+
const headers = new Headers()
|
|
54
|
+
for (const [key, val] of Object.entries(req.headers)) {
|
|
55
|
+
if (Array.isArray(val)) for (const v of val) headers.append(key, v)
|
|
56
|
+
else if (val) headers.set(key, val)
|
|
57
|
+
}
|
|
58
|
+
const { method } = req
|
|
59
|
+
const init = { method, headers }
|
|
60
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
61
|
+
init.body = Readable.toWeb(req)
|
|
62
|
+
init.duplex = 'half'
|
|
63
|
+
}
|
|
64
|
+
const request = new Request(urlObj.href, init)
|
|
65
|
+
const response = await renderPage(request)
|
|
66
|
+
res.statusCode = response.status
|
|
67
|
+
for (const [key, val] of response.headers) res.setHeader(key, val)
|
|
68
|
+
if (method === 'HEAD' || !response.body) {
|
|
69
|
+
await response.body?.cancel()
|
|
70
|
+
res.end()
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
try { await pipeline(Readable.fromWeb(response.body), res) } catch (error) { console.error('Stream error:', error) }
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('Handler error:', error)
|
|
76
|
+
if (!res.headersSent) {
|
|
77
|
+
res.statusCode = 500
|
|
78
|
+
res.end('Internal Server Error')
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
const port = process.env.PORT ? Number.parseInt(process.env.PORT) : 3000
|
|
83
|
+
server.listen(port, () => { console.log(`\n\u{1B}[32m🚀 Server is running at http://localhost:${port}\u{1B}[0m\n`) })
|
package/dist/vite.mjs
CHANGED
|
@@ -128,7 +128,7 @@ function vikeLite({ pagesDir = "pages", apiPrefix = "/api", prerender = false, s
|
|
|
128
128
|
for (const key in envVariables) if (process.env[key] === void 0) process.env[key] = envVariables[key];
|
|
129
129
|
outDir = config.build?.outDir ?? "dist";
|
|
130
130
|
const { emptyOutDir, minify = true, cssMinify = true, sourcemap } = config.build || {};
|
|
131
|
-
viteConfigRoot = config.root ? path.resolve(config.root) : process.cwd();
|
|
131
|
+
viteConfigRoot = (config.root ? path.resolve(config.root) : process.cwd()).replaceAll("\\", "/");
|
|
132
132
|
const { routes } = generateRoutes(viteConfigRoot, pagesDir);
|
|
133
133
|
hasAnyPrerender = prerender || routes.some((r) => r.prerender);
|
|
134
134
|
return {
|
|
@@ -262,19 +262,21 @@ function vikeLite({ pagesDir = "pages", apiPrefix = "/api", prerender = false, s
|
|
|
262
262
|
if (serverEntry) {
|
|
263
263
|
const basePath = path.join(viteConfigRoot, serverEntry);
|
|
264
264
|
let serverEntryPath = "";
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
265
|
+
if (!fs.existsSync(basePath)) {
|
|
266
|
+
for (const ext of [
|
|
267
|
+
".ts",
|
|
268
|
+
".js",
|
|
269
|
+
".mjs"
|
|
270
|
+
]) if (fs.existsSync(basePath + ext)) {
|
|
271
|
+
serverEntryPath = basePath + ext;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
if (!serverEntryPath) throw new Error(`[vike-lite] serverEntry ${serverEntry} file not found`);
|
|
272
275
|
}
|
|
273
|
-
if (!serverEntryPath) throw new Error(`[vike-lite] serverEntry ${serverEntry} file not found`);
|
|
274
276
|
return importSetup + `export*from'${serverEntryPath}';export{default}from'${serverEntryPath}';`;
|
|
275
277
|
}
|
|
276
278
|
if (serverEntry === false) return importSetup + "import{renderPage}from'vike-lite/server';export default{fetch:renderPage};";
|
|
277
|
-
const defaultServerEntryContent = isProd ? fs.readFileSync(path.
|
|
279
|
+
const defaultServerEntryContent = isProd ? fs.readFileSync(path.resolve("vike-lite/__internal/vite/defaultServerEntry.mjs"), "utf8") : `import{renderPage}from'vike-lite/server';`;
|
|
278
280
|
return importSetup + defaultServerEntryContent + "export default{fetch:renderPage};";
|
|
279
281
|
}
|
|
280
282
|
if (id === RESOLVED.entryPrerender) return importSetup + `export{routes}from'${VIRTUAL.routes}';`;
|