Skip to content

Commit 2855027

Browse files
authored
chore: simpler functions-internal cleanup (#8953)
#8899
1 parent 0abb8eb commit 2855027

File tree

2 files changed

+22
-48
lines changed

2 files changed

+22
-48
lines changed

.changeset/perfect-snails-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-netlify': patch
3+
---
4+
5+
chore: simplify functions-internal cleanup

packages/adapter-netlify/index.js

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import {
2-
appendFileSync,
3-
existsSync,
4-
readFileSync,
5-
writeFileSync,
6-
unlinkSync,
7-
createReadStream
8-
} from 'fs';
1+
import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
92
import { dirname, join, resolve, posix } from 'path';
103
import { fileURLToPath } from 'url';
114
import esbuild from 'esbuild';
125
import toml from '@iarna/toml';
13-
import { createInterface } from 'readline';
146

157
/**
168
* @typedef {{
@@ -41,6 +33,8 @@ const edge_set_in_env_var =
4133
process.env.NETLIFY_SVELTEKIT_USE_EDGE === 'true' ||
4234
process.env.NETLIFY_SVELTEKIT_USE_EDGE === '1';
4335

36+
const FUNCTION_PREFIX = 'sveltekit-';
37+
4438
/** @type {import('.').default} */
4539
export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
4640
return {
@@ -59,47 +53,21 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
5953
// "build" is the default publish directory when Netlify detects SvelteKit
6054
const publish = get_publish_directory(netlify_config, builder) || 'build';
6155

62-
const redirects_file_path = join(publish, '_redirects');
63-
64-
// If redirects file exists - empty any netlify generated files in functions-internal
65-
// Without removing other files that may have been auto generated by integrations
66-
if (existsSync(redirects_file_path)) {
67-
// Read each line of the file
68-
const fileStream = createReadStream(redirects_file_path);
69-
const rl = createInterface({
70-
input: fileStream,
71-
crlfDelay: Infinity
72-
});
73-
74-
// Create an array of lines
75-
const lines = [];
76-
for await (const line of rl) {
77-
lines.push(line);
78-
}
79-
80-
const functions_internal = join('.netlify', 'functions-internal');
81-
82-
// Loop through redirects, and delete corresponding functions-internal files
83-
lines.forEach((line) => {
84-
if (line) {
85-
// example line /.netlify/functions/{function_name} 200
86-
const path = line.split(' ')[1];
87-
const function_name = path.split('/').pop();
88-
const mjsFile = join(functions_internal, `${function_name}.mjs`);
89-
const jsonFile = join(functions_internal, `${function_name}.json`);
90-
if (existsSync(mjsFile)) unlinkSync(mjsFile);
91-
if (existsSync(jsonFile)) unlinkSync(jsonFile);
92-
}
93-
});
94-
}
95-
9656
// empty out existing build directories
9757
builder.rimraf(publish);
9858
builder.rimraf('.netlify/edge-functions');
9959
builder.rimraf('.netlify/server');
10060
builder.rimraf('.netlify/package.json');
10161
builder.rimraf('.netlify/serverless.js');
10262

63+
if (existsSync('.netlify/functions-internal')) {
64+
for (const file of readdirSync('.netlify/functions-internal')) {
65+
if (file.startsWith(FUNCTION_PREFIX)) {
66+
builder.rimraf(join('.netlify/functions-internal', file));
67+
}
68+
}
69+
}
70+
10371
builder.log.minor(`Publishing to "${publish}"`);
10472

10573
builder.log.minor('Copying assets...');
@@ -195,7 +163,7 @@ async function generate_edge_functions({ builder }) {
195163
* @param { boolean } params.split
196164
*/
197165
async function generate_lambda_functions({ builder, publish, split }) {
198-
builder.mkdirp('.netlify/functions-internal');
166+
builder.mkdirp('.netlify/functions-internal/.svelte-kit');
199167

200168
/** @type {string[]} */
201169
const redirects = [];
@@ -236,7 +204,8 @@ async function generate_lambda_functions({ builder, publish, split }) {
236204
}
237205

238206
const pattern = `/${parts.join('/')}`;
239-
const name = parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index';
207+
const name =
208+
FUNCTION_PREFIX + parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index';
240209

241210
// skip routes with identical patterns, they were already folded into another function
242211
if (seen.has(pattern)) continue;
@@ -271,9 +240,9 @@ async function generate_lambda_functions({ builder, publish, split }) {
271240

272241
const fn = `import { init } from '../serverless.js';\n\nexport const handler = init(${manifest});\n`;
273242

274-
writeFileSync(`.netlify/functions-internal/render.json`, fn_config);
275-
writeFileSync('.netlify/functions-internal/render.mjs', fn);
276-
redirects.push('* /.netlify/functions/render 200');
243+
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.json`, fn_config);
244+
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`, fn);
245+
redirects.push(`* /.netlify/functions/${FUNCTION_PREFIX}render 200`);
277246
}
278247

279248
// this should happen at the end, after builder.writeClient(...),

0 commit comments

Comments
 (0)