diff --git a/.changeset/perfect-snails-crash.md b/.changeset/perfect-snails-crash.md new file mode 100644 index 000000000000..a7456faf70c2 --- /dev/null +++ b/.changeset/perfect-snails-crash.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-netlify': patch +--- + +chore: simplify functions-internal cleanup diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 987d55ca0dee..2d2f91a98cb4 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -1,16 +1,8 @@ -import { - appendFileSync, - existsSync, - readFileSync, - writeFileSync, - unlinkSync, - createReadStream -} from 'fs'; +import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'; import { dirname, join, resolve, posix } from 'path'; import { fileURLToPath } from 'url'; import esbuild from 'esbuild'; import toml from '@iarna/toml'; -import { createInterface } from 'readline'; /** * @typedef {{ @@ -41,6 +33,8 @@ const edge_set_in_env_var = process.env.NETLIFY_SVELTEKIT_USE_EDGE === 'true' || process.env.NETLIFY_SVELTEKIT_USE_EDGE === '1'; +const FUNCTION_PREFIX = 'sveltekit-'; + /** @type {import('.').default} */ export default function ({ split = false, edge = edge_set_in_env_var } = {}) { return { @@ -59,40 +53,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) { // "build" is the default publish directory when Netlify detects SvelteKit const publish = get_publish_directory(netlify_config, builder) || 'build'; - const redirects_file_path = join(publish, '_redirects'); - - // If redirects file exists - empty any netlify generated files in functions-internal - // Without removing other files that may have been auto generated by integrations - if (existsSync(redirects_file_path)) { - // Read each line of the file - const fileStream = createReadStream(redirects_file_path); - const rl = createInterface({ - input: fileStream, - crlfDelay: Infinity - }); - - // Create an array of lines - const lines = []; - for await (const line of rl) { - lines.push(line); - } - - const functions_internal = join('.netlify', 'functions-internal'); - - // Loop through redirects, and delete corresponding functions-internal files - lines.forEach((line) => { - if (line) { - // example line /.netlify/functions/{function_name} 200 - const path = line.split(' ')[1]; - const function_name = path.split('/').pop(); - const mjsFile = join(functions_internal, `${function_name}.mjs`); - const jsonFile = join(functions_internal, `${function_name}.json`); - if (existsSync(mjsFile)) unlinkSync(mjsFile); - if (existsSync(jsonFile)) unlinkSync(jsonFile); - } - }); - } - // empty out existing build directories builder.rimraf(publish); builder.rimraf('.netlify/edge-functions'); @@ -100,6 +60,14 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) { builder.rimraf('.netlify/package.json'); builder.rimraf('.netlify/serverless.js'); + if (existsSync('.netlify/functions-internal')) { + for (const file of readdirSync('.netlify/functions-internal')) { + if (file.startsWith(FUNCTION_PREFIX)) { + builder.rimraf(join('.netlify/functions-internal', file)); + } + } + } + builder.log.minor(`Publishing to "${publish}"`); builder.log.minor('Copying assets...'); @@ -195,7 +163,7 @@ async function generate_edge_functions({ builder }) { * @param { boolean } params.split */ async function generate_lambda_functions({ builder, publish, split }) { - builder.mkdirp('.netlify/functions-internal'); + builder.mkdirp('.netlify/functions-internal/.svelte-kit'); /** @type {string[]} */ const redirects = []; @@ -236,7 +204,8 @@ async function generate_lambda_functions({ builder, publish, split }) { } const pattern = `/${parts.join('/')}`; - const name = parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index'; + const name = + FUNCTION_PREFIX + parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index'; // skip routes with identical patterns, they were already folded into another function if (seen.has(pattern)) continue; @@ -271,9 +240,9 @@ async function generate_lambda_functions({ builder, publish, split }) { const fn = `import { init } from '../serverless.js';\n\nexport const handler = init(${manifest});\n`; - writeFileSync(`.netlify/functions-internal/render.json`, fn_config); - writeFileSync('.netlify/functions-internal/render.mjs', fn); - redirects.push('* /.netlify/functions/render 200'); + writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.json`, fn_config); + writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`, fn); + redirects.push(`* /.netlify/functions/${FUNCTION_PREFIX}render 200`); } // this should happen at the end, after builder.writeClient(...),