-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (166 loc) · 5.93 KB
/
Copy pathindex.js
File metadata and controls
190 lines (166 loc) · 5.93 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { mkdirSync, readFileSync, rmSync } from 'node:fs';
import { extname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { rolldown } from 'rolldown';
const files = fileURLToPath(new URL('./files', import.meta.url).href);
/** @param {string} str */
function escape_regex(str) {
// TODO replace with `RegExp.escape(str)` when we require Node >= 24
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/** @type {typeof import('./index.js').default} */
export default function (opts = {}) {
const { out = 'build', precompress = true, envPrefix = '' } = opts;
return {
name: '@sveltejs/adapter-node',
async adapt(builder) {
const tmp = builder.getBuildDirectory('adapter-node');
rmSync(out, { force: true, recursive: true });
rmSync(tmp, { force: true, recursive: true });
mkdirSync(tmp, { recursive: true });
builder.log.minor('Copying assets');
const written = [
...builder.writeClient(`${out}/client${builder.config.paths.base}`),
...builder.writePrerendered(`${out}/prerendered${builder.config.paths.base}`)
];
/** @type {string[]} */
let compressed = [];
if (precompress) {
builder.log.minor('Compressing assets');
compressed = (
await Promise.all([
builder.compress(`${out}/client`),
builder.compress(`${out}/prerendered`)
])
).flat();
}
const compressed_extensions = new Set(compressed.map((file) => extname(file)));
// a pathname whose extension appears in neither set may be a route segment
// resolving to a compressed `index.html`, so it must keep its `Vary` header
const uncompressed_extensions = new Set(
written.map((file) => extname(file)).filter((ext) => ext && !compressed_extensions.has(ext))
);
builder.log.minor('Building server');
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
const server = builder.getServerDirectory();
// Copy the prebuilt entrypoints into the build directory so that the
// adapter's own bundled dependencies resolve correctly, then bundle them
// together with the app's server code. Bundling everything in a single
// pass means shared modules (e.g. `SvelteKitError` from `@sveltejs/kit`)
// aren't duplicated. See https://github.com/sveltejs/kit/issues/15755
const entries = posixify(`${tmp}/entries`);
builder.copy(files, entries);
const dir_id = `${entries}/dir.js`;
/** @type {Record<string, string>} */
const input = {
index: `${entries}/index.js`,
env: `${entries}/env.js`,
handler: `${entries}/handler.js`
};
if (builder.hasServerInstrumentationFile()) {
input['instrumentation.server'] = `${server}/instrumentation.server.js`;
}
builder.generateServerInstance(`${server}/server.js`);
/** @type {Record<string, string>} */
const defines = {
UNCOMPRESSED_EXTENSIONS: `new Set(${JSON.stringify([...uncompressed_extensions])})`,
BASE_PATH: JSON.stringify(builder.config.paths.base),
APP_PATH: JSON.stringify(builder.getAppPath()),
PRERENDERED: `new Set(${JSON.stringify(builder.prerendered.paths)})`,
MIME_TYPES: JSON.stringify(builder.mimeTypes),
ORIGIN: JSON.stringify(builder.config.paths.origin) || 'undefined',
ENV_PREFIX: JSON.stringify(envPrefix),
PRECOMPRESS: JSON.stringify(precompress)
};
// 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 rolldown({
input,
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`)),
// `@opentelemetry/api` is an optional peer dependency of `@sveltejs/kit`,
// so it's not in `pkg.dependencies` and wouldn't be matched by the regex above.
// It must stay external so that `instrumentation.server.js` and the SvelteKit
// runtime share a single instance — see https://github.com/sveltejs/kit/issues/16288
/^@opentelemetry\/api(\/.*)?$/
],
platform: 'node',
resolve: {
conditionNames: ['node']
},
experimental: {
nativeMagicString: true
},
plugins: [
{
// resolve the app's server and manifest, generated above
name: 'adapter-node-resolve-app',
resolveId: {
filter: { id: /^SERVER$/ },
handler() {
return `${server}/server.js`;
}
}
},
{
// replace build-time constants in the adapter's own entrypoints
// only, so that identifiers in the app or its dependencies aren't
// accidentally replaced
name: 'adapter-node-replace-constants',
transform: {
filter: { id: new RegExp(escape_regex(entries)) },
handler(_code, _id, { magicString }) {
if (!magicString) throw new Error('experimental.nativeMagicString is not enabled');
for (const [from, to] of Object.entries(defines)) {
// remove $& and $N substitutions by replacing every $ with $$
const value = to.replace(/\$/g, '$$$$');
magicString.replace(new RegExp(`\\b${from}\\b`, 'g'), value);
}
return {
code: magicString,
map: magicString.generateMap().toString()
};
}
}
}
]
});
await bundle.write({
dir: out,
format: 'esm',
sourcemap: true,
codeSplitting: {
groups: [
{
name: 'dir',
test: dir_id
}
]
},
chunkFileNames(chunk) {
if (chunk.name === 'dir') return '[name].js';
return 'server/chunks/[name]-[hash].js';
}
});
if (builder.hasServerInstrumentationFile()) {
builder.instrument({
entrypoint: `${out}/index.js`,
instrumentation: `${out}/instrumentation.server.js`,
module: {
exports: ['path', 'host', 'port', 'server']
}
});
}
},
supports: {
read: () => true,
instrumentation: () => true
}
};
}
/** @param {string} str */
function posixify(str) {
return str.replace(/\\/g, '/');
}