vite-plugin-serve-static 2.0.0 → 2.1.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 +1 -1
- package/README.md +19 -17
- package/dist/index.d.mts +17 -3
- package/dist/index.mjs +26 -6
- package/package.json +1 -15
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -13,20 +13,22 @@ import path from "path";
|
|
|
13
13
|
import { defineConfig } from "vite";
|
|
14
14
|
import serveStatic from "vite-plugin-serve-static";
|
|
15
15
|
|
|
16
|
-
const serveStaticPlugin = serveStatic(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
const serveStaticPlugin = serveStatic({
|
|
17
|
+
rules: [
|
|
18
|
+
{
|
|
19
|
+
pattern: /^\/metadata\.json/,
|
|
20
|
+
resolve: path.join(".", "metadata.json"),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
pattern: /^\/dog-photos\/.*/,
|
|
24
|
+
resolve: ([match]) => path.join("..", "dog-photos", match),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
pattern: /^\/author-photos\/(.*)/,
|
|
28
|
+
resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
});
|
|
30
32
|
|
|
31
33
|
export default defineConfig({
|
|
32
34
|
plugins: [serveStaticPlugin],
|
|
@@ -35,12 +37,12 @@ export default defineConfig({
|
|
|
35
37
|
|
|
36
38
|
## Config
|
|
37
39
|
|
|
38
|
-
The configuration is
|
|
40
|
+
The configuration is provided as an object with `rules`, plus an optional global `contentType`.
|
|
39
41
|
|
|
40
|
-
Each `pattern` is defined as a [regular expression]. The `resolve` property can either be a string containing the path to a single file or a function that returns a string given the result of executing the `pattern` against the request path.
|
|
42
|
+
Each rule defines which patterns to intercept and how to resolve them. Each `pattern` is defined as a [regular expression]. The `resolve` property can either be a string containing the path to a single file or a function that returns a string given the result of executing the `pattern` against the request path.
|
|
41
43
|
|
|
42
44
|
## License
|
|
43
45
|
|
|
44
|
-
Licensed under the [MIT License](https://github.com/typeparameter/vite-plugin-serve-static/blob/main/LICENSE).
|
|
46
|
+
Licensed under the [MIT License](https://github.com/typeparameter/vite-plugin-serve-static/blob/main/packages/vite-plugin-serve-static/LICENSE).
|
|
45
47
|
|
|
46
48
|
[regular expression]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
|
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
import { Plugin } from "vite";
|
|
2
|
+
import http from "http";
|
|
2
3
|
|
|
3
4
|
//#region lib/config.d.ts
|
|
4
5
|
type ResolveFn = (match: RegExpExecArray) => string;
|
|
5
|
-
type
|
|
6
|
+
type RuleConfig = {
|
|
6
7
|
readonly pattern: RegExp;
|
|
7
8
|
readonly resolve: string | ResolveFn;
|
|
8
|
-
|
|
9
|
+
readonly headers?: http.OutgoingHttpHeaders;
|
|
10
|
+
};
|
|
11
|
+
type Config = RuleConfig[] | {
|
|
12
|
+
readonly rules: RuleConfig[];
|
|
13
|
+
readonly contentType?: string;
|
|
14
|
+
};
|
|
15
|
+
declare function normalizeConfig(config: Config): {
|
|
16
|
+
contentType?: string;
|
|
17
|
+
rules: {
|
|
18
|
+
headers: http.OutgoingHttpHeaders;
|
|
19
|
+
pattern: RegExp;
|
|
20
|
+
resolve: string | ResolveFn;
|
|
21
|
+
}[];
|
|
22
|
+
};
|
|
9
23
|
//#endregion
|
|
10
24
|
//#region lib/index.d.ts
|
|
11
25
|
declare function serveStatic(config: Config): Plugin;
|
|
12
26
|
//#endregion
|
|
13
|
-
export { Config, ResolveFn, serveStatic as default };
|
|
27
|
+
export { Config, ResolveFn, serveStatic as default, normalizeConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,24 @@ import path from "path";
|
|
|
3
3
|
import corsMiddleware from "cors";
|
|
4
4
|
import * as mime from "mime-types";
|
|
5
5
|
|
|
6
|
+
//#region lib/config.ts
|
|
7
|
+
function normalizeConfig(config) {
|
|
8
|
+
const { rules, ...rest } = Array.isArray(config) ? { rules: config } : config;
|
|
9
|
+
return {
|
|
10
|
+
rules: rules.map((rule) => ({
|
|
11
|
+
...rule,
|
|
12
|
+
headers: normalizeHeaders(rule.headers)
|
|
13
|
+
})),
|
|
14
|
+
...rest
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function normalizeHeaders(headers) {
|
|
18
|
+
if (!headers) return {};
|
|
19
|
+
const entries = Object.entries(headers).filter(([, value]) => value !== void 0).map(([key, value]) => [key.toLowerCase(), value]);
|
|
20
|
+
return Object.fromEntries(entries);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
6
24
|
//#region lib/utils.ts
|
|
7
25
|
function isDevServer(server) {
|
|
8
26
|
return "pluginContainer" in server;
|
|
@@ -30,11 +48,12 @@ function setupLogger(logger) {
|
|
|
30
48
|
|
|
31
49
|
//#endregion
|
|
32
50
|
//#region lib/middleware.ts
|
|
33
|
-
function createMiddleware(
|
|
51
|
+
function createMiddleware(pluginConfig, rawLogger) {
|
|
34
52
|
const log = setupLogger(rawLogger);
|
|
53
|
+
const config = normalizeConfig(pluginConfig);
|
|
35
54
|
return function serveStaticMiddleware(req, res, next) {
|
|
36
55
|
if (!req.url) return next();
|
|
37
|
-
for (const { pattern, resolve } of config) {
|
|
56
|
+
for (const { pattern, resolve, headers } of config.rules) {
|
|
38
57
|
const match = pattern.exec(req.url);
|
|
39
58
|
if (match) {
|
|
40
59
|
const filePath = typeof resolve === "string" ? resolve : resolve(match);
|
|
@@ -45,10 +64,11 @@ function createMiddleware(config, rawLogger) {
|
|
|
45
64
|
log.error(`File ${filePath} is not a file`);
|
|
46
65
|
return;
|
|
47
66
|
}
|
|
48
|
-
const
|
|
67
|
+
const contentType = headers["content-type"] || config.contentType || mime.contentType(path.basename(filePath)) || "application/octet-stream";
|
|
49
68
|
res.writeHead(200, {
|
|
50
|
-
"
|
|
51
|
-
"
|
|
69
|
+
"content-length": stats.size,
|
|
70
|
+
"content-type": contentType,
|
|
71
|
+
...headers
|
|
52
72
|
});
|
|
53
73
|
fs.createReadStream(filePath).pipe(res);
|
|
54
74
|
return;
|
|
@@ -82,4 +102,4 @@ function serveStatic(config) {
|
|
|
82
102
|
}
|
|
83
103
|
|
|
84
104
|
//#endregion
|
|
85
|
-
export { serveStatic as default };
|
|
105
|
+
export { serveStatic as default, normalizeConfig };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-serve-static",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A Vite plugin for serving static files during local development",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,12 +20,9 @@
|
|
|
20
20
|
],
|
|
21
21
|
"main": "dist/index.mjs",
|
|
22
22
|
"types": "dist/index.d.mts",
|
|
23
|
-
"packageManager": "pnpm@10.0.0",
|
|
24
23
|
"scripts": {
|
|
25
24
|
"build": "tsdown",
|
|
26
25
|
"clean": "rimraf dist",
|
|
27
|
-
"format": "prettier --cache --write .",
|
|
28
|
-
"format:check": "prettier --cache --check .",
|
|
29
26
|
"lint": "tsc && eslint --cache --max-warnings=0 .",
|
|
30
27
|
"lint:fix": "tsc && eslint --fix --cache --max-warnings=0 .",
|
|
31
28
|
"prepack": "pnpm build",
|
|
@@ -39,26 +36,15 @@
|
|
|
39
36
|
"mime-types": "^2.1.35"
|
|
40
37
|
},
|
|
41
38
|
"devDependencies": {
|
|
42
|
-
"@eslint/compat": "^2.0.0",
|
|
43
|
-
"@eslint/js": "^9.39.2",
|
|
44
|
-
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
45
39
|
"@types/cors": "^2.8.17",
|
|
46
40
|
"@types/mime-types": "^2.1.4",
|
|
47
|
-
"conventional-changelog-conventionalcommits": "^8.0.0",
|
|
48
|
-
"eslint": "^9.39.2",
|
|
49
|
-
"eslint-config-prettier": "^10.1.8",
|
|
50
|
-
"prettier": "^3.4.2",
|
|
51
41
|
"rimraf": "^6.0.1",
|
|
52
42
|
"tsdown": "^0.18.4",
|
|
53
43
|
"typescript": "~5.7.2",
|
|
54
|
-
"typescript-eslint": "^8.51.0",
|
|
55
44
|
"vitest": "^4.0.16"
|
|
56
45
|
},
|
|
57
46
|
"publishConfig": {
|
|
58
47
|
"access": "public",
|
|
59
48
|
"provenance": true
|
|
60
|
-
},
|
|
61
|
-
"release": {
|
|
62
|
-
"preset": "conventionalcommits"
|
|
63
49
|
}
|
|
64
50
|
}
|