Skip to content

Commit 428328e

Browse files
committed
Add custom fonts and icons opinions
1 parent b52eec7 commit 428328e

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ This generates the starter based on the last known SvelteKit decisions, then sav
3535

3636
## Opinions
3737

38-
### Base
38+
### SvelteKit
3939

4040
Base `create-svelte` skeleton template with `jsdoc`, `prettier`, `eslint` and `playwright`.
4141

4242
### Tailwind CSS
4343

44-
Add `tailwindcss` using `svelte-add`.
44+
Adds `tailwindcss` using `svelte-add`, then adds `tailwindcss/typography`.
4545

4646
### Prettier config
4747

@@ -81,6 +81,14 @@ module.exports = {
8181
Seriously, don't use SSR unless you really need to. Installs `adapter-static` and adds sensible
8282
defaults.
8383

84+
### Custom fonts
85+
86+
Use `fontsource` for self-hosted open-source fonts.
87+
88+
### Custom icons
89+
90+
Use `iconify` to create your own tree-shaken open-source icon set. Add icons in `/src/lib/icons.js`.
91+
8492
## License
8593

8694
ISC

index.js

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { create } from 'create-svelte' // @latest
44

55
$.verbose = false
66

7-
export async function patchFiles(filepaths, ...replacers) {
8-
for (const file of [filepaths].flat()) {
7+
async function patchFiles(files, ...replacers) {
8+
for (const file of [files].flat()) {
99
let contents = await fs.readFile(file, 'utf8')
1010
for (const [pattern, replacement] of replacers) {
1111
contents = contents.replace(pattern, replacement)
@@ -14,9 +14,15 @@ export async function patchFiles(filepaths, ...replacers) {
1414
}
1515
}
1616

17-
export async function getVersion(pkg) {
18-
const version = await $`npm show ${pkg} version`
19-
return version.toString()
17+
async function patchPackage(name, ...dependencies) {
18+
const version = async (dep) => (await $`npm show ${dep} version`).stdout.trim()
19+
const file = path.join(name, 'package.json')
20+
let pkg = await fs.readJson(file)
21+
for (const dep of dependencies) {
22+
pkg.devDependencies[dep.slice(1)] =
23+
dep.charAt(0) === '+' ? `^${await version(dep.slice(1))}` : undefined
24+
}
25+
await fs.writeJson(file, pkg, { spaces: 2 })
2026
}
2127

2228
export async function addBaseTemplate({ name, template }) {
@@ -37,11 +43,17 @@ export async function addBaseTemplate({ name, template }) {
3743

3844
export async function addTailwindcss({ name }) {
3945
await $`cd ${name} && npx -y svelte-add@latest tailwindcss`
46+
await patchPackage(name, '+@tailwindcss/typography')
47+
await patchFiles(path.join(name, 'tailwind.config.cjs'), [
48+
'plugins: []',
49+
`plugins: [require('@tailwindcss/typography')]`
50+
])
4051
}
4152

4253
export async function addPrettier({ name }) {
43-
await fs.writeJson(path.join(name, '.prettierrc'), {
44-
...(await fs.readJson(path.join(name, '.prettierrc'))),
54+
const file = path.join(name, '.prettierrc')
55+
await fs.writeJson(file, {
56+
...(await fs.readJson(file)),
4557
printWidth: 100,
4658
useTabs: false,
4759
semi: false,
@@ -61,16 +73,40 @@ export async function addEslint({ name }) {
6173
}
6274

6375
export async function addAdapterStatic({ name }) {
64-
await patchFiles(
65-
[path.join(name, 'package.json'), path.join(name, 'svelte.config.js')],
66-
[`adapter-auto`, `adapter-static`]
67-
)
76+
await patchFiles(path.join(name, 'svelte.config.js'), [`adapter-auto`, `adapter-static`])
77+
await patchPackage(name, '-@sveltejs/adapter-auto', '+@sveltejs/adapter-static')
6878
await fs.outputFile(
6979
path.join(name, 'src', 'routes', '+layout.js'),
7080
`export const prerender = true\n`
7181
)
7282
}
7383

84+
export async function addFontsource({ name }) {
85+
await patchPackage(name, '+@fontsource/inter')
86+
await patchFiles(path.join(name, 'src', 'routes', '+layout.svelte'), [
87+
`<script>`,
88+
`<script>import '@fontsource/inter/variable.css';`
89+
])
90+
await patchFiles(
91+
path.join(name, 'tailwind.config.cjs'),
92+
[`const config`, `const dt = require('tailwindcss/defaultTheme');\n\nconst config`],
93+
[`extend: {}`, `extend: { fontFamily: { sans: ['InterVariable', ...dt.fontFamily.sans] } }`]
94+
)
95+
}
96+
97+
export async function addIconify({ name }) {
98+
await patchPackage(name, '+@iconify/svelte', '+@iconify-icons/mdi')
99+
await fs.outputFile(
100+
path.join(name, 'src', 'lib', 'icons.js'),
101+
`import Icon, { addIcon } from '@iconify/svelte/dist/OfflineIcon.svelte';\nimport check from '@iconify-icons/mdi/check';\n\naddIcon('check', check);\n\nexport { Icon as default }\n`
102+
)
103+
await patchFiles(
104+
path.join(name, 'src', 'routes', '+page.svelte'),
105+
[`<h1>`, `<script>import Icon from '$lib/icons'</script>\n\n<h1>`],
106+
[`</p>`, `</p>\n\n<Icon class="w-12 h-12" icon='check' />\n`]
107+
)
108+
}
109+
74110
void (async function () {
75111
const opts = {
76112
name: argv._[0],
@@ -85,16 +121,13 @@ void (async function () {
85121
process.exit(1)
86122
}
87123

88-
await addBaseTemplate(opts)
89-
echo`- created ${opts.template} template`
90-
await addTailwindcss(opts)
91-
echo`- added tailwindcss`
92-
await addPrettier(opts)
93-
echo`- patched prettier config`
94-
await addEslint(opts)
95-
echo`- patched eslint config`
96-
await addAdapterStatic(opts)
97-
echo`- added adapter-static`
124+
await addBaseTemplate(opts).then(() => echo`- created ${opts.template} template`)
125+
await addTailwindcss(opts).then(() => echo`- added tailwindcss`)
126+
await addPrettier(opts).then(() => echo`- patched prettier config`)
127+
await addEslint(opts).then(() => echo`- patched eslint config`)
128+
await addAdapterStatic(opts).then(() => echo`- added adapter-static`)
129+
await addFontsource(opts).then(() => echo`- added fontsource`)
130+
await addIconify(opts).then(() => echo`- added iconify`)
98131

99132
echo`\nAll done! Complete the setup with:\n`
100133
echo`$ cd ${opts.name}`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zerodevx/sveltekit-starter",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Opinionated starter template for SvelteKit",
55
"author": "Jason Lee <[email protected]>",
66
"scripts": {

0 commit comments

Comments
 (0)