Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import path from "path";
import { defineConfig } from "vite";
import serveStatic from "vite-plugin-serve-static";

const serveStaticPlugin = serveStatic([
{
pattern: /^\/metadata\.json/,
resolve: path.join(".", "metadata.json"),
},
{
pattern: /^\/dog-photos\/.*/,
resolve: ([match]) => path.join("..", "dog-photos", match),
},
{
pattern: /^\/author-photos\/(.*)/,
resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
},
]);
const serveStaticPlugin = serveStatic({
rules: [
{
pattern: /^\/metadata\.json/,
resolve: path.join(".", "metadata.json"),
},
{
pattern: /^\/dog-photos\/.*/,
resolve: ([match]) => path.join("..", "dog-photos", match),
},
{
pattern: /^\/author-photos\/(.*)/,
resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
},
],
});

export default defineConfig({
plugins: [serveStaticPlugin],
Expand All @@ -35,9 +37,25 @@ export default defineConfig({

## Config

The configuration is defined as an array of objects defining which patterns to intercept and how to resolve them.
The configuration is provided as an object with `rules`, plus an optional global `contentType`.

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. Rules can also specify `headers` to apply per match.

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.
```typescript
const serveStaticPlugin = serveStatic({
contentType: "text/plain",
rules: [
{
pattern: /^\/metadata\.json/,
resolve: path.join(".", "metadata.json"),
headers: {
"Cache-Control": "no-store",
"X-Static-File": "true",
},
},
],
});
```

## License

Expand Down
30 changes: 16 additions & 14 deletions apps/example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ const staticDir = path.join(__dirname, "static");

export default defineConfig({
plugins: [
serveStatic([
{
pattern: /^\/metadata\.json$/,
resolve: path.join(staticDir, "metadata.json"),
},
{
pattern: /^\/data\/(.*)$/,
resolve: (match: RegExpExecArray) => path.join(staticDir, "data", `${match[1]!}.csv`),
},
{
pattern: /^\/pages\/(.*)$/,
resolve: (match: RegExpExecArray) => path.join(staticDir, "pages", `${match[1]!}.html`),
},
]),
serveStatic({
rules: [
{
pattern: /^\/metadata\.json$/,
resolve: path.join(staticDir, "metadata.json"),
},
{
pattern: /^\/data\/(.*)$/,
resolve: (match: RegExpExecArray) => path.join(staticDir, "data", `${match[1]!}.csv`),
},
{
pattern: /^\/pages\/(.*)$/,
resolve: (match: RegExpExecArray) => path.join(staticDir, "pages", `${match[1]!}.html`),
},
],
}),
],
});
34 changes: 18 additions & 16 deletions packages/vite-plugin-serve-static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import path from "path";
import { defineConfig } from "vite";
import serveStatic from "vite-plugin-serve-static";

const serveStaticPlugin = serveStatic([
{
pattern: /^\/metadata\.json/,
resolve: path.join(".", "metadata.json"),
},
{
pattern: /^\/dog-photos\/.*/,
resolve: ([match]) => path.join("..", "dog-photos", match),
},
{
pattern: /^\/author-photos\/(.*)/,
resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
},
]);
const serveStaticPlugin = serveStatic({
rules: [
{
pattern: /^\/metadata\.json/,
resolve: path.join(".", "metadata.json"),
},
{
pattern: /^\/dog-photos\/.*/,
resolve: ([match]) => path.join("..", "dog-photos", match),
},
{
pattern: /^\/author-photos\/(.*)/,
resolve: (groups) => path.join("..", "authors", groups[1]) + ".jpg",
},
],
});

export default defineConfig({
plugins: [serveStaticPlugin],
Expand All @@ -35,9 +37,9 @@ export default defineConfig({

## Config

The configuration is defined as an array of objects defining which patterns to intercept and how to resolve them.
The configuration is provided as an object with `rules`, plus an optional global `contentType`.

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.
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.

## License

Expand Down
Loading