Skip to content

Commit

Permalink
feat: theme working
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 16, 2021
1 parent a70a214 commit d84450b
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 110 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions packages/demo/package.json → demo/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"private": true,
"version": "0.0.0-beta.1",
"scripts": {
"dev": "vite-slides",
"build": "vite-slides build"
},
"dependencies": {
"vite-slides": "workspace:*"
},
"version": "0.0.0-beta.1"
}
}
File renamed without changes
File renamed without changes.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "vite-slides",
"version": "0.0.0-beta.1",
"files": [],
"private": true,
"scripts": {
"dev": "npm -C packages/demo run dev",
"demo:build": "npm -C packages/demo run build",
"build": "pnpm -r run build",
"release": "npx bumpp package.json packages/*/package.json --commit --tag --push && pnpm -r publish"
"release": "npx bumpp packages/*/package.json --commit --tag --push && pnpm -r publish"
},
"devDependencies": {
"@antfu/eslint-config": "^0.6.2",
Expand Down
20 changes: 0 additions & 20 deletions packages/demo/.index.html

This file was deleted.

8 changes: 1 addition & 7 deletions packages/theme-default/layouts/center.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<template>
<div class="master center">
<div class="layout-master center">
<div class="my-auto">
<slot />
</div>
</div>
</template>

<style lang="postcss">
.master.center {
@apply h-full grid place-content-center;
}
</style>
16 changes: 1 addition & 15 deletions packages/theme-default/layouts/cover.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
<template>
<div class="master cover">
<div class="layout-master cover">
<div class="my-auto">
<slot />
</div>
</div>
</template>

<style lang="postcss">
.master.cover {
@apply h-full flex;
h1 {
@apply text-6xl leading-20;
}
h1 ~ p {
@apply opacity-40 mb-10 text-2xl;
}
}
</style>
5 changes: 1 addition & 4 deletions packages/theme-default/styles/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'virtual:windi-base.css'
import 'virtual:windi-components.css'
import './main.css'
import './layout.css'
import './code.css'
import 'virtual:windi-utilities.css'
import 'virtual:windi-devtools'
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<template>
<main class="master default">
<slot />
</main>
</template>

<style lang="postcss">
.master {
.layout-master {
@apply px-14 py-10 text-[1.1rem];

h1, h2, h3, h4, p, div {
Expand Down Expand Up @@ -84,4 +77,19 @@
@apply font-600;
}
}
</style>

.layout-master.cover {
@apply h-full flex;

h1 {
@apply text-6xl leading-20;
}

h1 ~ p {
@apply opacity-40 mb-10 text-2xl;
}
}

.layout-master.center {
@apply h-full grid place-content-center;
}
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/vite-slides/client/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<main class="layout-master default">
<slot />
</main>
</template>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<main class="master end">
<main class="layout-master end">
END
</main>
</template>

<style lang="postcss">
.master.end {
.layout-master.end {
@apply text-white text-opacity-85 text-xl tracking-widest bg-black h-full text-center grid place-content-center select-none;
}
</style>
4 changes: 4 additions & 0 deletions packages/vite-slides/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ViteSSG } from 'vite-ssg'
import { getRoutes } from './routes'
import App from './App.vue'
import 'virtual:windi-base.css'
import 'virtual:windi-components.css'
/* __imports__ */
import 'virtual:windi-utilities.css'
import 'virtual:windi-devtools'

const routes = getRoutes(/* __layouts__ */)

Expand Down
68 changes: 68 additions & 0 deletions packages/vite-slides/node/plugins/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { existsSync } from 'fs'
import { join, resolve, basename } from 'path'
import { Plugin } from 'vite'
import fg from 'fast-glob'
import { getPackageRoot, getThemeRoot } from '../env'

export function createEntryPlugin(): Plugin {
const packageRoot = getPackageRoot()
const themeRoot = getThemeRoot()
const mainEntry = resolve(packageRoot, 'client/main.ts')
const userRoot = process.cwd()

return {
name: 'vite-slides:entry',
enforce: 'pre',
async transform(code, id) {
if (id === mainEntry) {
const imports: string[] = []
const layouts: Record<string, string> = {}

async function scanStyle(root: string) {
const styles = [
join(root, 'styles/index.ts'),
join(root, 'styles/index.js'),
join(root, 'styles/index.css'),
join(root, 'styles.css'),
join(root, 'style.css'),
]

for (const style of styles) {
if (existsSync(style)) {
imports.push(`import "/@fs${style}"`)
return
}
}
}

async function scanLayouts(root: string) {
const layoutPaths = await fg('layouts/*.{vue,ts}', {
cwd: root,
absolute: true,
})

for (const layoutPath of layoutPaths) {
const layout = basename(layoutPath).replace(/\.\w+$/, '')
if (layouts[layout])
continue
imports.push(`import __layout_${layout} from "/@fs${layoutPath}"`)
layouts[layout] = `__layout_${layout}`
}
}

await scanStyle(themeRoot)
await scanStyle(userRoot)

await scanLayouts(join(packageRoot, 'client'))
await scanLayouts(themeRoot)
await scanLayouts(userRoot)

code = code.replace('/* __imports__ */', imports.join('\n'))
code = code.replace('/* __layouts__ */', `{${Object.entries(layouts).map(([k, v]) => `"${k}": ${v}`).join(',\n')}}`)
return code
}

return null
},
}
}
42 changes: 4 additions & 38 deletions packages/vite-slides/node/plugins/preset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

import { existsSync } from 'fs'
import { join, resolve, basename } from 'path'
import { mergeConfig, Plugin } from 'vite'
import Vue from '@vitejs/plugin-vue'
import ViteIcons, { ViteIconsResolver } from 'vite-plugin-icons'
import ViteComponents from 'vite-plugin-components'
import Markdown from 'vite-plugin-md'
import fg from 'fast-glob'
import WindiCSS from 'vite-plugin-windicss'
import Prism from 'markdown-it-prism'
import base64 from 'js-base64'
Expand All @@ -15,6 +12,7 @@ import { getDefultWindiConfig } from '../windicss'
import { getIndexHtml } from '../common'
import { createSlidesLoader } from './slides'
import { createMonacoLoader } from './monaco'
import { createEntryPlugin } from './entry'

export type ArgumentsType<T> = T extends ((...args: infer A) => void) ? A : never

Expand All @@ -36,11 +34,10 @@ export function ViteSlides(options: ViteSlidesOptions = {}): Plugin[] {
} = options

const packageRoot = getPackageRoot()
const mainEntry = resolve(packageRoot, 'client/main.ts')

return [
{
name: 'vite-slides:entry',
name: 'vite-slides:config',
config(config) {
return mergeConfig(config, {
optimizeDeps: {
Expand Down Expand Up @@ -91,39 +88,6 @@ export function ViteSlides(options: ViteSlidesOptions = {}): Plugin[] {

},

{
name: 'vite-slides:transform',
enforce: 'pre',
async transform(code, id) {
if (id === mainEntry) {
const themeRoot = getThemeRoot()
const styleIndex = join(themeRoot, 'styles/index.ts')
const imports: string[] = []
const layouts: Record<string, string> = {}

if (existsSync(styleIndex))
imports.push(`import "/@fs${styleIndex}"`)

const layoutPaths = await fg('layouts/*.{vue,ts}', {
cwd: themeRoot,
absolute: true,
})

for (const layoutPath of layoutPaths) {
const layout = basename(layoutPath).replace(/\.\w+$/, '')
imports.push(`import __layout_${layout} from "/@fs${layoutPath}"`)
layouts[layout] = `__layout_${layout}`
}

code = code.replace('/* __imports__ */', imports.join('\n'))
code = code.replace('/* __layouts__ */', `{${Object.entries(layouts).map(([k, v]) => `"${k}": ${v}`).join(',\n')}}`)
return code
}

return null
},
},

Vue({
include: [/\.vue$/, /\.md$/],
...vueOptions,
Expand Down Expand Up @@ -173,6 +137,7 @@ export function ViteSlides(options: ViteSlidesOptions = {}): Plugin[] {
`${packageRoot}/client/components`,
`${getThemeRoot()}/components`,
'src/components',
'components',
],

customLoaderMatcher: id => id.endsWith('.md'),
Expand All @@ -195,6 +160,7 @@ export function ViteSlides(options: ViteSlidesOptions = {}): Plugin[] {
...windicssOptions,
}),

createEntryPlugin(),
createSlidesLoader(),
createMonacoLoader(),
]
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-slides/node/windicss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getDefultWindiConfig() {
include: [
resolve(getPackageRoot(), 'client/**/*.{vue,ts}'),
resolve(getThemeRoot(), '**/*.{vue,ts}'),
'src/**/*.{vue,ts}',
'**/*.{vue,ts}',
'slides.md',
],
},
Expand Down
13 changes: 7 additions & 6 deletions packages/vite-slides/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
"name": "vite-slides",
"description": "Opinionated markdown slides maker, powered by Vite and Windi CSS",
"version": "0.0.0-beta.1",
"files": [
"bin",
"client",
"dist"
],
"scripts": {
"build": "tsup node/index.ts node/cli.ts --dts --format cjs,esm",
"build": "rimraf dist && tsup node/index.ts node/cli.ts --dts --format cjs,esm",
"dev": "nr build --watch",
"start": "esno node/cli.ts",
"prepublishOnly": "npm run build"
Expand All @@ -25,6 +20,11 @@
"bin": {
"vite-slides": "./bin/vite-slides.js"
},
"files": [
"bin",
"client",
"dist"
],
"dependencies": {
"@iconify/json": "^1.1.330",
"@vite-slides/theme-default": "workspace:*",
Expand Down Expand Up @@ -63,6 +63,7 @@
"eslint": "^7.24.0",
"esno": "^0.5.0",
"pnpm": "^6.1.0",
"rimraf": "^3.0.2",
"tsup": "^4.8.21",
"typescript": "^4.2.4",
"vite-ssg": "^0.9.2"
Expand Down
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- packages/*
- demo

0 comments on commit d84450b

Please sign in to comment.