vite-plugin-serve-static 1.2.0 → 2.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-2024 Drew Davis
3
+ Copyright (c) 2023-2025 Drew Davis
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -2,20 +2,21 @@
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/vite-plugin-serve-static/latest)](https://www.npmjs.com/package/vite-plugin-serve-static)
4
4
  [![vite](https://img.shields.io/npm/dependency-version/vite-plugin-serve-static/peer/vite)](https://github.com/vitejs/vite)
5
- [![license: MIT](https://img.shields.io/npm/l/vite-plugin-serve-static)](https://github.com/reifiedbeans/vite-plugin-serve-static/blob/main/LICENSE)
5
+ [![license: MIT](https://img.shields.io/npm/l/vite-plugin-serve-static)](https://github.com/typeparameter/vite-plugin-serve-static/blob/main/LICENSE)
6
6
 
7
7
  A simple Vite plugin for serving arbitrary static files that aren't in your `public` directory.
8
8
 
9
9
  ```typescript
10
10
  // vite.config.ts
11
11
  import path from "path";
12
+
12
13
  import { defineConfig } from "vite";
13
14
  import serveStatic from "vite-plugin-serve-static";
14
15
 
15
16
  const serveStaticPlugin = serveStatic([
16
17
  {
17
18
  pattern: /^\/metadata\.json/,
18
- resolve: "./metadata.json",
19
+ resolve: path.join(".", "metadata.json"),
19
20
  },
20
21
  {
21
22
  pattern: /^\/dog-photos\/.*/,
@@ -40,6 +41,6 @@ Each `pattern` is defined as a [regular expression]. The `resolve` property can
40
41
 
41
42
  ## License
42
43
 
43
- Licensed under the [MIT License](https://github.com/reifiedbeans/vite-plugin-serve-static/blob/main/LICENSE).
44
+ Licensed under the [MIT License](https://github.com/typeparameter/vite-plugin-serve-static/blob/main/LICENSE).
44
45
 
45
46
  [regular expression]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
@@ -0,0 +1,13 @@
1
+ import { Plugin } from "vite";
2
+
3
+ //#region lib/config.d.ts
4
+ type ResolveFn = (match: RegExpExecArray) => string;
5
+ type Config = {
6
+ readonly pattern: RegExp;
7
+ readonly resolve: string | ResolveFn;
8
+ }[];
9
+ //#endregion
10
+ //#region lib/index.d.ts
11
+ declare function serveStatic(config: Config): Plugin;
12
+ //#endregion
13
+ export { Config, ResolveFn, serveStatic as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,85 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import corsMiddleware from "cors";
4
+ import * as mime from "mime-types";
5
+
6
+ //#region lib/utils.ts
7
+ function isDevServer(server) {
8
+ return "pluginContainer" in server;
9
+ }
10
+ function setupLogger(logger) {
11
+ const defaultOptions = { timestamp: true };
12
+ function applyDefaultOptions(log) {
13
+ return function(msg, options) {
14
+ log(msg, {
15
+ ...defaultOptions,
16
+ ...options
17
+ });
18
+ };
19
+ }
20
+ return {
21
+ clearScreen: logger.clearScreen,
22
+ hasErrorLogged: logger.hasErrorLogged,
23
+ hasWarned: logger.hasWarned,
24
+ info: applyDefaultOptions(logger.info),
25
+ warn: applyDefaultOptions(logger.warn),
26
+ warnOnce: applyDefaultOptions(logger.warnOnce),
27
+ error: applyDefaultOptions(logger.error)
28
+ };
29
+ }
30
+
31
+ //#endregion
32
+ //#region lib/middleware.ts
33
+ function createMiddleware(config, rawLogger) {
34
+ const log = setupLogger(rawLogger);
35
+ return function serveStaticMiddleware(req, res, next) {
36
+ if (!req.url) return next();
37
+ for (const { pattern, resolve } of config) {
38
+ const match = pattern.exec(req.url);
39
+ if (match) {
40
+ const filePath = typeof resolve === "string" ? resolve : resolve(match);
41
+ const stats = fs.statSync(filePath, { throwIfNoEntry: false });
42
+ if (!stats || !stats.isFile()) {
43
+ res.writeHead(404);
44
+ res.end("Not found");
45
+ log.error(`File ${filePath} is not a file`);
46
+ return;
47
+ }
48
+ const type = mime.contentType(path.basename(filePath));
49
+ res.writeHead(200, {
50
+ "Content-Length": stats.size,
51
+ "Content-Type": type || "application/octet-stream"
52
+ });
53
+ fs.createReadStream(filePath).pipe(res);
54
+ return;
55
+ }
56
+ }
57
+ return next();
58
+ };
59
+ }
60
+ function applyMiddleware(server, pluginConfig) {
61
+ const pluginMiddleware = createMiddleware(pluginConfig, server.config.logger);
62
+ const corsConfig = isDevServer(server) ? server.config.server.cors : server.config.preview.cors;
63
+ if (corsConfig !== false) {
64
+ const config = typeof corsConfig === "boolean" ? {} : corsConfig;
65
+ server.middlewares.use(corsMiddleware(config));
66
+ }
67
+ server.middlewares.use(pluginMiddleware);
68
+ }
69
+
70
+ //#endregion
71
+ //#region lib/index.ts
72
+ function serveStatic(config) {
73
+ return {
74
+ name: "serve-static",
75
+ configureServer(server) {
76
+ applyMiddleware(server, config);
77
+ },
78
+ configurePreviewServer(server) {
79
+ applyMiddleware(server, config);
80
+ }
81
+ };
82
+ }
83
+
84
+ //#endregion
85
+ export { serveStatic as default };
package/package.json CHANGED
@@ -1,30 +1,34 @@
1
1
  {
2
2
  "name": "vite-plugin-serve-static",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A Vite plugin for serving static files during local development",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/reifiedbeans/vite-plugin-serve-static.git"
7
+ "url": "git+https://github.com/typeparameter/vite-plugin-serve-static.git"
8
8
  },
9
9
  "license": "MIT",
10
- "homepage": "https://github.com/reifiedbeans/vite-plugin-serve-static",
10
+ "homepage": "https://github.com/typeparameter/vite-plugin-serve-static",
11
11
  "keywords": [
12
12
  "vite-plugin"
13
13
  ],
14
14
  "type": "module",
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
15
18
  "files": [
16
19
  "dist"
17
20
  ],
18
- "main": "dist/index.js",
19
- "types": "dist/index.d.ts",
21
+ "main": "dist/index.mjs",
22
+ "types": "dist/index.d.mts",
23
+ "packageManager": "pnpm@10.0.0",
20
24
  "scripts": {
21
- "build": "tsc && tsup",
25
+ "build": "tsdown",
22
26
  "clean": "rimraf dist",
23
- "format": "prettier --write .",
24
- "format:check": "prettier --check .",
25
- "lint": "eslint --cache --max-warnings=0 .",
26
- "lint:fix": "eslint --fix --cache --max-warnings=0 .",
27
- "prepack": "npm-run-all build test lint format:check",
27
+ "format": "prettier --cache --write .",
28
+ "format:check": "prettier --cache --check .",
29
+ "lint": "tsc && eslint --cache --max-warnings=0 .",
30
+ "lint:fix": "tsc && eslint --fix --cache --max-warnings=0 .",
31
+ "prepack": "pnpm build",
28
32
  "test": "vitest --run"
29
33
  },
30
34
  "peerDependencies": {
@@ -35,23 +39,24 @@
35
39
  "mime-types": "^2.1.35"
36
40
  },
37
41
  "devDependencies": {
38
- "@trivago/prettier-plugin-sort-imports": "^5.2.0",
42
+ "@eslint/compat": "^2.0.0",
43
+ "@eslint/js": "^9.39.2",
44
+ "@ianvs/prettier-plugin-sort-imports": "^4.7.0",
39
45
  "@types/cors": "^2.8.17",
40
46
  "@types/mime-types": "^2.1.4",
41
- "@typescript-eslint/eslint-plugin": "^8.18.0",
42
- "@typescript-eslint/parser": "^8.18.0",
43
47
  "conventional-changelog-conventionalcommits": "^8.0.0",
44
- "eslint": "^8.57.1",
45
- "eslint-config-prettier": "^9.1.0",
46
- "npm-run-all": "^4.1.5",
48
+ "eslint": "^9.39.2",
49
+ "eslint-config-prettier": "^10.1.8",
47
50
  "prettier": "^3.4.2",
48
51
  "rimraf": "^6.0.1",
49
- "tsup": "~8.4.0",
52
+ "tsdown": "^0.18.4",
50
53
  "typescript": "~5.7.2",
51
- "vitest": "^3.0.8"
54
+ "typescript-eslint": "^8.51.0",
55
+ "vitest": "^4.0.16"
52
56
  },
53
57
  "publishConfig": {
54
- "access": "public"
58
+ "access": "public",
59
+ "provenance": true
55
60
  },
56
61
  "release": {
57
62
  "preset": "conventionalcommits"
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { Plugin } from 'vite';
2
-
3
- type ResolveFn = (match: RegExpExecArray) => string;
4
- type Config = {
5
- readonly pattern: RegExp;
6
- readonly resolve: string | ResolveFn;
7
- }[];
8
-
9
- declare function serveStatic(config: Config): Plugin;
10
-
11
- export { type Config, type ResolveFn, serveStatic as default };
package/dist/index.js DELETED
@@ -1,84 +0,0 @@
1
- // lib/middleware.ts
2
- import * as mime from "mime-types";
3
- import corsMiddleware from "cors";
4
- import fs from "fs";
5
- import path from "path";
6
-
7
- // lib/utils.ts
8
- function isDevServer(server) {
9
- return "pluginContainer" in server;
10
- }
11
- function setupLogger(logger) {
12
- const defaultOptions = { timestamp: true };
13
- function applyDefaultOptions(log) {
14
- return function(msg, options) {
15
- log(msg, { ...defaultOptions, ...options });
16
- };
17
- }
18
- return {
19
- clearScreen: logger.clearScreen,
20
- hasErrorLogged: logger.hasErrorLogged,
21
- hasWarned: logger.hasWarned,
22
- info: applyDefaultOptions(logger.info),
23
- warn: applyDefaultOptions(logger.warn),
24
- warnOnce: applyDefaultOptions(logger.warnOnce),
25
- error: applyDefaultOptions(logger.error)
26
- };
27
- }
28
-
29
- // lib/middleware.ts
30
- function createMiddleware(config, rawLogger) {
31
- const log = setupLogger(rawLogger);
32
- return function serveStaticMiddleware(req, res, next) {
33
- if (!req.url) {
34
- return next();
35
- }
36
- for (const { pattern, resolve } of config) {
37
- const match = pattern.exec(req.url);
38
- if (match) {
39
- const filePath = typeof resolve === "string" ? resolve : resolve(match);
40
- const stats = fs.statSync(filePath, { throwIfNoEntry: false });
41
- if (!stats || !stats.isFile()) {
42
- res.writeHead(404);
43
- res.end("Not found");
44
- log.error(`File ${filePath} is not a file`);
45
- return;
46
- }
47
- const type = mime.contentType(path.basename(filePath));
48
- res.writeHead(200, {
49
- "Content-Length": stats.size,
50
- "Content-Type": type || "application/octet-stream"
51
- });
52
- const stream = fs.createReadStream(filePath);
53
- stream.pipe(res);
54
- return;
55
- }
56
- }
57
- return next();
58
- };
59
- }
60
- function applyMiddleware(server, pluginConfig) {
61
- const pluginMiddleware = createMiddleware(pluginConfig, server.config.logger);
62
- const corsConfig = isDevServer(server) ? server.config.server.cors : server.config.preview.cors;
63
- if (corsConfig !== false) {
64
- const config = typeof corsConfig === "boolean" ? {} : corsConfig;
65
- server.middlewares.use(corsMiddleware(config));
66
- }
67
- server.middlewares.use(pluginMiddleware);
68
- }
69
-
70
- // lib/index.ts
71
- function serveStatic(config) {
72
- return {
73
- name: "serve-static",
74
- configureServer(server) {
75
- applyMiddleware(server, config);
76
- },
77
- configurePreviewServer(server) {
78
- applyMiddleware(server, config);
79
- }
80
- };
81
- }
82
- export {
83
- serveStatic as default
84
- };