Skip to content

Commit 431be7f

Browse files
committed
feat: Dowdload Svelte App with Tailwind CSS and fix app.css bug along side
1 parent 8f84350 commit 431be7f

File tree

7 files changed

+115
-9
lines changed

7 files changed

+115
-9
lines changed

apps/svelte.dev/.env

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
SUPABASE_URL=
2-
SUPABASE_KEY=
1+
SUPABASE_URL=https://fgffbwzjvuacbgumcsge.supabase.co
2+
SUPABASE_KEY=rBCAuM33T4pdrhm1Y3hzGnlxWPfLBb8lI3ZHa3yIuphBBx9DT9/zf7/AuYgP3M3Llst030ect/xZEKiTuer/0w==
33

4-
GITHUB_CLIENT_ID=
5-
GITHUB_CLIENT_SECRET=
4+
GITHUB_CLIENT_ID=Ov23ligzz3gnuw49XeqW
5+
GITHUB_CLIENT_SECRET=98d6876b2cd82f95b01141e6361ebeb8ac453275
66

77
# disable prerendering with `PRERENDER=false pnpm build` — this is useful for speeding up builds when previewing locally
88
PRERENDER=true

apps/svelte.dev/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/src/routes/_home/Supporters/donors.js
99
/scripts/svelte-template
1010
/static/svelte-template.json
11+
/static/svelte-tailwind-template.json
1112

1213
# git-repositories of synced docs go here
1314
/repos/

apps/svelte.dev/content/docs/svelte/07-misc/07-v5-migration-guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,9 @@ Svelte 5 is more strict about the HTML structure and will throw a compiler error
834834

835835
Assignments to destructured parts of a `@const` declaration are no longer allowed. It was an oversight that this was ever allowed.
836836

837-
### :is(...) and :where(...) are scoped
837+
### :is(...), :has(...), and :where(...) are scoped
838838

839-
Previously, Svelte did not analyse selectors inside `:is(...)` and `:where(...)`, effectively treating them as global. Svelte 5 analyses them in the context of the current component. As such, some selectors may now be treated as unused if they were relying on this treatment. To fix this, use `:global(...)` inside the `:is(...)/:where(...)` selectors.
839+
Previously, Svelte did not analyse selectors inside `:is(...)`, `:has(...)`, and `:where(...)`, effectively treating them as global. Svelte 5 analyses them in the context of the current component. As such, some selectors may now be treated as unused if they were relying on this treatment. To fix this, use `:global(...)` inside the `:is(...)/:has(...)/:where(...)` selectors.
840840

841841
When using Tailwind's `@apply` directive, add a `:global` selector to preserve rules that use Tailwind-generated `:is(...)` selectors:
842842
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// @ts-check
2+
import { readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
3+
import { join } from 'node:path';
4+
import { fileURLToPath } from 'node:url';
5+
import { create } from 'sv';
6+
7+
// This download the currente Vite template from Github, adjusts it to our needs, and saves it to static/svelte-template.json
8+
// This is used by the Svelte REPL as part of the "download project" feature
9+
10+
const viteConfigTailwind =
11+
"import { sveltekit } from '@sveltejs/kit/vite';\nimport { defineConfig } from 'vite';\nimport tailwindcss from '@tailwindcss/vite'\nexport default defineConfig({\n\tplugins: [sveltekit(),tailwindcss()]\n});\n";
12+
13+
const force = process.env.FORCE_UPDATE === 'true';
14+
const output_file = fileURLToPath(
15+
new URL('../static/svelte-tailwind-template.json', import.meta.url)
16+
);
17+
const output_dir = fileURLToPath(new URL('./svelte-tailwind-template', import.meta.url));
18+
19+
try {
20+
if (!force && statSync(output_file)) {
21+
console.info(`[update/template] ${output_file} exists. Skipping`);
22+
process.exit(0);
23+
}
24+
} catch {
25+
// create Svelte-Kit skelton app
26+
create(output_dir, { template: 'minimal', types: 'typescript', name: 'your-app' });
27+
28+
function get_all_files(dir) {
29+
const files = [];
30+
const items = readdirSync(dir, { withFileTypes: true });
31+
32+
for (const item of items) {
33+
const full_path = join(dir, item.name);
34+
if (item.isDirectory()) {
35+
files.push(...get_all_files(full_path));
36+
} else {
37+
files.push(full_path.replaceAll('\\', '/'));
38+
}
39+
}
40+
41+
return files;
42+
}
43+
44+
const all_files = get_all_files(output_dir);
45+
const files = [];
46+
47+
for (let path of all_files) {
48+
const bytes = readFileSync(path);
49+
const string = bytes.toString();
50+
let data = bytes.compare(Buffer.from(string)) === 0 ? string : [...bytes];
51+
52+
// vite config to use along with Tailwind CSS
53+
if (path.endsWith('vite.config.ts')) {
54+
files.push({
55+
path: 'vite.config.ts',
56+
data: viteConfigTailwind
57+
});
58+
}
59+
60+
// add Tailwind CSS as devDependencies
61+
if (path.endsWith('package.json')) {
62+
try {
63+
const pkg = JSON.parse(string);
64+
65+
pkg.devDependencies ||= {};
66+
pkg.devDependencies['tailwindcss'] = '^4.1.8';
67+
pkg.devDependencies['@tailwindcss/vite'] = '^4.1.8';
68+
69+
data = JSON.stringify(pkg, null, 2); // Pretty-print with 2 spaces
70+
} catch (err) {
71+
console.error('Failed to parse package.json:', err);
72+
}
73+
}
74+
75+
if (path.endsWith('routes/+page.svelte')) {
76+
data = `<script>\n\timport '../app.css';\n\timport App from './App.svelte';\n</script>\n\n<App />\n`;
77+
}
78+
79+
files.push({ path: path.slice(output_dir.length + 1), data });
80+
}
81+
82+
files.push({
83+
path: 'src/routes/+page.js',
84+
data:
85+
"// Because we don't know whether or not your playground app can run in a server environment, we disable server-side rendering.\n" +
86+
'// Make sure to test whether or not you can re-enable it, as SSR improves perceived performance and site accessibility.\n' +
87+
'// Read more about this option here: https://svelte.dev/docs/kit/page-options#ssr\n' +
88+
'export const ssr = false;\n'
89+
});
90+
91+
// add CSS styles from playground to the project
92+
93+
files.push({
94+
path: 'src/app.css',
95+
data: '@import "tailwindcss";'
96+
});
97+
98+
writeFileSync(output_file, JSON.stringify(files));
99+
100+
// remove output dir afterwards to prevent it messing with Vite watcher
101+
rmSync(output_dir, { force: true, recursive: true });
102+
}

apps/svelte.dev/scripts/get_svelte_template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ try {
6666
{ encoding: 'utf-8' }
6767
);
6868
const css = html
69-
.slice(html.indexOf('<style>') + 7, html.indexOf('</style>'))
69+
.slice(html.indexOf('<style id="injected">') + 19, html.indexOf('</style>'))
7070
.split('\n')
7171
.map((line) =>
7272
// remove leading \t

apps/svelte.dev/scripts/update.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ const env = {
99

1010
fork(`${dir}/get_contributors.js`, { env });
1111
fork(`${dir}/get_donors.js`, { env });
12+
fork(`${dir}/get_svelte_tailwind_template.js`, { env });
1213
fork(`${dir}/get_svelte_template.js`, { env });

apps/svelte.dev/src/routes/(authed)/playground/[id]/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@
9797
}
9898
9999
async function download() {
100-
const { files: components, imports } = repl.toJSON();
100+
const { files: components, imports, tailwind } = repl.toJSON();
101101
102102
const files: Array<{ path: string; data: string }> = await (
103-
await fetch('/svelte-template.json')
103+
tailwind
104+
? await fetch('/svelte-tailwind-template.json')
105+
: await fetch('/svelte-template.json')
104106
).json();
105107
106108
if (imports.length > 0) {

0 commit comments

Comments
 (0)