-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (134 loc) · 4.81 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
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
// @ts-self-types="./index.d.ts"
/**
* SvelteKit adapter for AWS Lambda and Lambda@Edge.
* @module sveltekit-adapter-aws
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { build, transform } from "esbuild";
/**
* @typedef {import('@sveltejs/kit').Adapter} Adapter
* @typedef {import('@sveltejs/kit').Builder} Builder
*/
/**
* Adapter options type definition.
* @typedef {object} AdapterOptions
* @property {string} [out] - The output directory for the adapter
* @property {boolean} [edge] - Whether to build for edge deployment (requires environment variables to use placeholders)
* @property {boolean} [stream] - Whether to build for response streaming (requires lambda invoke method to be RESPONSE_STREAM)
* @property {import('esbuild').BuildOptions} [esbuild] - Additional esbuild options
*/
const __dirname = fileURLToPath(new URL(".", import.meta.url));
/**
* Default function to create the SvelteKit adapter.
* @param {AdapterOptions} [options] - The adapter options
* @returns {Adapter} The adapter configuration
*/
export default function (options = {}) {
const name = "sveltekit-adapter-aws";
const opt = {
out: options.out ?? "build",
edge: options.edge ?? false,
stream: options.stream ?? false,
esbuild: options.esbuild ?? {},
};
/**
* WARNING: This is a workaround to inject environment variables into the edge function
* The placeholder '{{_EDGE_FUNCTION_ENVIRONMENT_}}' must be replaced with your IaC pipeline
*/
const banner = opt.edge
? "process.env = { ...process.env, ...'{{_EDGE_FUNCTION_ENVIRONMENT_}}' }; import { createRequire } from 'module'; const require = createRequire(import.meta.url);"
: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);";
const adapter = {
name: name,
/**
* Adapts the project for deployment.
* @param {Builder} builder - The builder instance from SvelteKit
* @returns {Promise<void>}
*/
async adapt(builder) {
const tmp = path.join(".svelte-kit", name);
const clientDir = path.join(tmp, "client");
const serverDir = path.join(tmp, "server");
const prerenderedDir = path.join(tmp, "prerendered");
// Cleanup temporary output folder
builder.rimraf(tmp);
builder.mkdirp(clientDir);
builder.mkdirp(serverDir);
builder.mkdirp(prerenderedDir);
// Create static output
builder.log.minor("Copying assets...");
builder.writeClient(clientDir);
const prerenderedFiles = builder.writePrerendered(prerenderedDir);
// Create Lambda function
builder.log.minor("Generating server function...");
builder.writeServer(serverDir);
// copy over handler files in server handler folder
builder.copy(
path.join(__dirname, "handler"),
path.join(serverDir, "lambda-handler")
);
// copy over cloudfront files in server handler folder
builder.copy(
path.join(__dirname, "cloudfront"),
path.join(serverDir, "cloudfront")
);
// save a list of files in server handler folder
fs.writeFileSync(
path.join(serverDir, "lambda-handler", "prerendered-file-list.js"),
`export default ${JSON.stringify(prerenderedFiles)}`
);
// Set output directory
const out = path.resolve(opt.out);
const s3 = path.join(out, "s3");
const lambda = path.join(out, "lambda");
const cloudfront = path.join(out, "cloudfront");
// Cleanup output directory
builder.rimraf(out);
builder.mkdirp(s3);
builder.mkdirp(lambda);
builder.mkdirp(cloudfront);
// Copy static files & prerendered pages to S3
builder.log.minor("Copying assets for S3...");
builder.copy(clientDir, s3);
builder.copy(prerenderedDir, s3);
// Minify Cloudfront Function code
builder.log.minor("Minifying Cloudfront function...");
const minifiedCloudfrontFunction = await transform(
fs.readFileSync(
path.join(serverDir, "cloudfront", "index.js"),
"utf-8"
),
{
minify: true,
target: "es2020",
}
);
fs.writeFileSync(
path.join(cloudfront, "index.js"),
`${minifiedCloudfrontFunction.code}`
);
// Bundle and minify server code
builder.log.minor("Bundling Lambda function...");
await build({
entryPoints: [path.join(serverDir, "lambda-handler", "index.js")],
outfile: path.join(lambda, "index.mjs"),
outExtension: {
".js": ".mjs",
},
bundle: true,
minify: true,
platform: "node",
target: "esnext",
format: "esm",
external: ["aws-sdk"],
banner: {
js: banner,
},
...opt.esbuild,
});
},
};
return adapter;
}