Skip to content

Commit 88efcf6

Browse files
authored
Initial proxy code (#97)
1 parent 4052eca commit 88efcf6

File tree

12 files changed

+294
-2
lines changed

12 files changed

+294
-2
lines changed

demo/docusaurus.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const config = {
3838
theme: {
3939
customCss: require.resolve("./src/css/custom.css"),
4040
},
41+
proxy: {
42+
"/proxy": {
43+
target: "http://localhost:8091",
44+
pathRewrite: { "^/proxy": "" },
45+
},
46+
},
4147
}),
4248
],
4349
],
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "docusaurus-plugin-proxy",
3+
"description": "A dev server proxy for Docusaurus.",
4+
"version": "0.2.3",
5+
"license": "MIT",
6+
"publishConfig": {
7+
"access": "public"
8+
},
9+
"types": "src/plugin-proxy.d.ts",
10+
"main": "dist/index.js",
11+
"scripts": {
12+
"build": "scripts/build.ts",
13+
"watch": "scripts/build.ts --watch"
14+
},
15+
"devDependencies": {
16+
"@types/concurrently": "^6.3.0",
17+
"@types/webpack-dev-server": "^4.5.0",
18+
"concurrently": "^5.2.0",
19+
"cpx": "^1.5.0"
20+
},
21+
"dependencies": {
22+
"@docusaurus/core": "^2.0.0-beta.13",
23+
"@docusaurus/types": "^2.0.0-beta.13"
24+
},
25+
"engines": {
26+
"node": ">=14"
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env ts-node
2+
/* ============================================================================
3+
* Copyright (c) Cloud Annotations
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
* ========================================================================== */
8+
9+
import concurrently, { CommandObj } from "concurrently";
10+
11+
const watch = process.argv.includes("--watch");
12+
13+
const watchFlag = watch ? "--watch" : "";
14+
15+
const commands: CommandObj[] = [
16+
{
17+
command: `tsc --preserveWatchOutput ${watchFlag}`,
18+
name: "tsc",
19+
prefixColor: "yellow",
20+
},
21+
{
22+
// Copy all files BUT *.ts and *.tsx (which will be turned into JS by TS).
23+
// Notice that we include *.d.ts, they're perfectly valid to copy.
24+
command: `cpx 'src/**/{*.d.ts,!(*.ts|*.tsx)}' dist ${watchFlag}`,
25+
name: "cpx",
26+
prefixColor: "yellow",
27+
},
28+
];
29+
30+
concurrently(commands, {
31+
killOthers: ["failure"],
32+
prefix: "[{time} {name}]",
33+
timestampFormat: "mm:ss.S",
34+
}).catch(() => {
35+
process.exit(1);
36+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import { LoadContext, Plugin } from "@docusaurus/types";
9+
10+
import { PluginOptions } from "./types";
11+
12+
export default function pluginOpenAPI(
13+
_context: LoadContext,
14+
options: PluginOptions
15+
): Plugin {
16+
return {
17+
name: "docusaurus-plugin-proxy",
18+
19+
configureWebpack() {
20+
const { proxy } = options;
21+
22+
return {
23+
devServer: {
24+
proxy: proxy,
25+
},
26+
} as any;
27+
},
28+
};
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import type { PluginOptions } from "./types";
9+
10+
export const DEFAULT_OPTIONS: Omit<PluginOptions, "id"> = {
11+
proxy: undefined,
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
declare module "docusaurus-plugin-proxy" {
9+
export type Options = Partial<import("./types").PluginOptions>;
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* ============================================================================
2+
* Copyright (c) Cloud Annotations
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import type { Configuration } from "webpack-dev-server";
9+
10+
export interface PluginOptions {
11+
proxy: Configuration["proxy"];
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": ["src"]
7+
}

packages/docusaurus-preset-openapi/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@docusaurus/theme-classic": "2.0.0-beta.13",
4343
"@docusaurus/theme-search-algolia": "2.0.0-beta.13",
4444
"docusaurus-plugin-openapi": "^0.2.3",
45+
"docusaurus-plugin-proxy": "^0.2.3",
4546
"docusaurus-theme-openapi": "^0.2.3"
4647
},
4748
"peerDependencies": {

packages/docusaurus-preset-openapi/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function preset(
2323
context: LoadContext,
2424
options: Options = {}
2525
): Preset {
26-
const { api, ...rest } = options;
26+
const { proxy, api, ...rest } = options;
2727

2828
const { themes = [], plugins = [] } = presetClassic(context, rest);
2929

@@ -33,5 +33,9 @@ export default function preset(
3333
plugins.push(makePluginConfig("docusaurus-plugin-openapi", api));
3434
}
3535

36+
if (proxy !== false) {
37+
plugins.push(makePluginConfig("docusaurus-plugin-proxy", proxy));
38+
}
39+
3640
return { themes, plugins };
3741
}

0 commit comments

Comments
 (0)