Skip to content

Commit

Permalink
wip: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Oct 22, 2023
1 parent d5d72e4 commit c95f0d6
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions scripts/postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { resolve } from 'node:path'
import { copy, copyFileSync, readFileSync, writeFileSync } from 'fs-extra'

copyFileSync(
resolve(import.meta.dir, '../src/index.html'),
resolve(import.meta.dir, '../src/_lib/index.html'),
resolve(import.meta.dir, '../src/vite/index.html'),
resolve(import.meta.dir, '../src/_lib/vite/index.html'),
)
copy(
resolve(import.meta.dir, '../src/app/styles'),
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ cli
.command('[root]')
.alias('dev')
.action(async () => {
const { createDevServer } = await import('./dev-server.js')
const { createDevServer } = await import('./vite/dev-server.js')
const server = await createDevServer()
await server.listen()
server.printUrls()
})

// build
cli.command('build').action(async () => {
const { build } = await import('./build.js')
const { build } = await import('./vite/build.js')
await build()
})

// preview
cli.command('preview').action(async () => {
const { preview } = await import('./preview.js')
const { preview } = await import('./vite/preview.js')
const server = await preview()
server.printUrls()
})
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { build } from './build.js'
export { createDevServer } from './dev-server.js'
export { preview } from './preview.js'
export { build } from './vite/build.js'
export { createDevServer } from './vite/dev-server.js'
export { preview } from './vite/preview.js'
6 changes: 3 additions & 3 deletions src/build.ts → src/vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import * as vite from 'vite'

import { prerender } from './vite-plugins/prerender.js'
import { prerender } from './plugins/prerender.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

Expand All @@ -25,7 +25,7 @@ export async function build({ outDir = 'dist' }: BuildParameters = {}) {
build: {
emptyOutDir: false,
outDir: resolve(outDir),
ssr: resolve(__dirname, 'app/index.server.tsx'),
ssr: resolve(__dirname, '../app/index.server.tsx'),
},
plugins: [prerender({ outDir })],
root: __dirname,
Expand All @@ -37,7 +37,7 @@ export async function build({ outDir = 'dist' }: BuildParameters = {}) {
lib: {
formats: ['iife'],
name: 'theme',
entry: [resolve(__dirname, './app/utils/initialize-theme.ts')],
entry: [resolve(__dirname, '../app/utils/initialize-theme.ts')],
},
minify: true,
outDir: resolve(outDir),
Expand Down
2 changes: 1 addition & 1 deletion src/dev-server.ts → src/vite/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createServer } from 'vite'

import { dev } from './vite-plugins/dev.js'
import { dev } from './plugins/dev.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

Expand Down
6 changes: 3 additions & 3 deletions src/index.html → src/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/app/utils/initialize-theme.ts"></script>
<link href="/app/styles/index.css" rel="stylesheet">
<script src="../app/utils/initialize-theme.ts"></script>
<link href="../app/styles/index.css" rel="stylesheet">
<!--head-->
</head>
<body>
<div id="app"><!--body--></div>
<script type="module" src="/app/index.client.tsx"></script>
<script type="module" src="../app/index.client.tsx"></script>
</body>
</html>
9 changes: 7 additions & 2 deletions src/vite-plugins/dev.ts → src/vite/plugins/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ export function dev(): PluginOption {

const indexHtml = readFileSync(resolve(__dirname, '../index.html'), 'utf-8')
const template = await server.transformIndexHtml(url!, indexHtml)
const module = await server.ssrLoadModule(resolve(__dirname, '../app/index.server.tsx'))
const module = await server.ssrLoadModule(
resolve(__dirname, '../../app/index.server.tsx'),
)
const { head, body } = await module.render(req)
const html = template.replace('<!--body-->', body).replace('<!--head-->', head)
const html = template
.replace(/\.\.\/app/g, `/@fs${resolve(__dirname, '../../app')}`)
.replace('<!--body-->', body)
.replace('<!--head-->', head)
res.end(html)
} finally {
next()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function prerender({ outDir = 'dist' }: PrerenderPluginParameters): Plugi
const html = template
.replace('<!--body-->', body)
.replace('<!--head-->', head)
.replace('/app/utils/initialize-theme.ts', '/initialize-theme.iife.js')
.replace('../app/utils/initialize-theme.ts', '/initialize-theme.iife.js')
const filePath = `${route.endsWith('/') ? `${route}index` : route}.html`.replace(/^\//, '')
const path = resolve(outDir_resolved, filePath)
const pathDir = dirname(path)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 9 additions & 4 deletions src/vite.config.ts → src/vite/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ import {
} from 'shiki-processor'
import { defineConfig } from 'vite'

import { remarkCallout } from './remark-plugins/callout.js'
import { remarkCodeGroup } from './remark-plugins/code-group.js'
import { remarkSubheading } from './remark-plugins/subheading.js'
import { routes } from './vite-plugins/routes.js'
import { routes } from './plugins/routes.js'
import { remarkCallout } from './remark/callout.js'
import { remarkCodeGroup } from './remark/code-group.js'
import { remarkSubheading } from './remark/subheading.js'

export default defineConfig({
css: {
postcss: {
plugins: [(autoprefixer as any).default()],
},
},
server: {
fs: {
allow: ['..'],
},
},
plugins: [
react(),
mdx({
Expand Down

0 comments on commit c95f0d6

Please sign in to comment.