Skip to content

Commit

Permalink
Implement breaking svelte-kit changes (#50)
Browse files Browse the repository at this point in the history
Implement the breaking changes necessary to upgrade svelte-kit. Main focus is the new routes structure. Check https://kit.svelte.dev/docs/introduction for info.

Also, `svelte-kit` now requires `vite` as a separate dependency. This fixes that.

Moved the API_URL building logic to `vite.config.ts` also.

I also noticed that the LICENSE file was not matching to the license in `frontend`. AFAIK we should be using MPL license, which saves us from people copying the design but still gives us zero responsibility in terms of warranty and zero liability.
  • Loading branch information
jfranciscosousa authored Sep 2, 2022
1 parent 7f3e302 commit 2a2ecab
Show file tree
Hide file tree
Showing 21 changed files with 1,087 additions and 575 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nodejs 16.14.0
nodejs 16.17.0
elixir 1.13.3
erlang 24.3.4
ruby 2.6.6
357 changes: 353 additions & 4 deletions LICENSE.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
/.svelte-kit
/build
/functions
.netlify
2 changes: 1 addition & 1 deletion frontend/.node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.2.0
16.17.0
1 change: 0 additions & 1 deletion frontend/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
engine-strict=true
3 changes: 1 addition & 2 deletions frontend/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[build]
ignore = "/bin/false"
command = "yarn build"
publish = "build/"
functions = "functions/"

[functions]
node_bundler = "esbuild"
37 changes: 18 additions & 19 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
{
"name": "secrets-frontend",
"license": "MPL 2.0",
"license": "MPL-2.0",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
"preview": "svelte-kit preview",
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
"format": "prettier --write --plugin-search-dir=. ."
},
"devDependencies": {
"@sveltejs/adapter-netlify": "next",
"@sveltejs/kit": "next",
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"autoprefixer": "^10.4.7",
"cssnano": "^5.1.11",
"eslint": "^8.17.0",
"@sveltejs/adapter-netlify": "1.0.0-next.75",
"@sveltejs/kit": "^1.0.0-next.455",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"autoprefixer": "^10.4.8",
"cssnano": "^5.1.13",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte3": "^4.0.0",
"postcss": "^8.4.14",
"postcss-load-config": "^4.0.1",
"prettier": "~2.7.1",
"prettier-plugin-svelte": "^2.7.0",
"prettier-plugin-tailwindcss": "^0.1.12",
"prettier-plugin-tailwindcss": "^0.1.13",
"svelte": "^3.49.0",
"svelte-preprocess": "^4.10.7",
"tailwindcss": "^3.1.3",
"tailwindcss": "^3.1.8",
"tslib": "^2.4.0",
"typescript": "^4.7.3"
"typescript": "^4.8.2",
"vite": "^3.0.0"
},
"type": "module",
"dependencies": {
"uuid": "^8.3.2"
}
"dependencies": {}
}
3 changes: 0 additions & 3 deletions frontend/postcss.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ const dev = mode === 'development';

module.exports = {
plugins: [
// Some plugins, like postcss-nested, need to run before Tailwind
tailwindcss,
// But others, like autoprefixer, need to run after
autoprefixer,

!dev &&
cssnano({
preset: 'default'
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
<meta name="viewport" content="width=device-width" />

%sveltekit.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
<div>%sveltekit.body%</div>
</body>
</html>
27 changes: 16 additions & 11 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const API_URL = import.meta.env.VITE_API_URL;

export async function createSecret({
secret,
expiry
}: {
secret: string;
expiry: number;
}): Promise<string> {
export type fetchType = (info: RequestInfo, init?: RequestInit) => Promise<Response>;

export async function createSecret(
{
secret,
expiry
}: {
secret: string;
expiry: number;
},
fetch = globalThis.fetch
): Promise<string> {
const response = await fetch(`${API_URL}/api/secrets`, {
method: 'POST',
body: JSON.stringify({ secret, expiry }),
Expand All @@ -21,13 +26,13 @@ export async function createSecret({
return body.room_id;
}

export async function checkIfRoomExists(room: string): Promise<boolean> {
export async function checkIfRoomExists(room: string, fetch = globalThis.fetch): Promise<boolean> {
const response = await fetch(`${API_URL}/api/secrets/${room}`, { method: 'HEAD' });

return response.status === 200;
}

export async function getRoomSecret(room: string): Promise<string> {
export async function getRoomSecret(room: string, fetch = globalThis.fetch): Promise<string> {
let response, body;

try {
Expand All @@ -43,15 +48,15 @@ export async function getRoomSecret(room: string): Promise<string> {
return body.secret;
}

export async function deleteSecret(room: string): Promise<void> {
export async function deleteSecret(room: string, fetch = globalThis.fetch): Promise<void> {
try {
await fetch(`${API_URL}/api/secrets/${room}`, { method: 'DELETE' });
} catch (error) {
throw 'Something exploded!';
}
}

export async function getStats(): Promise<{ secretsCounter: string }> {
export async function getStats(fetch = globalThis.fetch): Promise<{ secretsCounter: string }> {
try {
const response = await fetch(`${API_URL}/api/stats`);
const json = await response.json();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
<script context="module" lang="ts">
import type { Load } from '@sveltejs/kit';
import { getStats } from '$lib/api';
export const load: Load = async () => {
const stats = await getStats();
return {
props: stats
};
};
</script>

<script lang="ts">
import type { LayoutData } from './$types';
import SecretsLogo from '$lib/components/SecretsLogo.svelte';
import FiniamI from '$lib/components/FiniamI.svelte';
import FiniamM from '$lib/components/FiniamM.svelte';
import '../app.css';
export let secretsCounter: number;
export let data: LayoutData;
let { secretsCounter } = data;
</script>

<svelte:head>
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getStats } from '$lib/api';
import type { LayoutLoad } from './$types';

export const load: LayoutLoad = async ({ fetch }) => {
const stats = await getStats(fetch);

return stats;
};
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@
bind:value={expiry}
>
<!-- Disable for now, while we use a non-persistent redis
<option value="86400">1 day</option>
<option value="604800">7 days</option>
-->
<option value="86400">1 day</option>
<option value="604800">7 days</option>
-->
<option value="21600">6 hours </option>
<option value="3600">1 hour</option>
<option value="1800">30 min</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
<script context="module" lang="ts">
import type { Load } from '@sveltejs/kit';
export const load: Load = async ({ page }) => {
return {
props: { room: page.params.room, roomExists: await checkIfRoomExists(page.params.room) }
};
};
</script>

<script lang="ts">
// @hmr:keep-all
import type { PageData } from './$types';
import SEO from '$lib/components/SEO.svelte';
import Button from '$lib/components/Button.svelte';
import CopyButton from '$lib/components/CopyButton.svelte';
import { decryptData } from '$lib/crypto';
import { getRoomSecret, checkIfRoomExists } from '$lib/api';
import { getRoomSecret } from '$lib/api';
import { goto } from '$app/navigation';
export let room: string;
export let roomExists: boolean;
export let data: PageData;
let { room, roomExists } = data;
let loading = false;
let decryptedSecret: string;
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/routes/[room]/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { checkIfRoomExists } from '$lib/api';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ params, fetch }) => {
return { room: params.room, roomExists: await checkIfRoomExists(params.room, fetch) };
};
File renamed without changes.
19 changes: 0 additions & 19 deletions frontend/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import preprocess from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-netlify';

function buildApiUrl() {
if (process.env.NODE_ENV === 'development') {
return 'http://localhost:4000';
}

const prNumber = process.env['REVIEW_ID'];

if (prNumber) return `https://finiam-secrets-pr-${prNumber}.herokuapp.com`;

return `https://finiam-secrets.herokuapp.com`;
}

const API_URL = buildApiUrl();

console.log('RUNNING WITH THE FOLLOWING API_URL', API_URL);

process.env.VITE_API_URL = API_URL;

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
Expand All @@ -30,7 +12,6 @@ const config = {
],

kit: {
target: '#svelte',
adapter: adapter()
}
};
Expand Down
3 changes: 1 addition & 2 deletions frontend/tailwind.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
mode: 'jit',
purge: ['./src/**/*.{html,js,svelte,ts}'],
content: ['./src/**/*.{html,js,svelte,ts}'],
theme: {
extend: {
colors: {
Expand Down
29 changes: 1 addition & 28 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020"],
"target": "es2019",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
To have warnings/errors of the Svelte compiler at the correct position,
enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
26 changes: 26 additions & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, type UserConfig } from 'vite';

export default defineConfig(({ mode }) => {
function buildApiUrl() {
if (mode === 'development') {
return 'http://localhost:4000';
}

const prNumber = process.env['REVIEW_ID'];

if (prNumber) return `https://finiam-secrets-pr-${prNumber}.herokuapp.com`;

return `https://finiam-secrets.herokuapp.com`;
}

const apiUrl = buildApiUrl();
console.log('RUNNING WITH THE FOLLOWING API_URL', apiUrl);
process.env.VITE_API_URL = apiUrl;

const config: UserConfig = {
plugins: [...sveltekit()]
};

return config;
});
Loading

0 comments on commit 2a2ecab

Please sign in to comment.