Skip to content

Commit

Permalink
fix: emit assets to server dist folder correctly (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Mar 8, 2024
1 parent 1b78a96 commit 5be128b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 20 deletions.
5 changes: 0 additions & 5 deletions e2e/cases/node-addons/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { build } from '@e2e/helper';
test('should compile Node addons correctly', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
output: {
targets: ['node'],
},
},
});
const files = await rsbuild.unwrapOutputJSON();
const addonFile = Object.keys(files).find((file) => file.endsWith('a.node'));
Expand Down
5 changes: 5 additions & 0 deletions e2e/cases/node-addons/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
output: {
targets: ['node'],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -1421,11 +1421,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"splitChunks": false,
},
"output": {
"chunkFilename": "server/[name].js",
"filename": "server/[name].js",
"chunkFilename": "[name].js",
"filename": "[name].js",
"hashFunction": "xxhash64",
"libraryTarget": "commonjs2",
"path": "<ROOT>/packages/compat/webpack/tests/dist",
"path": "<ROOT>/packages/compat/webpack/tests/dist/server",
"pathinfo": false,
"publicPath": "/",
"webassemblyModuleFilename": "static/wasm/[hash].module.wasm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
exports[`plugin-output > should allow to custom server directory with distPath.server 1`] = `
{
"output": {
"chunkFilename": "server/[name].js",
"filename": "server/[name].js",
"chunkFilename": "[name].js",
"filename": "[name].js",
"hashFunction": "xxhash64",
"libraryTarget": "commonjs2",
"path": "<ROOT>/packages/compat/webpack/tests/dist",
"path": "<ROOT>/packages/compat/webpack/tests/dist/server",
"pathinfo": false,
"publicPath": "/",
},
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1848,13 +1848,13 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"splitChunks": false,
},
"output": {
"chunkFilename": "server/[name].js",
"chunkFilename": "[name].js",
"cssChunkFilename": "static/css/async/[name].css",
"cssFilename": "static/css/[name].css",
"filename": "server/[name].js",
"filename": "[name].js",
"hashFunction": "xxhash64",
"libraryTarget": "commonjs2",
"path": "<ROOT>/packages/core/tests/dist",
"path": "<ROOT>/packages/core/tests/dist/server",
"pathinfo": false,
"publicPath": "/",
"webassemblyModuleFilename": "static/wasm/[hash].module.wasm",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/__snapshots__/output.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
exports[`plugin-output > should allow to custom server directory with distPath.server 1`] = `
{
"output": {
"chunkFilename": "server/[name].js",
"chunkFilename": "[name].js",
"cssChunkFilename": "static/css/async/[name].css",
"cssFilename": "static/css/[name].css",
"filename": "server/[name].js",
"filename": "[name].js",
"hashFunction": "xxhash64",
"libraryTarget": "commonjs2",
"path": "<ROOT>/packages/core/tests/dist",
"path": "<ROOT>/packages/core/tests/dist/server",
"pathinfo": false,
"publicPath": "/",
},
Expand Down
1 change: 1 addition & 0 deletions packages/document/docs/en/config/output/targets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Refers to the build target running in the Node.js environment, usually used in s

When `target` is set to `'node'`, Rsbuild will:

- Output the build files to the `dist/server` directory, which can be configured via [output.distPath](/config/output/dist-path).
- No HTML files will be generated, and HTML-related logic will not be executed, since HTML is not required by the Node.js environment.
- CSS code will not be bundled or extracted, but the id information of CSS Modules will be included in the bundle.
- The default code split strategy will be disabled, but dynamic import can still work.
Expand Down
1 change: 1 addition & 0 deletions packages/document/docs/zh/config/output/targets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default {

`target` 设置为 `'node'` 时,Rsbuild 会进行以下处理:

- 将构建产物输出到 `dist/server` 目录下,可通过 [output.distPath](/config/output/dist-path) 配置。
- 不会生成 HTML 文件,与 HTML 相关的逻辑也不会执行,因为 Node.js 环境不需要 HTML。
- 不会打包或抽取 CSS 代码,但产物中会包含 CSS Modules 的 id 信息。
- 不会开启默认的拆包策略,但 dynamic import 依然可以生效。
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ export function applyOutputPlugin(api: RsbuildPluginAPI) {

if (isServer) {
const serverPath = getDistPath(config, 'server');
const filename = posix.join(serverPath, '[name].js');

chain.output
.filename(filename)
.chunkFilename(filename)
.path(posix.join(api.context.distPath, serverPath))
.filename('[name].js')
.chunkFilename('[name].js')
.libraryTarget('commonjs2');
}

Expand Down

0 comments on commit 5be128b

Please sign in to comment.