Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simpler functions-internal cleanup #8953

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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