-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compile component slot outlet (#430)
* chore: copied from prev chapter * feat: slot outlet
- Loading branch information
Showing
90 changed files
with
8,275 additions
and
178 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...impls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/.gitignore
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,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
13 changes: 13 additions & 0 deletions
13
...impls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/index.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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>chibivue</title> | ||
</head> | ||
|
||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
...pls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/package.json
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,15 @@ | ||
{ | ||
"name": "playground", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.7.2", | ||
"vite": "^6.0.0" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...mpls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/src/App.vue
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,20 @@ | ||
<script> | ||
import Comp from './Comp.vue' | ||
export default { | ||
components: { | ||
Comp, | ||
}, | ||
setup() { | ||
const count = ref(0) | ||
return { count } | ||
}, | ||
} | ||
</script> | ||
|
||
<template> | ||
<Comp> | ||
<template #default> | ||
<button @click="count++">count is: {{ count }}</button> | ||
</template> | ||
</Comp> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
...pls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/src/Comp.vue
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 @@ | ||
<template> | ||
<p><slot name="default" /></p> | ||
</template> |
7 changes: 7 additions & 0 deletions
7
...mpls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/src/main.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @ts-nocheck | ||
import { createApp } from 'chibivue' | ||
import { createStore } from 'chibivue-store' | ||
import App from './App.vue' | ||
|
||
const app = createApp(App) | ||
app.mount('#app') |
22 changes: 22 additions & 0 deletions
22
...ls/50_basic_template_compiler/080_component_slot_outlet/examples/playground/tsconfig.json
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"module": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"moduleResolution": "Node", | ||
"strict": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"noEmit": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"skipLibCheck": true, | ||
"paths": { | ||
"chibivue": ["../../packages"] | ||
} | ||
}, | ||
"include": ["src"] | ||
} |
16 changes: 16 additions & 0 deletions
16
...s/50_basic_template_compiler/080_component_slot_outlet/examples/playground/vite.config.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { defineConfig } from 'vite' | ||
|
||
import chibivue from '../../packages/@extensions/vite-plugin-chibivue' | ||
|
||
const dirname = path.dirname(fileURLToPath(new URL(import.meta.url))) | ||
|
||
export default defineConfig({ | ||
resolve: { | ||
alias: { | ||
chibivue: path.resolve(dirname, '../../packages'), | ||
}, | ||
}, | ||
plugins: [chibivue()], | ||
}) |
19 changes: 19 additions & 0 deletions
19
book/impls/50_basic_template_compiler/080_component_slot_outlet/package.json
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,19 @@ | ||
{ | ||
"name": "01_project_setup", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "cd examples/playground && pnpm i && pnpm run dev" | ||
}, | ||
"keywords": [], | ||
"author": "ubugeeei <[email protected]>", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/node": "^22.10.1" | ||
}, | ||
"dependencies": { | ||
"@babel/parser": "^7.21.8", | ||
"magic-string": "^0.30.0" | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ate_compiler/080_component_slot_outlet/packages/@extensions/vite-plugin-chibivue/index.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import fs from 'node:fs' | ||
import type { Plugin } from 'vite' | ||
import { createFilter } from 'vite' | ||
import { parse, rewriteDefault } from '../../compiler-sfc' | ||
import { compile } from '../../compiler-dom' | ||
|
||
export default function vitePluginChibivue(): Plugin { | ||
const filter = createFilter(/\.vue$/) | ||
|
||
return { | ||
name: 'vite:chibivue', | ||
resolveId(id) { | ||
if (id.match(/\.vue\.css$/)) return id | ||
}, | ||
|
||
load(id) { | ||
if (id.match(/\.vue\.css$/)) { | ||
const filename = id.replace(/\.css$/, '') | ||
const content = fs.readFileSync(filename, 'utf-8') | ||
const { descriptor } = parse(content, { filename }) | ||
const styles = descriptor.styles.map(it => it.content).join('\n') | ||
return { code: styles } | ||
} | ||
}, | ||
|
||
transform(code, id) { | ||
if (!filter(id)) return | ||
|
||
const outputs = [] | ||
outputs.push("import * as ChibiVue from 'chibivue'") | ||
outputs.push(`import '${id}.css'`) | ||
|
||
const { descriptor } = parse(code, { filename: id }) | ||
|
||
const SFC_MAIN = '_sfc_main' | ||
const scriptCode = rewriteDefault( | ||
descriptor.script?.content ?? '', | ||
SFC_MAIN, | ||
) | ||
outputs.push(scriptCode) | ||
|
||
const templateCode = compile(descriptor.template?.content ?? '', { | ||
isBrowser: false, | ||
}) | ||
outputs.push(templateCode) | ||
|
||
outputs.push('\n') | ||
outputs.push(`export default { ...${SFC_MAIN}, render }`) | ||
|
||
return { code: outputs.join('\n') } | ||
}, | ||
} | ||
} |
Oops, something went wrong.