Skip to content

Commit

Permalink
[feat] Ignore adapter build files (#1924)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanJPNM authored Jul 29, 2021
1 parent 485ee23 commit 883d4b8
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-countries-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add public API to let adapters update .gitignore
9 changes: 9 additions & 0 deletions documentation/docs/10-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export default {
};
```

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:

```diff
.svelte-kit
.env

- build
+ # build
```
A variety of official adapters exist for serverless platforms...

- [`adapter-cloudflare-workers`](https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers) — for [Cloudflare Workers](https://developers.cloudflare.com/workers/)
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/80-adapter-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-static/test/apps/prerendered/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
/.svelte-kit
/build
/functions

build
2 changes: 2 additions & 0 deletions packages/adapter-static/test/apps/spa/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
/.svelte-kit
/build
/functions

build
2 changes: 2 additions & 0 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions packages/create-svelte/shared/+eslint+prettier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"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=. ."
}
}
4 changes: 0 additions & 4 deletions packages/create-svelte/shared/+prettier/.prettierignore

This file was deleted.

4 changes: 2 additions & 2 deletions packages/create-svelte/shared/-eslint+prettier/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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=. ."
}
}
20 changes: 20 additions & 0 deletions packages/kit/src/core/adapt/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SVELTE_KIT } from '../constants.js';
import { copy, rimraf, mkdirp } from '../filesystem/index.js';
import { prerender } from './prerender.js';
import fs from 'fs';

/**
* @param {{
Expand Down Expand Up @@ -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}`);
}
};
}
1 change: 1 addition & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 883d4b8

Please sign in to comment.