web-infra-dev / rslib

The Rsbuild powered package build tool.
MIT License
307 stars 16 forks source link

[Bug]: ESM format output is incorrect & auto detect entry file fails #162

Closed ShaneYu closed 2 weeks ago

ShaneYu commented 2 weeks ago

Version

System:
    OS: Windows 11 10.0.22631
    CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
    Memory: 4.11 GB / 31.21 GB
  Browsers:
    Edge: Chromium (127.0.2651.86)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @rslib/core: ^0.0.2 => 0.0.2

Details

There are currently two issues I am facing with Rslib at this time.

Rsbuild is meant to be able to automatically find the entry point via src/index.ts and a load of other file extensions, according to the error I get. Issue is that when using Rslib without specifying a source.entry in the config, it cannot locate the entry src/index.ts file that I have.

This results in the following error:

error   Failed to build.
error   Could not find any entry module, please make sure that src/index.(ts|js|tsx|jsx|mjs|cjs) exists,
        or customize entry through the source.entry configuration.

Entry config added to make it work:

source: {
  entry: {
    index: './src/index.ts',
  },
},

Adding in the source.entry config resolves the above issue, then the second issue is that the ESM format output does not look correct. It only outputs *.d.mts file and no actual *.mjs files are created; this means that only type definitions exist and no actual code. Whereas the CJS format is correctly output *.js code files and also the *.d.ts type definition files.

image

Reproduce link

https://github.com/ShaneYu/rslib-esm-issue

Reproduce Steps

To replicate these issues I have created a temp GitHub repository that can be cloned down and played with.

For the second issue regarding ESM format output being incorrect:

  1. Clone the repository with git clone git@github.com:ShaneYu/rslib-esm-issue.git
  2. Install the dependencies with pnpm install
  3. Run a build with pnpm build

Now go and look at the output files under ./dist/esm you will notice that no *.mjs code files have been created, however the *.mts type definition files have been. The CJS output on the other hand, this has the expected code files and type definitions.

image

For the first issue regarding the entry point configuration:

Skip steps 1 and 2 if done previously.

  1. Clone the repository with git clone git@github.com:ShaneYu/rslib-esm-issue.git

  2. Install the dependencies with pnpm install

  3. Edit the rslib.config.ts file to remove the source.entry config from both ESM and CJS lib configs

    source: {
      entry: {
        index: './src/index.ts',
      },
    },
  4. Run a build with pnpm build

Notice that despite having a src/index.ts file, it results in an error and states that the entry file cannot be found.

image

Timeless0911 commented 2 weeks ago

Can you upgrade @rslib/core to 0.0.4, I think the two issues have been solved.

ShaneYu commented 2 weeks ago

@Timeless0911 Thank you, I can confirm that upgrading to 0.0.4 has indeed resolved both issues. 😁

❤️ Thank you! 😄 Loving rslib so much! 😉