From 7f9399b7fc1cbf488745d1a69e7b2cf7af20f5cb Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 21 Jun 2024 17:20:02 +0800 Subject: [PATCH 1/2] refactor!: remove distPath.server --- e2e/cases/babel/preset-node/index.test.ts | 3 ++- e2e/cases/node-addons/index.test.ts | 14 ++++++------- .../output/externals/tests/index.test.ts | 2 +- .../performance/print-file-size/index.test.ts | 5 ++++- .../print-file-size/rsbuild.config.ts | 21 +++++++++++++++++++ .../tests/__snapshots__/default.test.ts.snap | 2 +- packages/core/src/config.ts | 2 -- packages/core/src/constants.ts | 1 - packages/core/src/plugins/output.ts | 3 --- .../tests/__snapshots__/default.test.ts.snap | 2 +- .../__snapshots__/environments.test.ts.snap | 6 ------ .../tests/__snapshots__/output.test.ts.snap | 2 +- packages/core/tests/output.test.ts | 4 ++-- packages/shared/src/types/config/output.ts | 5 ----- website/docs/en/config/output/dist-path.mdx | 3 --- website/docs/en/guide/basic/output-files.mdx | 18 ++++++++++++---- website/docs/zh/config/output/dist-path.mdx | 3 --- website/docs/zh/guide/basic/output-files.mdx | 20 +++++++++++++----- 18 files changed, 68 insertions(+), 48 deletions(-) diff --git a/e2e/cases/babel/preset-node/index.test.ts b/e2e/cases/babel/preset-node/index.test.ts index e5d2110536..ca2ef370d0 100644 --- a/e2e/cases/babel/preset-node/index.test.ts +++ b/e2e/cases/babel/preset-node/index.test.ts @@ -9,7 +9,8 @@ rspackOnlyTest( runServer: true, }); - const { foo } = await import('./dist/server/index.js'); + // @ts-ignore .js file + const { foo } = await import('./dist/index.js'); expect(foo).toEqual(1); }, ); diff --git a/e2e/cases/node-addons/index.test.ts b/e2e/cases/node-addons/index.test.ts index 0ad8086b9a..1082214a4c 100644 --- a/e2e/cases/node-addons/index.test.ts +++ b/e2e/cases/node-addons/index.test.ts @@ -13,15 +13,15 @@ test('should compile Node addons correctly', async () => { file.endsWith('test.darwin.node'), ); - expect(addonFile?.includes('server/test.darwin.node')).toBeTruthy(); + expect(addonFile?.includes('/test.darwin.node')).toBeTruthy(); expect( - fs.existsSync(join(__dirname, 'dist', 'server', 'test.darwin.node')), + fs.existsSync(join(__dirname, 'dist', 'test.darwin.node')), ).toBeTruthy(); // the `test.darwin.node` is only compatible with darwin if (process.platform === 'darwin') { - const content = await import('./dist/server/index.js'); + const content = await import('./dist/index.js'); expect(typeof (content.default as any).readLength).toEqual('function'); } }); @@ -61,14 +61,12 @@ test('should compile Node addons in the node_modules correctly', async () => { file.endsWith('other.node'), ); - expect(addonFile?.includes('server/other.node')).toBeTruthy(); + expect(addonFile?.includes('/other.node')).toBeTruthy(); - expect( - fs.existsSync(join(__dirname, 'dist', 'server', 'other.node')), - ).toBeTruthy(); + expect(fs.existsSync(join(__dirname, 'dist', 'other.node'))).toBeTruthy(); if (process.platform === 'darwin') { - const content = await import('./dist/server/index.js'); + const content = await import('./dist/index.js'); expect(typeof (content.default as any).readLength).toEqual('function'); } }); diff --git a/e2e/cases/output/externals/tests/index.test.ts b/e2e/cases/output/externals/tests/index.test.ts index 4d0a182efa..960685a8f7 100644 --- a/e2e/cases/output/externals/tests/index.test.ts +++ b/e2e/cases/output/externals/tests/index.test.ts @@ -1,4 +1,4 @@ -import { join, resolve } from 'node:path'; +import { resolve } from 'node:path'; import { build, gotoPage } from '@e2e/helper'; import { expect, test } from '@playwright/test'; import { pluginReact } from '@rsbuild/plugin-react'; diff --git a/e2e/cases/performance/print-file-size/index.test.ts b/e2e/cases/performance/print-file-size/index.test.ts index 12882880f6..a3ab29c4c9 100644 --- a/e2e/cases/performance/print-file-size/index.test.ts +++ b/e2e/cases/performance/print-file-size/index.test.ts @@ -33,7 +33,7 @@ test.describe('should print file size correctly', async () => { expect(logs.some((log) => log.includes('Gzipped size:'))).toBeTruthy(); }); - test('should print size of multiple targets correctly', async () => { + test.only('should print size of multiple targets correctly', async () => { await build({ cwd, rsbuildConfig: { @@ -52,6 +52,9 @@ test.describe('should print file size correctly', async () => { node: { output: { target: 'node', + distPath: { + root: 'dist/server', + }, }, }, }, diff --git a/e2e/cases/performance/print-file-size/rsbuild.config.ts b/e2e/cases/performance/print-file-size/rsbuild.config.ts index d8df500358..ce96b50ab8 100644 --- a/e2e/cases/performance/print-file-size/rsbuild.config.ts +++ b/e2e/cases/performance/print-file-size/rsbuild.config.ts @@ -2,4 +2,25 @@ import { pluginReact } from '@rsbuild/plugin-react'; export default { plugins: [pluginReact()], + output: { + filenameHash: false, + }, + performance: { + printFileSize: true, + }, + environments: { + web: { + output: { + target: 'web', + }, + }, + node: { + output: { + target: 'node', + distPath: { + root: 'dist/server', + }, + }, + }, + }, }; diff --git a/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap b/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap index d869ecf176..b5342e15ac 100644 --- a/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap +++ b/packages/compat/webpack/tests/__snapshots__/default.test.ts.snap @@ -1038,7 +1038,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe "library": { "type": "commonjs2", }, - "path": "/packages/compat/webpack/tests/dist/server", + "path": "/packages/compat/webpack/tests/dist", "pathinfo": false, "publicPath": "/", "webassemblyModuleFilename": "static/wasm/[hash].module.wasm", diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 190e6a8aba..8e3361d337 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -35,7 +35,6 @@ import { JS_DIST_DIR, MEDIA_DIST_DIR, ROOT_DIST_DIR, - SERVER_DIST_DIR, SVG_DIST_DIR, TS_CONFIG_FILE, WASM_DIST_DIR, @@ -138,7 +137,6 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({ wasm: WASM_DIST_DIR, image: IMAGE_DIST_DIR, media: MEDIA_DIST_DIR, - server: SERVER_DIST_DIR, }, assetPrefix: DEFAULT_ASSET_PREFIX, filename: {}, diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index d968ea0a60..20db50b52a 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -5,7 +5,6 @@ import type { RsbuildTarget } from '@rsbuild/shared'; // loaders will be emitted to the same folder of the main bundle export const ROOT_DIST_DIR = 'dist'; export const HTML_DIST_DIR = '/'; -export const SERVER_DIST_DIR = 'server'; export const JS_DIST_DIR = 'static/js'; export const CSS_DIST_DIR = 'static/css'; export const SVG_DIST_DIR = 'static/svg'; diff --git a/packages/core/src/plugins/output.ts b/packages/core/src/plugins/output.ts index 38f5b65172..28be9a31df 100644 --- a/packages/core/src/plugins/output.ts +++ b/packages/core/src/plugins/output.ts @@ -99,10 +99,7 @@ export const pluginOutput = (): RsbuildPlugin => ({ .hashFunction('xxhash64'); if (isServer) { - const serverPath = config.output.distPath.server; - chain.output - .path(posix.join(api.context.distPath, serverPath)) .filename('[name].js') .chunkFilename('[name].js') .library({ diff --git a/packages/core/tests/__snapshots__/default.test.ts.snap b/packages/core/tests/__snapshots__/default.test.ts.snap index b78448f6e7..f626045f2f 100644 --- a/packages/core/tests/__snapshots__/default.test.ts.snap +++ b/packages/core/tests/__snapshots__/default.test.ts.snap @@ -1240,7 +1240,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe "library": { "type": "commonjs2", }, - "path": "/packages/core/tests/dist/server", + "path": "/packages/core/tests/dist", "pathinfo": false, "publicPath": "/", "webassemblyModuleFilename": "static/wasm/[hash].module.wasm", diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap index 0f0f77e994..308c594c47 100644 --- a/packages/core/tests/__snapshots__/environments.test.ts.snap +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -51,7 +51,6 @@ exports[`environment config > should normalize environment config correctly 1`] "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, @@ -173,7 +172,6 @@ exports[`environment config > should print environment config when inspect confi "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, @@ -291,7 +289,6 @@ exports[`environment config > should print environment config when inspect confi "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, @@ -414,7 +411,6 @@ exports[`environment config > should support modify environment config by api.mo "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, @@ -532,7 +528,6 @@ exports[`environment config > should support modify environment config by api.mo "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, @@ -651,7 +646,6 @@ exports[`environment config > should support modify environment config by api.mo "js": "static/js", "media": "static/media", "root": "dist", - "server": "server", "svg": "static/svg", "wasm": "static/wasm", }, diff --git a/packages/core/tests/__snapshots__/output.test.ts.snap b/packages/core/tests/__snapshots__/output.test.ts.snap index 6b19916bd2..c04e71ae6d 100644 --- a/packages/core/tests/__snapshots__/output.test.ts.snap +++ b/packages/core/tests/__snapshots__/output.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`plugin-output > should allow to custom server directory with distPath.server 1`] = ` +exports[`plugin-output > should allow to custom server directory with distPath.root 1`] = ` { "output": { "chunkFilename": "[name].js", diff --git a/packages/core/tests/output.test.ts b/packages/core/tests/output.test.ts index 487cc8e0c8..5fb74c4512 100644 --- a/packages/core/tests/output.test.ts +++ b/packages/core/tests/output.test.ts @@ -11,14 +11,14 @@ describe('plugin-output', () => { expect(bundlerConfigs[0]).toMatchSnapshot(); }); - it('should allow to custom server directory with distPath.server', async () => { + it('should allow to custom server directory with distPath.root', async () => { const rsbuild = await createStubRsbuild({ plugins: [pluginOutput()], rsbuildConfig: { output: { target: 'node', distPath: { - server: 'server', + root: 'dist/server', }, }, }, diff --git a/packages/shared/src/types/config/output.ts b/packages/shared/src/types/config/output.ts index bcab40588f..271c320e20 100644 --- a/packages/shared/src/types/config/output.ts +++ b/packages/shared/src/types/config/output.ts @@ -63,11 +63,6 @@ export type DistPathConfig = { * @default 'static/media' */ media?: string; - /** - * The output directory of server bundles when target is `node`. - * @default 'server' - */ - server?: string; }; export type FilenameConfig = { diff --git a/website/docs/en/config/output/dist-path.mdx b/website/docs/en/config/output/dist-path.mdx index ffac41879d..41ab50fb83 100644 --- a/website/docs/en/config/output/dist-path.mdx +++ b/website/docs/en/config/output/dist-path.mdx @@ -15,7 +15,6 @@ type DistPathConfig = { wasm?: string; image?: string; media?: string; - server?: string; }; ``` @@ -34,7 +33,6 @@ const defaultDistPath = { wasm: 'static/wasm', image: 'static/image', media: 'static/media', - server: 'server', }; ``` @@ -53,7 +51,6 @@ Detail: - `wasm`: The output directory of WebAssembly files. - `image`: The output directory of non-SVG images. - `media`: The output directory of media assets, such as videos. -- `server`: The output directory of server bundles when [target](/config/output/targets) is `node`. ### Root Directory diff --git a/website/docs/en/guide/basic/output-files.mdx b/website/docs/en/guide/basic/output-files.mdx index 305c3c7cf0..50d2084929 100644 --- a/website/docs/en/guide/basic/output-files.mdx +++ b/website/docs/en/guide/basic/output-files.mdx @@ -106,15 +106,25 @@ dist Node.js files usually only contain JS files, no HTML or CSS. Also, JS file names will not contain hash. -You can modify the output path of Node.js files through the [output.distPath.server](/config/output/dist-path) config. +You can modify the output path of Node.js files via the [environments](/config/environments) config. For example, output Node.js files to the `server` directory: ```ts export default { - output: { - distPath: { - server: 'server', + environments: { + web: { + output: { + target: 'web', + }, + }, + node: { + output: { + target: 'node', + distPath: { + root: 'dist/server', + }, + }, }, }, }; diff --git a/website/docs/zh/config/output/dist-path.mdx b/website/docs/zh/config/output/dist-path.mdx index a9625a1b73..b7d108c7bc 100644 --- a/website/docs/zh/config/output/dist-path.mdx +++ b/website/docs/zh/config/output/dist-path.mdx @@ -15,7 +15,6 @@ type DistPathConfig = { wasm?: string; image?: string; media?: string; - server?: string; }; ``` @@ -34,7 +33,6 @@ const defaultDistPath = { wasm: 'static/wasm', image: 'static/image', media: 'static/media', - server: 'server', }; ``` @@ -53,7 +51,6 @@ const defaultDistPath = { - `wasm`:表示 WebAssembly 文件的输出目录。 - `image`:表示非 SVG 图片的输出目录。 - `media`:表示视频等媒体资源的输出目录。 -- `server`: 表示服务端产物的输出目录,仅在 [target](/config/output/targets) 为 `node` 时有效。 ### 根目录 diff --git a/website/docs/zh/guide/basic/output-files.mdx b/website/docs/zh/guide/basic/output-files.mdx index 24f02b20b5..50869c2749 100644 --- a/website/docs/zh/guide/basic/output-files.mdx +++ b/website/docs/zh/guide/basic/output-files.mdx @@ -106,15 +106,25 @@ dist Node.js 产物通常只包含 JS 文件,不包含 HTML、CSS 等文件。此外,Node 产物的 JS 文件名称也不会自动生成哈希值。 -你可以通过 [output.distPath.server](/config/output/dist-path) 配置项来修改 Node 产物的输出路径。 +你可以通过 [environments](/config/environments) 配置项来修改 Node 产物的输出路径。 -比如,将 Node.js 产物输出到 `server` 目录: +比如,将 Node.js 产物输出到 `dist/server` 目录: ```ts export default { - output: { - distPath: { - server: 'server', + environments: { + web: { + output: { + target: 'web', + }, + }, + node: { + output: { + target: 'node', + distPath: { + root: 'dist/server', + }, + }, }, }, }; From b89524c79a55bd545d7cfdda530bbdb7f2b8df32 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 25 Jun 2024 18:45:09 +0800 Subject: [PATCH 2/2] fix: e2e --- .../print-file-size/rsbuild.config.ts | 21 ------------------- .../rsbuild.config.ts | 3 +++ .../source/alias-by-target/rsbuild.config.ts | 3 +++ .../source/multiple-entry/rsbuild.config.ts | 3 +++ 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/e2e/cases/performance/print-file-size/rsbuild.config.ts b/e2e/cases/performance/print-file-size/rsbuild.config.ts index ce96b50ab8..d8df500358 100644 --- a/e2e/cases/performance/print-file-size/rsbuild.config.ts +++ b/e2e/cases/performance/print-file-size/rsbuild.config.ts @@ -2,25 +2,4 @@ import { pluginReact } from '@rsbuild/plugin-react'; export default { plugins: [pluginReact()], - output: { - filenameHash: false, - }, - performance: { - printFileSize: true, - }, - environments: { - web: { - output: { - target: 'web', - }, - }, - node: { - output: { - target: 'node', - distPath: { - root: 'dist/server', - }, - }, - }, - }, }; diff --git a/e2e/cases/plugin-api/plugin-transform-by-targets/rsbuild.config.ts b/e2e/cases/plugin-api/plugin-transform-by-targets/rsbuild.config.ts index 0e033c3457..421641c807 100644 --- a/e2e/cases/plugin-api/plugin-transform-by-targets/rsbuild.config.ts +++ b/e2e/cases/plugin-api/plugin-transform-by-targets/rsbuild.config.ts @@ -11,6 +11,9 @@ export default { node: { output: { target: 'node', + distPath: { + root: 'dist/server', + }, }, }, }, diff --git a/e2e/cases/source/alias-by-target/rsbuild.config.ts b/e2e/cases/source/alias-by-target/rsbuild.config.ts index a88da75fbb..3baa6fb0c0 100644 --- a/e2e/cases/source/alias-by-target/rsbuild.config.ts +++ b/e2e/cases/source/alias-by-target/rsbuild.config.ts @@ -23,6 +23,9 @@ export default defineConfig({ }, output: { target: 'node', + distPath: { + root: 'dist/server', + }, }, }, }, diff --git a/e2e/cases/source/multiple-entry/rsbuild.config.ts b/e2e/cases/source/multiple-entry/rsbuild.config.ts index 7ec315eea6..907b2dccda 100644 --- a/e2e/cases/source/multiple-entry/rsbuild.config.ts +++ b/e2e/cases/source/multiple-entry/rsbuild.config.ts @@ -27,6 +27,9 @@ export default defineConfig({ node: { output: { target: 'node', + distPath: { + root: 'dist/server', + }, }, }, },