-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
104 lines (83 loc) · 3.2 KB
/
index.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
const { copyFileSync, unlinkSync, existsSync, statSync, mkdirSync, emptyDirSync, readdirSync, writeFileSync } = require('fs-extra');
const { join } = require('path/posix');
const esbuild = require('esbuild');
/**
* @param {{
* out?: string;
* esbuildOverride?: import('esbuild').BuildOptions;
* }} options
*/
module.exports = function ({ out = 'build', esbuildOverride = {} } = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: 'adapter-serverless',
async adapt(builder) {
emptyDirSync(out);
const static_directory = join(out, 'assets');
if (!existsSync(static_directory)) {
mkdirSync(static_directory, { recursive: true });
}
const prerendered_directory = join(out, 'prerendered');
if (!existsSync(prerendered_directory)) {
mkdirSync(prerendered_directory, { recursive: true });
}
const server_directory = join(out, 'server');
if (!existsSync(server_directory)) {
mkdirSync(server_directory, { recursive: true });
}
const edge_directory = join(out, 'edge');
if (!existsSync(edge_directory)) {
mkdirSync(edge_directory, { recursive: true });
}
builder.log.minor('Copying assets');
builder.writeClient(static_directory);
builder.log.minor('Copying server');
builder.writeServer(out);
copyFileSync(`${__dirname}/files/serverless.js`, `${server_directory}/_serverless.js`);
copyFileSync(`${__dirname}/files/shims.js`, `${server_directory}/shims.js`);
builder.log.minor('Building lambda');
esbuild.buildSync({
...{
entryPoints: [`${server_directory}/_serverless.js`],
outfile: `${server_directory}/serverless.js`,
inject: [join(`${server_directory}/shims.js`)],
external: ['node:*'],
format: 'cjs',
bundle: true,
platform: 'node',
}, ...esbuildOverride
});
builder.log.minor('Prerendering static pages');
await builder.writePrerendered(prerendered_directory);
console.log('Building router');
copyFileSync(`${__dirname}/files/router.js`, `${edge_directory}/_router.js`);
let files = JSON.stringify([...getAllFiles(static_directory), ...getAllFiles(prerendered_directory)])
writeFileSync(`${edge_directory}/static.js`, `export default ${files}`)
esbuild.buildSync({
entryPoints: [`${edge_directory}/_router.js`],
outfile: `${edge_directory}/router.js`,
format: 'cjs',
bundle: true,
platform: 'node',
});
builder.log.minor('Cleanup');
unlinkSync(`${server_directory}/_serverless.js`);
unlinkSync(`${edge_directory}/_router.js`);
unlinkSync(`${out}/index.js`);
},
};
return adapter;
};
const getAllFiles = function (dirPath, basePath, arrayOfFiles) {
files = readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
basePath = basePath || dirPath
files.forEach(function (file) {
if (statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, basePath, arrayOfFiles)
} else {
arrayOfFiles.push(join("/", dirPath.replace(basePath, ''), "/", file))
}
})
return arrayOfFiles
}