diff --git a/src/history-api/historyApiFallbackPlugin.ts b/src/history-api/historyApiFallbackPlugin.ts index a8d28a9..61098b0 100644 --- a/src/history-api/historyApiFallbackPlugin.ts +++ b/src/history-api/historyApiFallbackPlugin.ts @@ -27,10 +27,7 @@ export const historyApiFallbackPlugin = (historyApiOptions: HistoryApiOptions): export function buildHistoryApiFallback(server: ViteDevServer, rewrites: Array) { server.middlewares.use(history({ disableDotRule: undefined, - htmlAcceptHeaders: [ - 'text/html', - 'application/xhtml+xml' - ], + htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'], rewrites: rewrites, }) as Connect.NextHandleFunction) } diff --git a/src/html/Base.ts b/src/html/Base.ts index 45f5f6f..f363091 100644 --- a/src/html/Base.ts +++ b/src/html/Base.ts @@ -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' diff --git a/src/html/Build.ts b/src/html/Build.ts index 649054c..416ccfe 100644 --- a/src/html/Build.ts +++ b/src/html/Build.ts @@ -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 = [] _distDir!: string - + constructor(virtualHtmlOptions: HtmlPluginOptions) { super(virtualHtmlOptions) } - + /** * check html file's parent directory * @param html @@ -33,7 +33,7 @@ export class Build extends Base { }) } } - + async _buildConfig(config: UserConfig,) { this._config = config const pagesKey = Object.keys(this._pages) @@ -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) @@ -68,7 +68,7 @@ export class Build extends Base { }, } } - + _closeBundle() { // remove files should not be under project root for (let vHtml of this._needRemove) { @@ -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 } - + } diff --git a/src/html/Serve.ts b/src/html/Serve.ts index 4992a47..02869a9 100644 --- a/src/html/Serve.ts +++ b/src/html/Serve.ts @@ -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 diff --git a/src/html/VirtualHtmlPlugin.ts b/src/html/VirtualHtmlPlugin.ts index ffc14c2..3db1855 100644 --- a/src/html/VirtualHtmlPlugin.ts +++ b/src/html/VirtualHtmlPlugin.ts @@ -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> - + configureServer?: OmitThisParameter<(server: ViteDevServer) => () => void> - + transform?: OmitThisParameter<(code: string, id: string) => Promise> - + 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 diff --git a/src/html/types.ts b/src/html/types.ts index e96d551..1c8faae 100644 --- a/src/html/types.ts +++ b/src/html/types.ts @@ -16,7 +16,9 @@ export type VirtualHtmlTemplateRender = (template: string, data: Record -export type Pages = { [key: string]: VirtualHtmlPage } +export type Pages = { + [key: string]: VirtualHtmlPage +} export type VirtualPageOptions = { entry: string, @@ -24,7 +26,7 @@ export type VirtualPageOptions = { body?: string, } -export type UrlTransformerFunction = (resolvedUrl: string,req: IncomingMessage) => string +export type UrlTransformerFunction = (resolvedUrl: string, req: IncomingMessage) => string /** * plugin config options @@ -64,15 +66,14 @@ export type HtmlPluginOptions = { * key: html name, can be * */ injectCode?: Record - + } /** * inject code to tag's before or after */ export enum POS { - before, - after + before, after } /** diff --git a/src/html/utils.ts b/src/html/utils.ts deleted file mode 100644 index 316a504..0000000 --- a/src/html/utils.ts +++ /dev/null @@ -1,14 +0,0 @@ -import path from "node:path" -import os from "node:os" - -// sourcecode from vite -// export const isWindows = os.platform() === 'win32' -export function slash(p: string): string { - return p.replace(/\\/g, '/') -} - -export function normalizePath(id: string): string { - return path.posix.normalize(os.platform() === 'win32' ? slash(id) : id) -} - -// end diff --git a/src/index.ts b/src/index.ts index 9b6f0bc..bf321d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)