[feat] Ignore adapter build files
ADD
.changeset/big-countries-pump.md
+---
+'@sveltejs/kit': patch
+---
+
+Add public API to let adapters update .gitignore

MODIFY
documentation/docs/80-adapter-api.md
documentation/docs/80-adapter-api.md
@@ -26,6 +26,7 @@ The types for `Adapter` and its parameters are available in [types/config.d.ts](
Within the `adapt` method, there are a number of things that an adapter should do:
- Clear out the build directory
+- Call `utils.update_ignores` to ignore build output in existing `.gitignore` files at the location of `svelte.config.js`
- Output code that:
- Calls `init`
- Converts from the platform's request to a [SvelteKit request](#hooks-handle), calls `render`, and converts from a [SvelteKit response](#hooks-handle) to the platform's

MODIFY
packages/adapter-cloudflare-workers/index.js
packages/adapter-cloudflare-workers/index.js
@@ -25,6 +25,8 @@ export default function (options) {
const files = fileURLToPath(new URL('./files', import.meta.url));
+ utils.update_ignores({ patterns: [bucket, entrypoint] });
+
utils.rimraf(bucket);
utils.rimraf(entrypoint);

MODIFY
packages/adapter-netlify/index.js
packages/adapter-netlify/index.js
@@ -26,6 +26,8 @@ export default function (options) {
const files = fileURLToPath(new URL('./files', import.meta.url));
+ utils.update_ignores({ patterns: [publish, functions] });
+
utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');

MODIFY
packages/adapter-node/index.js
packages/adapter-node/index.js
@@ -42,6 +42,7 @@ export default function ({
name: '@sveltejs/adapter-node',
async adapt({ utils, config }) {
+ utils.update_ignores({ patterns: [out] });
utils.log.minor('Copying assets');
const static_directory = join(out, 'assets');
utils.copy_client_files(static_directory);

MODIFY
packages/adapter-static/index.js
packages/adapter-static/index.js
@@ -11,6 +11,7 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
name: '@sveltejs/adapter-static',
async adapt({ utils }) {
+ utils.update_ignores({ patterns: [pages, assets] });
utils.copy_static_files(assets);
utils.copy_client_files(assets);

MODIFY
packages/adapter-static/test/apps/prerendered/.gitignore
packages/adapter-static/test/apps/prerendered/.gitignore
@@ -3,3 +3,5 @@ node_modules
/.svelte-kit
/build
/functions
+
+build

MODIFY
packages/adapter-static/test/apps/spa/.gitignore
packages/adapter-static/test/apps/spa/.gitignore
@@ -3,3 +3,5 @@ node_modules
/.svelte-kit
/build
/functions
+
+build

MODIFY
packages/adapter-vercel/index.js
packages/adapter-vercel/index.js
@@ -19,6 +19,8 @@ export default function (options) {
async adapt({ utils }) {
const dir = '.vercel_build_output';
+
+ utils.update_ignores({ patterns: [dir] });
utils.rimraf(dir);
const files = fileURLToPath(new URL('./files', import.meta.url));

MODIFY
packages/create-svelte/shared/+eslint+prettier/package.json
packages/create-svelte/shared/+eslint+prettier/package.json
"eslint-config-prettier": "^8.1.0"
},
"scripts": {
- "lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
- "format": "prettier --write --plugin-search-dir=. ."
+ "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
+ "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
}
}

DELETE
packages/create-svelte/shared/+prettier/.prettierignore
-.svelte-kit/**
-static/**
-build/**
-node_modules/**

MODIFY
packages/create-svelte/shared/-eslint+prettier/package.json
packages/create-svelte/shared/-eslint+prettier/package.json
{
"scripts": {
- "lint": "prettier --check --plugin-search-dir=. .",
- "format": "prettier --write --plugin-search-dir=. ."
+ "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. .",
+ "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
}
}

MODIFY
packages/kit/src/core/adapt/utils.js
packages/kit/src/core/adapt/utils.js
import { SVELTE_KIT } from '../constants.js';
import { copy, rimraf, mkdirp } from '../filesystem/index.js';
import { prerender } from './prerender.js';
+import fs from 'fs';
/**
* @param {{
@@ -42,6 +43,25 @@ export function get_utils({ cwd, config, build_data, log }) {
log
});
}
+ },
+
+ /** @param {{patterns: string[], log?: boolean}} options */
+ update_ignores({ patterns, log = true }) {
+ const target = '.gitignore';
+ if (!fs.existsSync(target)) return;
+
+ const file = fs.readFileSync(target, { encoding: 'utf-8' });
+ const eol = file.includes('\r\n') ? '\r\n' : '\n';
+ const lines = file.split(eol);
+ const new_lines = new Set(patterns);
+ // remove repeated lines
+ for (const line of lines) {
+ // this will prevent commented ignores to be reinserted
+ new_lines.delete(line.replace(/#\s*/, ''));
+ }
+ if (new_lines.size === 0) return;
+ fs.writeFileSync(target, [...lines, ...new_lines].join(eol));
+ if (log) this.log.success(`Updated ${target}`);
}
};
}

MODIFY
packages/kit/types/config.d.ts
packages/kit/types/config.d.ts
@@ -10,6 +10,7 @@ export interface AdapterUtils {
copy_server_files: (dest: string) => void;
copy_static_files: (dest: string) => void;
copy: (from: string, to: string, filter?: (basename: string) => boolean) => void;
+ update_ignores: ({ patterns, log }: { patterns: string[]; log?: boolean }) => void;
prerender: ({
all,
dest,

[feat] Ignore adapter build files (#1924)