Skip to content

[Bug]: output.filename does not allow setting filename dynamically using a function #911

@hackwater

Description

@hackwater

Version

System:
  OS: Windows 11 10.0.26100
  CPU: (32) x64 AMD Ryzen 9 7950X3D 16-Core Processor          
  Memory: 14.20 GB / 31.18 GB
Browsers:
  Edge: Chromium (127.0.2651.74)
  Internet Explorer: 11.0.26100.1882
npmPackages:
  @rslib/core: ^0.6.2 => 0.6.2

Details

Trying to set the filename dynamically results in an error when running any rslib function. I suspect there's an issue in composeCreateRsbuildConfig when calling mergeRsbuildConfig to generate the userConfig, but then calling it again with the userConfig as the third argument. This results in the merger combining two identical functions into a single array per the logic of the Rsbuild merge function. This array is eventually passed to path.join, which expects a string (I assume the result of the filename function) and therefore fails.

I'm not sure what the right solution is, which is why I'm not trying to provide a PR; it's possible this is an issue stemming from the mergeRsbuildConfig function, but I can definitely successfully generate my config with a dynamic filename in Rsbuild.

Here's a minimal rslib.config.ts that fails when using the dynamic filename example from the Rsbuild docs:

import { defineConfig } from '@rslib/core';

export default defineConfig({
  lib: [
    {
      format: 'esm',
    },
  ],
  output: {
    filename: {
      image: (pathData) => {
        if (pathData.filename?.includes('foo')) {
          return '/foo/[name][ext]';
        }
        return '/bar/[name][ext]';
      },
    },
  },
});

The error:

> rslib inspect --verbose

  Rslib v0.6.2

error   Failed to inspect config.
error   TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Array
    at Object.join (node:path:1268:7)
    at getMergedFilename (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:2358:86)
    at createAssetRule (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:2368:35)
    at Object.handler (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:2372:17)
    at Object.callChain (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:1821:45)
    at async modifyBundlerChain (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:2249:133)
    at async generateRspackConfig (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:4656:268)
    at async Promise.all (index 0)
    at async initConfigs (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:4794:25)
    at async inspectConfig (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rsbuild/core/dist/index.js:4564:44)
    at async inspect (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rslib/core/dist/index.js:2427:5)
    at async Command.<anonymous> (file:///C:/Users/xboxl/WebstormProjects/minimal-repro/node_modules/@rslib/core/dist/index.js:2498:13)

Reproduce link

.

Reproduce Steps

  1. add a function to return a value for output.filename.image
  2. run rslib inspect

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions