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

[feat] Ignore adapter build files #1924

Merged
merged 43 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9d0911e
add api to let adapters modify the user's .gitignore
JeanJPNM Jul 14, 2021
b5e2f86
update documentation
JeanJPNM Jul 14, 2021
81994dd
Merge branch 'master' into adapter-file-ignore
JeanJPNM Jul 16, 2021
9c96539
handle changes in ignored files
JeanJPNM Jul 16, 2021
1443779
remove test code
JeanJPNM Jul 16, 2021
cb1d390
support updating multiple files
JeanJPNM Jul 16, 2021
7ef9b75
add TODO
JeanJPNM Jul 16, 2021
c436c63
add changeset
JeanJPNM Jul 16, 2021
572b1a2
use fs instead of fs/promises
JeanJPNM Jul 16, 2021
9ce434e
update documentation
JeanJPNM Jul 16, 2021
7ad5add
fix error inside loop
JeanJPNM Jul 16, 2021
0440983
fix empty line at the start of a file
JeanJPNM Jul 16, 2021
f2571e1
move function to utils object
JeanJPNM Jul 16, 2021
62d4ba5
update adapter-node
JeanJPNM Jul 16, 2021
7d77989
update adapter-static
JeanJPNM Jul 16, 2021
b9ebbc0
allow users to comment out ignore patterns
JeanJPNM Jul 16, 2021
fe432a7
fix line verification
JeanJPNM Jul 16, 2021
34c65eb
improve delimeter title
JeanJPNM Jul 16, 2021
e511a18
update docs
JeanJPNM Jul 16, 2021
a75f5ae
update adapter-vercel
JeanJPNM Jul 16, 2021
5817df0
update adapter-cloudfare-workers
JeanJPNM Jul 16, 2021
fc0b94b
updata adapter-netlify
JeanJPNM Jul 16, 2021
b4f3a1b
update adapter-begin
JeanJPNM Jul 16, 2021
ebd3df6
add todo
JeanJPNM Jul 16, 2021
8d6a883
put logging inside function
JeanJPNM Jul 17, 2021
1b8bee2
update adapters
JeanJPNM Jul 17, 2021
191e9c3
split lines with optional \r
JeanJPNM Jul 18, 2021
ff76519
check for patterns on the whole file
JeanJPNM Jul 18, 2021
517b254
minimal viable code
JeanJPNM Jul 20, 2021
235e608
remove old comment
JeanJPNM Jul 21, 2021
99fac63
replace \n by EOL
JeanJPNM Jul 21, 2021
14561a1
update docs
JeanJPNM Jul 22, 2021
5e57aae
update adapter api doc
JeanJPNM Jul 22, 2021
57ddbbb
clarify file location
JeanJPNM Jul 23, 2021
f581003
undo adapter-begin update
JeanJPNM Jul 26, 2021
bd9613d
check for file's eol
JeanJPNM Jul 28, 2021
d74eeee
target only .gitignore
JeanJPNM Jul 28, 2021
4403cb9
change ignore path to .gitignore for prettier
JeanJPNM Jul 28, 2021
e587a92
Update documentation/docs/80-adapter-api.md
JeanJPNM Jul 28, 2021
4e9c146
update `.gitignore` files on test apps
JeanJPNM Jul 28, 2021
cf903e5
update adapter section of documentation
JeanJPNM Jul 28, 2021
50cb7d8
update changeset
JeanJPNM Jul 28, 2021
445b992
add end of line on test apps `.gitignore`
JeanJPNM Jul 28, 2021
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/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 patform'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 @@ -16,6 +16,8 @@ export default function () {

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 @@ -17,6 +17,8 @@ export default function () {

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 @@ -36,6 +36,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 @@ -10,6 +10,8 @@ export default function () {

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';

/**
*
Expand Down Expand Up @@ -47,6 +48,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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also update the templates to use .gitignore. It looks like eslint might already, but not prettier. https://github.com/sveltejs/kit/tree/master/packages/create-svelte/shared

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 @@ -9,6 +9,7 @@ export type 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