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(dev-server): enable gzip compression, add devServer.compress config #3723

Merged
merged 6 commits into from
May 22, 2023
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
10 changes: 10 additions & 0 deletions .changeset/lovely-jars-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@modern-js/builder-shared': patch
'@modern-js/builder-doc': patch
'@modern-js/server': patch
'@modern-js/types': patch
---

feat(dev-server): enable gzip compression, add devServer.compress config

feat(dev-server): 默认启用 gzip 压缩,新增 devServer.compress 配置项
1 change: 1 addition & 0 deletions packages/builder/builder-shared/src/schema/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const sharedDevServerConfigSchema = z.partialObj({
]),
}),
historyApiFallback: z.union([z.boolean(), z.record(z.unknown())]),
compress: z.boolean(),
hot: z.boolean(),
https: DevServerHttpsOptionsSchema,
liveReload: z.boolean(),
Expand Down
19 changes: 19 additions & 0 deletions packages/document/builder-doc/docs/en/config/tools/devServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ const defaultConfig = {

The config of HMR client, which are usually used to set the WebSocket URL of HMR.

#### compress

- **Type:** `boolean`
- **Default:** `true`

Whether to enable gzip compression for served static resources.

If you want to disable the gzip compression, you can set `compress` to `false`:

```ts
export default {
tools: {
devServer: {
compress: false,
},
},
};
```

#### devMiddleware

- **Type:**
Expand Down
19 changes: 19 additions & 0 deletions packages/document/builder-doc/docs/zh/config/tools/devServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ const defaultConfig = {

对应 HMR 客户端的配置,通常用于设置 HMR 对应的 WebSocket URL。

#### compress

- **类型:** `boolean`
- **默认值:** `true`

是否对静态资源启用 gzip 压缩。

如果你需要禁用 gzip 压缩,可以将 `compress` 设置为 `false`:

```ts
export default {
tools: {
devServer: {
compress: false,
},
},
};
```

#### devMiddleware

- **类型:**
Expand Down
5 changes: 3 additions & 2 deletions packages/server/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
"@modern-js/server-utils": "workspace:*",
"@modern-js/types": "workspace:*",
"@modern-js/utils": "workspace:*",
"@swc/helpers": "0.5.1",
"connect-history-api-fallback": "^2.0.0",
"http-compression": "1.0.6",
"minimatch": "^3.0.4",
"path-to-regexp": "^6.2.0",
"ws": "^8.2.0",
"@swc/helpers": "0.5.1"
"ws": "^8.2.0"
},
"devDependencies": {
"@modern-js/server-core": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/server/server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const getDefaultDevOptions = (): DevServerOptions => {
devMiddleware: { writeToDisk: true },
watch: true,
hot: true,
compress: true,
liveReload: true,
};
};
13 changes: 13 additions & 0 deletions packages/server/server/src/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ export class ModernDevServer extends ModernServer {
private async applyDefaultMiddlewares(app: Server) {
const { pwd, dev, devMiddleware } = this;

// compression should be the first middleware
if (dev.compress) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error http-compression does not provide a type definition
const { default: compression } = await import('http-compression');
this.addHandler((ctx, next) => {
compression({
gzip: true,
brotli: false,
})(ctx.req, ctx.res, next);
});
}

this.addHandler((ctx: ModernServerContext, next: NextFunction) => {
// allow hmr request cross-domain, because the user may use global proxy
ctx.res.setHeader('Access-Control-Allow-Origin', '*');
Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/types/server/devServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type DevServerOptions = {
host?: string;
protocol?: string;
};
/** Whether to enable gzip compression */
compress?: boolean;
devMiddleware?: {
writeToDisk?: boolean | ((filename: string) => boolean);
outputFileSystem?: Record<string, any>;
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

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