Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support launch editor #1381

Merged
merged 2 commits into from
Jan 21, 2024
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
8 changes: 7 additions & 1 deletion e2e/cases/server/custom-server/scripts/pure-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export async function startDevServerPure(fixtures) {
res.end('Hello World!');
});

app.use(...middlewares);
middlewares.forEach((item) => {
if (Array.isArray(item)) {
app.use(...item);
} else {
app.use(item);
}
});

app.get('/bbb', (_req, res) => {
res.end('Hello Express!');
Expand Down
8 changes: 7 additions & 1 deletion e2e/cases/server/custom-server/scripts/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export async function startDevServer(fixtures) {
res.end('Hello World!');
});

app.use(...middlewares);
middlewares.forEach((item) => {
if (Array.isArray(item)) {
app.use(...item);
} else {
app.use(item);
}
});

app.get('/bbb', (_req, res) => {
res.end('Hello Express!');
Expand Down
1 change: 1 addition & 0 deletions packages/core/compiled/launch-editor-middleware/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export = any;
1 change: 1 addition & 0 deletions packages/core/compiled/launch-editor-middleware/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions packages/core/compiled/launch-editor-middleware/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-present, Yuxi (Evan) You

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"launch-editor-middleware","author":"Evan You","version":"2.6.1","license":"MIT","types":"index.d.ts","type":"commonjs"}
8 changes: 7 additions & 1 deletion packages/core/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ export async function startDevServer<
compileMiddlewareAPI,
});

devMiddlewares.middlewares.forEach((m) => middlewares.use(m));
devMiddlewares.middlewares.forEach((item) => {
if (Array.isArray(item)) {
middlewares.use(...item);
} else {
middlewares.use(item);
}
});

middlewares.use(notFoundMiddleware);

Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/server/getDevMiddlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import url from 'url';
import type {
ServerAPIs,
Middlewares,
UpgradeEvent,
RequestHandler,
DevMiddlewaresConfig,
Expand Down Expand Up @@ -56,7 +57,7 @@ const applyDefaultMiddlewares = async ({
}: {
output: RsbuildDevMiddlewareOptions['output'];
pwd: RsbuildDevMiddlewareOptions['pwd'];
middlewares: RequestHandler[];
middlewares: Middlewares;
dev: RsbuildDevMiddlewareOptions['dev'];
compileMiddlewareAPI?: CompileMiddlewareAPI;
}): Promise<{
Expand Down Expand Up @@ -106,6 +107,11 @@ const applyDefaultMiddlewares = async ({
});
}

const { default: launchEditorMiddleware } = await import(
'../../compiled/launch-editor-middleware'
);
middlewares.push(['/__open-in-editor', launchEditorMiddleware()]);

if (compileMiddlewareAPI) {
middlewares.push(compileMiddlewareAPI.middleware);

Expand Down Expand Up @@ -164,7 +170,7 @@ const applyDefaultMiddlewares = async ({
};

export const getMiddlewares = async (options: RsbuildDevMiddlewareOptions) => {
const middlewares: RequestHandler[] = [];
const middlewares: Middlewares = [];
const { compileMiddlewareAPI } = options;

// Order: setupMiddlewares.unshift => internal middlewares => setupMiddlewares.push
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/src/types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export type CompileMiddlewareAPI = {
close: () => void;
};

export type Middlewares = Array<RequestHandler | [string, RequestHandler]>;

export type DevServerAPIs = {
/**
* The resolved rsbuild server config
Expand Down Expand Up @@ -131,7 +133,7 @@ export type DevServerAPIs = {
*/
overrides?: DevMiddlewaresConfig;
}) => Promise<{
middlewares: RequestHandler[];
middlewares: Middlewares;
close: () => Promise<void>;
/**
* Subscribe http upgrade event
Expand Down
45 changes: 44 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading