-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
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
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,5 @@ | ||
import * as path from "path"; | ||
|
||
export const ROOT = path.resolve(__dirname, "../../../"); | ||
|
||
export const DEFAULT_HTML_PATH = path.resolve(ROOT, "template.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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { createServer as createViteDevServer } from "vite"; | ||
import { pluginIndexHtml } from "./plugin-r-press/indexHtml"; | ||
|
||
|
||
export function createDevServer(root: string = process.cwd()) { | ||
return createViteDevServer({ | ||
root, | ||
plugins: [pluginIndexHtml()] | ||
}) | ||
} |
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,28 @@ | ||
import { readFile } from "fs/promises"; | ||
import { Plugin } from "vite"; | ||
import { DEFAULT_HTML_PATH } from "../constant"; | ||
|
||
|
||
export function pluginIndexHtml(): Plugin { | ||
return { | ||
name: 'r-press:index-html', | ||
// 不在函数题体直接插入中间件,而是在configureServer中插入? | ||
// 防止影响vite的逻辑 | ||
configureServer(server) { | ||
server.middlewares.use(async (req, res, next) => { | ||
if (req.url !== '/') { | ||
return next(); | ||
} | ||
const htmlContent = await readFile(DEFAULT_HTML_PATH, 'utf-8'); | ||
|
||
try { | ||
res.statusCode = 200; | ||
res.setHeader("Content-Type", "text/html"); | ||
res.end(htmlContent); | ||
} catch (e) { | ||
return next(e); | ||
} | ||
}) | ||
} | ||
} | ||
} |
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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root">running~</div> | ||
</body> | ||
|
||
</html> |