Skip to content

Commit 883d4b8

Browse files
authored
[feat] Ignore adapter build files (#1924)
1 parent 485ee23 commit 883d4b8

File tree

15 files changed

+52
-8
lines changed

15 files changed

+52
-8
lines changed

.changeset/big-countries-pump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Add public API to let adapters update .gitignore

documentation/docs/10-adapters.md

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export default {
3131
};
3232
```
3333

34+
Some adapters may modify your project's `.gitignore` to include their build output. In case you don't want those patterns included you can comment them out:
35+
36+
```diff
37+
.svelte-kit
38+
.env
39+
40+
- build
41+
+ # build
42+
```
3443
A variety of official adapters exist for serverless platforms...
3544

3645
- [`adapter-cloudflare-workers`](https://github.com./sveltejs/kit/tree/master/packages/adapter-cloudflare-workers) — for [Cloudflare Workers](https://developers.cloudflare.com/workers/)

documentation/docs/80-adapter-api.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The types for `Adapter` and its parameters are available in [types/config.d.ts](
2626
Within the `adapt` method, there are a number of things that an adapter should do:
2727

2828
- Clear out the build directory
29+
- Call `utils.update_ignores` to ignore build output in existing `.gitignore` files at the location of `svelte.config.js`
2930
- Output code that:
3031
- Calls `init`
3132
- 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

packages/adapter-cloudflare-workers/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default function (options) {
2525

2626
const files = fileURLToPath(new URL('./files', import.meta.url));
2727

28+
utils.update_ignores({ patterns: [bucket, entrypoint] });
29+
2830
utils.rimraf(bucket);
2931
utils.rimraf(entrypoint);
3032

packages/adapter-netlify/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export default function (options) {
2626

2727
const files = fileURLToPath(new URL('./files', import.meta.url));
2828

29+
utils.update_ignores({ patterns: [publish, functions] });
30+
2931
utils.log.minor('Generating serverless function...');
3032
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');
3133

packages/adapter-node/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function ({
4242
name: '@sveltejs/adapter-node',
4343

4444
async adapt({ utils, config }) {
45+
utils.update_ignores({ patterns: [out] });
4546
utils.log.minor('Copying assets');
4647
const static_directory = join(out, 'assets');
4748
utils.copy_client_files(static_directory);

packages/adapter-static/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
1111
name: '@sveltejs/adapter-static',
1212

1313
async adapt({ utils }) {
14+
utils.update_ignores({ patterns: [pages, assets] });
1415
utils.copy_static_files(assets);
1516
utils.copy_client_files(assets);
1617

packages/adapter-static/test/apps/prerendered/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33
/.svelte-kit
44
/build
55
/functions
6+
7+
build

packages/adapter-static/test/apps/spa/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33
/.svelte-kit
44
/build
55
/functions
6+
7+
build

packages/adapter-vercel/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default function (options) {
1919

2020
async adapt({ utils }) {
2121
const dir = '.vercel_build_output';
22+
23+
utils.update_ignores({ patterns: [dir] });
2224
utils.rimraf(dir);
2325

2426
const files = fileURLToPath(new URL('./files', import.meta.url));

packages/create-svelte/shared/+eslint+prettier/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"eslint-config-prettier": "^8.1.0"
44
},
55
"scripts": {
6-
"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
7-
"format": "prettier --write --plugin-search-dir=. ."
6+
"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
7+
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
88
}
99
}

packages/create-svelte/shared/+prettier/.prettierignore

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"lint": "prettier --check --plugin-search-dir=. .",
4-
"format": "prettier --write --plugin-search-dir=. ."
3+
"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. .",
4+
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
55
}
66
}

packages/kit/src/core/adapt/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SVELTE_KIT } from '../constants.js';
22
import { copy, rimraf, mkdirp } from '../filesystem/index.js';
33
import { prerender } from './prerender.js';
4+
import fs from 'fs';
45

56
/**
67
* @param {{
@@ -42,6 +43,25 @@ export function get_utils({ cwd, config, build_data, log }) {
4243
log
4344
});
4445
}
46+
},
47+
48+
/** @param {{patterns: string[], log?: boolean}} options */
49+
update_ignores({ patterns, log = true }) {
50+
const target = '.gitignore';
51+
if (!fs.existsSync(target)) return;
52+
53+
const file = fs.readFileSync(target, { encoding: 'utf-8' });
54+
const eol = file.includes('\r\n') ? '\r\n' : '\n';
55+
const lines = file.split(eol);
56+
const new_lines = new Set(patterns);
57+
// remove repeated lines
58+
for (const line of lines) {
59+
// this will prevent commented ignores to be reinserted
60+
new_lines.delete(line.replace(/#\s*/, ''));
61+
}
62+
if (new_lines.size === 0) return;
63+
fs.writeFileSync(target, [...lines, ...new_lines].join(eol));
64+
if (log) this.log.success(`Updated ${target}`);
4565
}
4666
};
4767
}

packages/kit/types/config.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface AdapterUtils {
1010
copy_server_files: (dest: string) => void;
1111
copy_static_files: (dest: string) => void;
1212
copy: (from: string, to: string, filter?: (basename: string) => boolean) => void;
13+
update_ignores: ({ patterns, log }: { patterns: string[]; log?: boolean }) => void;
1314
prerender: ({
1415
all,
1516
dest,

0 commit comments

Comments
 (0)