Skip to content

feat: Download Svelte App with Tailwind CSS and fix app.css bug along side #1374

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions apps/svelte.dev/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/src/routes/_home/Supporters/donors.js
/scripts/svelte-template
/static/svelte-template.json
/static/svelte-tailwind-template.json

# git-repositories of synced docs go here
/repos/
Expand Down
102 changes: 102 additions & 0 deletions apps/svelte.dev/scripts/get_svelte_tailwind_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// @ts-check
import { readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { create } from 'sv';

// This download the currente Vite template from Github, adjusts it to our needs, and saves it to static/svelte-template.json
// This is used by the Svelte REPL as part of the "download project" feature

const viteConfigTailwind =
"import { sveltekit } from '@sveltejs/kit/vite';\nimport { defineConfig } from 'vite';\nimport tailwindcss from '@tailwindcss/vite'\nexport default defineConfig({\n\tplugins: [sveltekit(),tailwindcss()]\n});\n";

const force = process.env.FORCE_UPDATE === 'true';
const output_file = fileURLToPath(
new URL('../static/svelte-tailwind-template.json', import.meta.url)
);
const output_dir = fileURLToPath(new URL('./svelte-tailwind-template', import.meta.url));

try {
if (!force && statSync(output_file)) {
console.info(`[update/template] ${output_file} exists. Skipping`);
process.exit(0);
}
} catch {
// create Svelte-Kit skelton app
create(output_dir, { template: 'minimal', types: 'typescript', name: 'your-app' });

function get_all_files(dir) {
const files = [];
const items = readdirSync(dir, { withFileTypes: true });

for (const item of items) {
const full_path = join(dir, item.name);
if (item.isDirectory()) {
files.push(...get_all_files(full_path));
} else {
files.push(full_path.replaceAll('\\', '/'));
}
}

return files;
}

const all_files = get_all_files(output_dir);
const files = [];

for (let path of all_files) {
const bytes = readFileSync(path);
const string = bytes.toString();
let data = bytes.compare(Buffer.from(string)) === 0 ? string : [...bytes];

// vite config to use along with Tailwind CSS
if (path.endsWith('vite.config.ts')) {
files.push({
path: 'vite.config.ts',
data: viteConfigTailwind
});
}

// add Tailwind CSS as devDependencies
if (path.endsWith('package.json')) {
try {
const pkg = JSON.parse(string);

pkg.devDependencies ||= {};
pkg.devDependencies['tailwindcss'] = '^4.1.8';
pkg.devDependencies['@tailwindcss/vite'] = '^4.1.8';

data = JSON.stringify(pkg, null, 2); // Pretty-print with 2 spaces
} catch (err) {
console.error('Failed to parse package.json:', err);
}
}

if (path.endsWith('routes/+page.svelte')) {
data = `<script>\n\timport '../app.css';\n\timport App from './App.svelte';\n</script>\n\n<App />\n`;
}

files.push({ path: path.slice(output_dir.length + 1), data });
}

files.push({
path: 'src/routes/+page.js',
data:
"// Because we don't know whether or not your playground app can run in a server environment, we disable server-side rendering.\n" +
'// Make sure to test whether or not you can re-enable it, as SSR improves perceived performance and site accessibility.\n' +
'// Read more about this option here: https://svelte.dev/docs/kit/page-options#ssr\n' +
'export const ssr = false;\n'
});

// add CSS styles from playground to the project

files.push({
path: 'src/app.css',
data: '@import "tailwindcss";'
});

writeFileSync(output_file, JSON.stringify(files));

// remove output dir afterwards to prevent it messing with Vite watcher
rmSync(output_dir, { force: true, recursive: true });
}
2 changes: 1 addition & 1 deletion apps/svelte.dev/scripts/get_svelte_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ try {
{ encoding: 'utf-8' }
);
const css = html
.slice(html.indexOf('<style>') + 7, html.indexOf('</style>'))
.slice(html.indexOf('<style id="injected">') + 19, html.indexOf('</style>'))
.split('\n')
.map((line) =>
// remove leading \t
Expand Down
1 change: 1 addition & 0 deletions apps/svelte.dev/scripts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ const env = {

fork(`${dir}/get_contributors.js`, { env });
fork(`${dir}/get_donors.js`, { env });
fork(`${dir}/get_svelte_tailwind_template.js`, { env });
fork(`${dir}/get_svelte_template.js`, { env });
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@
}

async function download() {
const { files: components, imports } = repl.toJSON();
const { files: components, imports, tailwind } = repl.toJSON();

const files: Array<{ path: string; data: string }> = await (
await fetch('/svelte-template.json')
tailwind
? await fetch('/svelte-tailwind-template.json')
: await fetch('/svelte-template.json')
).json();

if (imports.length > 0) {
Expand Down