This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: generate typescript types for svelte component props definition file
- Loading branch information
Showing
15 changed files
with
162 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@leanweb/fullstack": minor | ||
--- | ||
|
||
Add views and props intellisense plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import path from "node:path"; | ||
import fs from "node:fs"; | ||
import type { Plugin } from "vite"; | ||
import { globSync as glob } from "glob"; | ||
import { dedent } from "ts-dedent"; | ||
|
||
const cwd = process.cwd(); | ||
|
||
const outDir = cwd; | ||
const outFile = path.join(outDir, "views.d.ts"); | ||
|
||
const prefix = path.join(cwd, "src/views/"); | ||
|
||
let cache = new Set<string>(); | ||
|
||
const write = (views: string[]) => { | ||
fs.writeFileSync( | ||
outFile, | ||
dedent` | ||
import {ComponentProps} from 'svelte'; | ||
${views | ||
.map((file, i) => `import $${i} from "${relative_path(outDir, file)}";`) | ||
.join("\n")} | ||
declare module "@leanweb/fullstack/runtime/Render" { | ||
interface Views { | ||
${views | ||
.map((file, i) => { | ||
const file_ = file.replace(prefix, ""); | ||
const { dir, name } = path.parse(file_); | ||
const key = path.join(dir, name); | ||
const value = `ComponentProps<$${i}>`; | ||
return [`"${key}": ${value}`, `"${file_}": ${value}`]; | ||
}) | ||
.flat() | ||
.join(",\n")} | ||
} | ||
} | ||
` | ||
); | ||
}; | ||
|
||
export const views: Plugin = { | ||
name: "fullstack:views", | ||
config() { | ||
return { | ||
server: { | ||
watch: { | ||
ignored: [outFile], | ||
}, | ||
}, | ||
}; | ||
}, | ||
buildStart() { | ||
const files = glob("**/*.svelte", { | ||
cwd, | ||
absolute: true, | ||
root: "/src/views", | ||
}); | ||
|
||
write(files); | ||
|
||
cache = new Set(files); | ||
}, | ||
configureServer(server) { | ||
return () => { | ||
server.watcher.on("all", (e, file) => { | ||
if (!file.endsWith(".svelte")) return; | ||
|
||
if (e == "unlink") { | ||
cache.delete(file); | ||
} | ||
|
||
if (e == "add") { | ||
cache.add(file); | ||
} | ||
|
||
write([...cache]); | ||
}); | ||
}; | ||
}, | ||
}; | ||
|
||
function posixify(str: string) { | ||
return str.replace(/\\/g, "/"); | ||
} | ||
|
||
/** | ||
* Like `path.join`, but posixified and with a leading `./` if necessary | ||
*/ | ||
function join_relative(...str: string[]) { | ||
let result = posixify(path.join(...str)); | ||
if (!result.startsWith(".")) { | ||
result = `./${result}`; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Like `path.relative`, but always posixified and with a leading `./` if necessary. | ||
* Useful for JS imports so the path can safely reside inside of `node_modules`. | ||
* Otherwise paths could be falsely interpreted as package paths. | ||
*/ | ||
function relative_path(from: string, to: string) { | ||
return join_relative(path.relative(from, to)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
node_modules | ||
.env | ||
public/assets | ||
env.d.ts | ||
env.d.ts | ||
views.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/// <reference types="svelte" /> | ||
/// <reference path="env.d.ts" /> | ||
/// <reference path="views.d.ts" /> | ||
/// <reference types="vite/client" /> | ||
/// <reference types="@leanweb/fullstack/ssr" /> | ||
/// <reference types="vite-plugin-simple-scope/types" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<script lang="ts"> | ||
export let count: number; | ||
export let name: string | ||
</script> | ||
|
||
<!DOCTYPE html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ | |
<title>Document</title> | ||
</head> | ||
<body> | ||
<Footer __island /> | ||
<Footer /> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script lang='ts'> | ||
export let count: number | ||
</script> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters