web-infra-dev / rslib

Create JavaScript libraries in a simple and intuitive way.
https://lib.rsbuild.dev/
MIT License
479 stars 26 forks source link

[Bug]: performance.chunkSplit is not working for Node.js libraries #354

Closed xc2 closed 1 day ago

xc2 commented 4 weeks ago

Version

  System:
    OS: macOS 15.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 142.00 MB / 32.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Browsers:
    Chrome: 130.0.6723.70
    Safari: 18.1
  npmPackages:
    @rslib/core: ^0.0.15 => 0.0.15

Details

rslib config

rslib.config.ts ```ts import { defineConfig } from "@rslib/core"; export default defineConfig({ output: { cleanDistPath: true }, lib: [ { format: "cjs", syntax: "es2021", dts: false, autoExternal: false, autoExtension: false, source: { entry: { bun: "./binary/bun.ts", node: "./binary/node.ts", }, }, performance: { chunkSplit: { strategy: "custom", splitChunks: { cacheGroups: { sources: { test: /./, name: "sources", enforce: true, chunks: "all", reuseExistingChunk: true, }, }, }, }, }, output: { externals: { url: "node:url", buffer: "node:buffer", events: "node:events", }, minify: true, cleanDistPath: false, target: "node", distPath: { root: "releases/binary", jsAsync: "internal", }, }, }, ], }); ```

Inspect

Only chunks: 'async' is in rspack config

// rspack.config.mjs optimization.splitChunks
{ chunks: 'async' }

Reproduce link

-

Reproduce Steps

-

fi3ework commented 4 weeks ago

Related to output: node. The related code is https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/plugins/splitChunks.ts#L224 which directly return. It's documented here

https://rsbuild.dev/config/performance/chunk-split#:~:text=When%20the%20Rsbuild%20target%20is%20%22node%22%2C%20since%20Node%20Bundles%20do%20not%20need%20to%20be%20split%20to%20optimize%20loading%20performance%2C%20the%20chunkSplit%20rule%20will%20not%20take%20effect.

When the Rsbuild target is "node", since Node Bundles do not need to be split to optimize loading performance, the chunkSplit rule will not take effect.

This makes sense, but it also prevents splitting chunks by config.performance.chunkSplit and can use tools.rspack.optimization.splitChunks only. I think maybe strategy: "costom" could be applied to the node or webworker type? cc @chenjiahan

chenjiahan commented 4 weeks ago

Yes we should allow to manually configuring split chunks for the node target, let me think about how to allow it..

chenjiahan commented 4 weeks ago

For the node target, I think only splitting async chunks is allowed. If we split some initial chunks, HTML plugin can reload all of them, but Node package consumers can only import an entry module rather than import all initial modules.

For the web worker target, as far as I know, only importScripts is allowed to import other scripts, so chunk splitting never seems to work in the worker thread.

chenjiahan commented 1 day ago

I have added documentation the clarify the current behavior: https://github.com/web-infra-dev/rsbuild/pull/4088

This means that when output.target is node or web-worker, Rsbuild's performance.chunkSplit will not take effect. If we change this default behavior, this may have unpredictable impacts on existing SSR projects.

Using tools.rspack should work if users need to split Node bundles:

export default {
  tools: {
    rspack: {
      optimization: {
        splitChunks: {
          // options
        },
      },
    },
  },
};