vite-pug-adapter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +25 -0
  2. package/package.json +28 -0
  3. package/src/index.js +37 -0
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # vite-pug-adapter
2
+ A simple Vite plugin that serves Pug files directly without physical HTML files.
3
+
4
+ ## Features
5
+ - No dirties. No physical HTML in your root.
6
+ - Simple usage. No configs.
7
+ - Serve Pug files to Vite as if they were html files.
8
+
9
+ ## Installation
10
+ ```bash
11
+ npm install -D vite-pug-adapter pug
12
+ ```
13
+ ```vite.config.js
14
+ //vite.config.js
15
+ // vite.config.js
16
+ import vite_pug_adapter from 'vite-pug-adapter';
17
+
18
+ export default {
19
+ plugins: [vite_pug_adapter]
20
+ };
21
+ ```
22
+
23
+ ## Caution
24
+ This **only** compiles Pug files in your root dir, then if you need to put Pug files in other places such as /src, you should use anothor module.
25
+
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "vite-pug-adapter",
3
+ "version": "1.0.0",
4
+ "description": "vite put adapter",
5
+ "keywords": [
6
+ "vite",
7
+ "pug"
8
+ ],
9
+ "homepage": "https://github.com/mtugb/vite-pug-adapter#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/mtugb/vite-pug-adapter/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/mtugb/vite-pug-adapter.git"
16
+ },
17
+ "license": "ISC",
18
+ "author": "mtugb",
19
+ "type": "module",
20
+ "main": "index.js",
21
+ "scripts": {
22
+ "test": "echo \"Error: no test specified\" && exit 1"
23
+ },
24
+ "peerDependencies": {
25
+ "vite": "^6.0.0",
26
+ "pug": "^3.0.0"
27
+ }
28
+ }
package/src/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import process from "node:process";
2
+ const vite_pug_adapter = {
3
+ name: "vite-pug-adapter",
4
+ configureServer: function (server) {
5
+ server.middlewares.use(async (req, res, next) => {
6
+ // normal page
7
+ if (/^\/[a-zA-Z]*$/.test(req.url)) {
8
+ const pug_path = req.url === "/" ? "/index" : req.url;
9
+ try {
10
+ let html = pug.renderFile(`${process.cwd()}${pug_path}.pug`);
11
+ res.setHeader("Content-Type", "text/html");
12
+ // enable hot reloading
13
+ html = await server.transformIndexHtml(req.url, html);
14
+ res.end(html);
15
+ } catch (e) {
16
+ // throw error to vite ( this func mutate error into explict way)
17
+ server.ssrFixStacktrace(e);
18
+ next(e);
19
+ }
20
+ } else {
21
+ next();
22
+ }
23
+ });
24
+
25
+ server.watcher.add(`${process.cwd()}/**/*.pug`);
26
+
27
+ server.watcher.on("change", (file) => {
28
+ if (file.endsWith(".pug")) {
29
+ server.ws.send({
30
+ type: "full-reload",
31
+ path: "*",
32
+ });
33
+ }
34
+ });
35
+ },
36
+ };
37
+ export default vite_pug_adapter;