-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (144 loc) · 4.27 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
154
155
156
157
158
159
160
161
162
163
164
165
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { readFileSync, writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { rollup } from 'rollup';
const files = fileURLToPath(new URL('./files', import.meta.url).href);
/** @type {import('.').default} */
export default function (opts = {}) {
const {
out = 'build',
precompress = false,
envPrefix = '',
polyfill = true,
copyDevNodeModules = false,
cleanPackageJson = true,
copyNpmrc = true,
} = opts;
const buildername = 'amplify-adapter';
return {
name: buildername,
async adapt(builder) {
const tmp = builder.getBuildDirectory(buildername);
const computePath = `${out}/compute/default`;
builder.rimraf(out);
builder.rimraf(tmp);
builder.mkdirp(tmp);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/static${builder.config.kit.paths.base}`);
builder.writePrerendered(`${computePath}/prerendered${builder.config.kit.paths.base}`);
if (precompress) {
builder.log.minor('Compressing assets');
await Promise.all([
builder.compress(`${out}/static`),
builder.compress(`${computePath}/prerendered`),
]);
}
builder.log.minor('Building server');
builder.writeServer(tmp);
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath: './' })};\n\n` +
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n`
);
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
// we bundle the Vite output so that deployments only need
// their production dependencies. Anything in devDependencies
// will get included in the bundled code
const bundle = await rollup({
input: {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`,
},
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`)),
],
plugins: [
nodeResolve({
preferBuiltins: true,
exportConditions: ['node'],
}),
commonjs({ strictRequires: true }),
json(),
],
});
await bundle.write({
dir: `${computePath}/server`,
format: 'esm',
sourcemap: true,
sourcemapPathTransform: (relativePath) => {
let regex = new RegExp(`((..\/)+.svelte-kit\/${buildername}\/)`, 'g');
relativePath = relativePath.replace(regex, './');
regex = new RegExp(`((..\/)+node_modules/)`, 'g');
relativePath = relativePath.replace(regex, '../node_modules');
return relativePath;
},
chunkFileNames: 'chunks/[name]-[hash].js',
});
builder.copy(files, computePath, {
replace: {
ENV: './env.js',
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: './server/index.js',
SHIMS: './shims.js',
ENV_PREFIX: JSON.stringify(envPrefix),
},
});
writeFileSync(
`${out}/deploy-manifest.json`,
JSON.stringify({
version: 1,
framework: { name: 'SvelteKit', version: '0.0.3' },
routes: [
{
path: '/*.*',
target: {
kind: 'Static',
cacheControl: 'public, max-age=2',
},
fallback: {
kind: 'Compute',
src: 'default',
},
},
{
path: '/*',
target: {
kind: 'Compute',
src: 'default',
},
},
],
computeResources: [
{
name: 'default',
runtime: 'nodejs18.x',
entrypoint: 'index.js',
},
],
})
);
// If polyfills aren't wanted then clear the file
if (!polyfill) {
writeFileSync(`${computePath}/shims.js`, '', 'utf-8');
}
if (copyDevNodeModules) {
builder.copy('node_modules', `${computePath}/node_modules`, {});
}
if (copyNpmrc) {
builder.copy('.npmrc', `${computePath}/.npmrc`, {});
}
if (cleanPackageJson) {
const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
delete packageJson.devDependencies;
delete packageJson.scripts;
writeFileSync(`${computePath}/package.json`, JSON.stringify(packageJson, null, 2), 'utf-8');
} else {
builder.copy('package.json', `${computePath}/package.json`, {});
}
},
};
}