-
Notifications
You must be signed in to change notification settings - Fork 8
/
build-vercel.js
62 lines (51 loc) · 2.09 KB
/
build-vercel.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
const {resolve} = require("path");
const commandLineArgs = require('command-line-args');
const {copySync, rmSync, mkdirSync} = require("fs-extra");
const {buildClientBundle, buildVercelMiddleware} = require("./index");
const { existsSync, writeFileSync, write, rm, rmdirSync } = require("fs");
const optionDefinition = [
{name: 'dir', alias: 'd', defaultOption: true},
{name: 'ssr', alias: 's', type: Boolean},
{name: 'stream', type: Boolean},
{name: 'public', alias: 'u', multiple: true},
{name: 'minify', alias: 'm', type: Boolean},
];
const runOptions = commandLineArgs(optionDefinition, process.argv);
if (!runOptions.dir?.length) {
console.log("Required: dir");
process.exit(0);
}
const {dir, ssr, public, minify} = runOptions;
const indexModule = resolve(dir, "index.h0.ts");
const publicFolders = [...public || [], resolve(dir, "public")];
const htmlFile = resolve(dir, "template.h0.html");
if (!existsSync(htmlFile))
throw new Error(`Template ${htmlFile} not found`);
if (!existsSync(indexModule))
throw new Error(`Index ${indexModule} not found`);
if (existsSync(".vercel/output"))
rmSync(".vercel/output", {recursive: true});
mkdirSync(".vercel/output");
mkdirSync(".vercel/output/functions");
mkdirSync(".vercel/output/functions/_middleware.func");
mkdirSync(".vercel/output/functions/_middleware.func/node_modules");
mkdirSync(".vercel/output/static");
for (const pub of publicFolders)
copySync(pub, ".vercel/output/static");
copySync("node_modules", ".vercel/output/functions/_middleware.func/node_modules");
writeFileSync(".vercel/output/config.json", JSON.stringify({
"version": 3,
"routes": [
{
"src": "/(.*)",
"middlewarePath": "_middleware",
"continue": true
}
]
}));
writeFileSync(".vercel/output/functions/_middleware.func/.vc-config.json", JSON.stringify({
"runtime": "edge",
"entrypoint": "index.js"
}));
buildClientBundle(indexModule, resolve(".vercel/output/static/.h0"), {minify: !!minify});
buildVercelMiddleware(dir, ".vercel/output/functions/_middleware.func/index.js", {serverSideRendering: ssr, stream: !!runOptions.stream});