@sveltejs/adapter-vercel 4.0.5 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { installPolyfills } from '@sveltejs/kit/node/polyfills';
2
- import { getRequest, setResponse } from '@sveltejs/kit/node';
2
+ import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/node';
3
3
  import { Server } from 'SERVER';
4
4
  import { manifest } from 'MANIFEST';
5
5
 
@@ -8,7 +8,8 @@ installPolyfills();
8
8
  const server = new Server(manifest);
9
9
 
10
10
  await server.init({
11
- env: /** @type {Record<string, string>} */ (process.env)
11
+ env: /** @type {Record<string, string>} */ (process.env),
12
+ read: createReadableStream
12
13
  });
13
14
 
14
15
  const DATA_SUFFIX = '/__data.json';
package/index.js CHANGED
@@ -5,6 +5,7 @@ import { nodeFileTrace } from '@vercel/nft';
5
5
  import esbuild from 'esbuild';
6
6
  import { get_pathname } from './utils.js';
7
7
 
8
+ const name = '@sveltejs/adapter-vercel';
8
9
  const DEFAULT_FUNCTION_NAME = 'fn';
9
10
 
10
11
  const get_default_runtime = () => {
@@ -17,6 +18,9 @@ const get_default_runtime = () => {
17
18
  );
18
19
  };
19
20
 
21
+ // https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules
22
+ const compatible_node_modules = ['async_hooks', 'events', 'buffer', 'assert', 'util'];
23
+
20
24
  /** @type {import('.').default} **/
21
25
  const plugin = function (defaults = {}) {
22
26
  if ('edge' in defaults) {
@@ -24,7 +28,7 @@ const plugin = function (defaults = {}) {
24
28
  }
25
29
 
26
30
  return {
27
- name: '@sveltejs/adapter-vercel',
31
+ name,
28
32
 
29
33
  async adapt(builder) {
30
34
  if (!builder.routes) {
@@ -63,6 +67,8 @@ const plugin = function (defaults = {}) {
63
67
  * @param {import('@sveltejs/kit').RouteDefinition<import('.').Config>[]} routes
64
68
  */
65
69
  async function generate_serverless_function(name, config, routes) {
70
+ const dir = `${dirs.functions}/${name}.func`;
71
+
66
72
  const relativePath = path.posix.relative(tmp, builder.getServerDirectory());
67
73
 
68
74
  builder.copy(`${files}/serverless.js`, `${tmp}/index.js`, {
@@ -77,12 +83,12 @@ const plugin = function (defaults = {}) {
77
83
  `export const manifest = ${builder.generateManifest({ relativePath, routes })};\n`
78
84
  );
79
85
 
80
- await create_function_bundle(
81
- builder,
82
- `${tmp}/index.js`,
83
- `${dirs.functions}/${name}.func`,
84
- config
85
- );
86
+ await create_function_bundle(builder, `${tmp}/index.js`, dir, config);
87
+
88
+ for (const asset of builder.findServerAssets(routes)) {
89
+ // TODO use symlinks, once Build Output API supports doing so
90
+ builder.copy(`${builder.getServerDirectory()}/${asset}`, `${dir}/${asset}`);
91
+ }
86
92
  }
87
93
 
88
94
  /**
@@ -106,20 +112,61 @@ const plugin = function (defaults = {}) {
106
112
  `export const manifest = ${builder.generateManifest({ relativePath, routes })};\n`
107
113
  );
108
114
 
109
- await esbuild.build({
110
- entryPoints: [`${tmp}/edge.js`],
111
- outfile: `${dirs.functions}/${name}.func/index.js`,
112
- target: 'es2020', // TODO verify what the edge runtime supports
113
- bundle: true,
114
- platform: 'browser',
115
- format: 'esm',
116
- external: config.external,
117
- sourcemap: 'linked',
118
- banner: { js: 'globalThis.global = globalThis;' },
119
- loader: {
120
- '.wasm': 'copy'
115
+ try {
116
+ const result = await esbuild.build({
117
+ entryPoints: [`${tmp}/edge.js`],
118
+ outfile: `${dirs.functions}/${name}.func/index.js`,
119
+ target: 'es2020', // TODO verify what the edge runtime supports
120
+ bundle: true,
121
+ platform: 'browser',
122
+ format: 'esm',
123
+ external: [
124
+ ...compatible_node_modules,
125
+ ...compatible_node_modules.map((id) => `node:${id}`),
126
+ ...(config.external || [])
127
+ ],
128
+ sourcemap: 'linked',
129
+ banner: { js: 'globalThis.global = globalThis;' },
130
+ loader: {
131
+ '.wasm': 'copy'
132
+ }
133
+ });
134
+
135
+ if (result.warnings.length > 0) {
136
+ const formatted = await esbuild.formatMessages(result.warnings, {
137
+ kind: 'warning',
138
+ color: true
139
+ });
140
+
141
+ console.error(formatted.join('\n'));
121
142
  }
122
- });
143
+ } catch (error) {
144
+ for (const e of error.errors) {
145
+ for (const node of e.notes) {
146
+ const match =
147
+ /The package "(.+)" wasn't found on the file system but is built into node/.exec(
148
+ node.text
149
+ );
150
+
151
+ if (match) {
152
+ node.text = `Cannot use "${match[1]}" when deploying to Vercel Edge Functions.`;
153
+ }
154
+ }
155
+ }
156
+
157
+ const formatted = await esbuild.formatMessages(error.errors, {
158
+ kind: 'error',
159
+ color: true
160
+ });
161
+
162
+ console.error(formatted.join('\n'));
163
+
164
+ throw new Error(
165
+ `Bundling with esbuild failed with ${error.errors.length} ${
166
+ error.errors.length === 1 ? 'error' : 'errors'
167
+ }`
168
+ );
169
+ }
123
170
 
124
171
  write(
125
172
  `${dirs.functions}/${name}.func/.vc-config.json`,
@@ -335,6 +382,21 @@ const plugin = function (defaults = {}) {
335
382
  builder.log.minor('Writing routes...');
336
383
 
337
384
  write(`${dir}/config.json`, JSON.stringify(static_config, null, '\t'));
385
+ },
386
+
387
+ supports: {
388
+ // reading from the filesystem only works in serverless functions
389
+ read: ({ config, route }) => {
390
+ const runtime = config.runtime ?? defaults.runtime;
391
+
392
+ if (runtime === 'edge') {
393
+ throw new Error(
394
+ `${name}: Cannot use \`read\` from \`$app/server\` in route \`${route.id}\` configured with \`runtime: 'edge'\``
395
+ );
396
+ }
397
+
398
+ return true;
399
+ }
338
400
  }
339
401
  };
340
402
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/adapter-vercel",
3
- "version": "4.0.5",
3
+ "version": "5.1.0",
4
4
  "description": "A SvelteKit adapter that creates a Vercel app",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,10 +33,10 @@
33
33
  "@types/node": "^18.19.3",
34
34
  "typescript": "^5.3.3",
35
35
  "vitest": "^1.2.0",
36
- "@sveltejs/kit": "^2.3.3"
36
+ "@sveltejs/kit": "^2.4.1"
37
37
  },
38
38
  "peerDependencies": {
39
- "@sveltejs/kit": "^2.0.0"
39
+ "@sveltejs/kit": "^2.4.0"
40
40
  },
41
41
  "scripts": {
42
42
  "lint": "prettier --check .",