Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
windsonR committed Feb 9, 2024
1 parent 64a3f8f commit 3a9b38e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 60 deletions.
5 changes: 1 addition & 4 deletions src/history-api/historyApiFallbackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export const historyApiFallbackPlugin = (historyApiOptions: HistoryApiOptions):
export function buildHistoryApiFallback(server: ViteDevServer, rewrites: Array<HistoryRewrites>) {
server.middlewares.use(history({
disableDotRule: undefined,
htmlAcceptHeaders: [
'text/html',
'application/xhtml+xml'
],
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
rewrites: rewrites,
}) as Connect.NextHandleFunction)
}
3 changes: 1 addition & 2 deletions src/html/Base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { HtmlPluginOptions, InjectCode, PageObject, VirtualHtmlTemplateData } from './types'
import { Pages, POS, VirtualHtmlPage, VirtualHtmlTemplateRender, VirtualPageOptions } from './types'
import { type UserConfig, createFilter, } from 'vite'
import { createFilter, normalizePath, type UserConfig, } from 'vite'
import * as path from 'path'
import * as fs from 'fs'
import { normalizePath, } from './utils'
import glob from 'fast-glob'
import debug from 'debug'
import { createRequire } from 'node:module'
Expand Down
42 changes: 23 additions & 19 deletions src/html/Build.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type {HtmlPluginOptions} from "./types"
import {VirtualHtmlPage, VirtualPageOptions} from "./types"
import type {UserConfig} from 'vite'
import {Base} from "./Base"
import fs, {promises as fsp} from "fs"
import path from "path"
import {normalizePath} from "./utils"
import type { HtmlPluginOptions } from './types'
import { VirtualHtmlPage, VirtualPageOptions } from './types'
import type { UserConfig } from 'vite'
import { normalizePath } from 'vite'
import { Base } from './Base'
import fs, { promises as fsp } from 'fs'
import path from 'path'

export class Build extends Base {

_needRemove: Array<string> = []
_distDir!: string

constructor(virtualHtmlOptions: HtmlPluginOptions) {
super(virtualHtmlOptions)
}

/**
* check html file's parent directory
* @param html
Expand All @@ -33,7 +33,7 @@ export class Build extends Base {
})
}
}

async _buildConfig(config: UserConfig,) {
this._config = config
const pagesKey = Object.keys(this._pages)
Expand All @@ -43,7 +43,7 @@ export class Build extends Base {
const vHtml = normalizePath(path.resolve(this.cwd, `./${config.root ? this.addTrailingSlash(config.root) : ''}${this.htmlNameAddIndex(key)}.html`))
if (!fs.existsSync(vHtml)) {
this._needRemove.push(vHtml)
await this.checkVirtualPath(vHtml, this._needRemove, config.root??'')
await this.checkVirtualPath(vHtml, this._needRemove, config.root ?? '')
if (typeof pageOption === 'string' || 'template' in pageOption) {
const genPageOption = await this.generatePageOptions(pageOption, this._globalData, this._globalRender)
await fsp.copyFile(path.resolve(this.cwd, `.${genPageOption.template}`), vHtml)
Expand All @@ -68,7 +68,7 @@ export class Build extends Base {
},
}
}

_closeBundle() {
// remove files should not be under project root
for (let vHtml of this._needRemove) {
Expand All @@ -81,21 +81,25 @@ export class Build extends Base {
}
}
}

/**
* use pages' key as html name
* @param pages
*/
extractHtmlPath(pages: { [p: string]: VirtualHtmlPage | VirtualPageOptions }) {
const newPages: { [key: string]: string } = {}
extractHtmlPath(pages: {
[p: string]: VirtualHtmlPage | VirtualPageOptions
}) {
const newPages: {
[key: string]: string
} = {}
Object.keys(pages).forEach(key => {
newPages[key] = `/${this.htmlNameAddIndex(key)}.html`
})
return newPages
}

htmlNameAddIndex(htmlName: string): string{
htmlNameAddIndex(htmlName: string): string {
return htmlName.endsWith('/') ? htmlName + 'index' : htmlName
}

}
2 changes: 1 addition & 1 deletion src/html/Serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { HtmlPluginOptions, UrlTransformerFunction } from './types'
import { Base } from './Base'
import { buildHistoryApiFallback } from '../history-api/historyApiFallbackPlugin'
import type { ViteDevServer } from 'vite'
import { normalizePath } from 'vite'
import type { HistoryApiOptions, HistoryRewrites } from '../history-api/types'
import { normalizePath } from './utils'

export class Serve extends Base {
_rewrites?: Array<HistoryRewrites>
Expand Down
22 changes: 11 additions & 11 deletions src/html/VirtualHtmlPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import {HtmlPluginOptions} from "./types"
import { ConfigEnv, Plugin, ResolvedConfig, UserConfig, ViteDevServer } from 'vite'
import {HistoryApiOptions} from "../history-api/types"
import { HtmlPluginOptions } from './types'
import { ConfigEnv, Plugin, UserConfig, ViteDevServer } from 'vite'
import { HistoryApiOptions } from '../history-api/types'
import { Serve } from './Serve'
import { Build } from './Build'

export class VirtualHtmlPlugin implements Plugin {
name = 'vite-plugin-virtual-html'

_htmlOptions: HtmlPluginOptions

_config!: UserConfig

load?: OmitThisParameter<(id: string) => Promise<string | null>>

configureServer?: OmitThisParameter<(server: ViteDevServer) => () => void>

transform?: OmitThisParameter<(code: string, id: string) => Promise<string | null>>

closeBundle?: OmitThisParameter<() => void>

constructor(virtualHtmlOptions: HtmlPluginOptions & HistoryApiOptions) {
this._htmlOptions = virtualHtmlOptions
this.config = this.config.bind(this)
}

async config(config: UserConfig, {command}: ConfigEnv) {
config.appType = 'custom'
this._config = config
Expand Down
11 changes: 6 additions & 5 deletions src/html/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export type VirtualHtmlTemplateRender = (template: string, data: Record<string,

export type VirtualHtmlTemplateData = Record<string, any>

export type Pages = { [key: string]: VirtualHtmlPage }
export type Pages = {
[key: string]: VirtualHtmlPage
}

export type VirtualPageOptions = {
entry: string,
title?: string,
body?: string,
}

export type UrlTransformerFunction = (resolvedUrl: string,req: IncomingMessage) => string
export type UrlTransformerFunction = (resolvedUrl: string, req: IncomingMessage) => string

/**
* plugin config options
Expand Down Expand Up @@ -64,15 +66,14 @@ export type HtmlPluginOptions = {
* key: html name, can be *
*/
injectCode?: Record<string, InjectCode>

}

/**
* inject code to tag's before or after
*/
export enum POS {
before,
after
before, after
}

/**
Expand Down
14 changes: 0 additions & 14 deletions src/html/utils.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Plugin,} from 'vite'
import type {HtmlPluginOptions,} from './html/types'
import { VirtualHtmlPlugin } from "./html/VirtualHtmlPlugin"
import type {HistoryApiOptions} from "./history-api/types"
import type { Plugin, } from 'vite'
import type { HtmlPluginOptions, } from './html/types'
import { VirtualHtmlPlugin } from './html/VirtualHtmlPlugin'
import type { HistoryApiOptions } from './history-api/types'

export default (virtualHtmlOptions: HtmlPluginOptions & HistoryApiOptions): Plugin => {
return new VirtualHtmlPlugin(virtualHtmlOptions)
Expand Down

0 comments on commit 3a9b38e

Please sign in to comment.