Skip to content

Commit

Permalink
chore: simpler functions-internal cleanup (#8953)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Feb 9, 2023
1 parent 0abb8eb commit 2855027
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-snails-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

chore: simplify functions-internal cleanup
65 changes: 17 additions & 48 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -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 {{
Expand Down Expand Up @@ -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 {
Expand All @@ -59,47 +53,21 @@ 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');
builder.rimraf('.netlify/server');
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...');
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(...),
Expand Down

0 comments on commit 2855027

Please sign in to comment.