rslib icon indicating copy to clipboard operation
rslib copied to clipboard

[Bug]: Wrong bundling of `asset/source` in `cjs` format

Open Shamilik opened this issue 6 months ago • 6 comments

Version

@rslib/[email protected]

Details

Config almost default

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

export default defineConfig({
  lib: [
    {
      format: 'esm',
      syntax: ['node 18'],
      dts: true,
      bundle: false,
    },
    {
      format: 'cjs',
      syntax: ['node 18'],
      bundle: false,
    },
  ],
  tools: {
    rspack(config, { addRules }) {
      addRules([
        {
          test: /\.html$/i,
          type: 'asset/source',
        },
      ]);
    },
  },
});

Result code of test.html with comments

"use strict";
var __webpack_modules__ = {
    "./src/html/test.html": function(module) {
        // Exports without `default`
        module.exports = "<html>\n  <body>\n    <h1>Hello World</h1>\n  </body>\n</html>\n";
    }
};
var __webpack_module_cache__ = {};
function __webpack_require__(moduleId) {
    var cachedModule = __webpack_module_cache__[moduleId];
    if (void 0 !== cachedModule) return cachedModule.exports;
    var module = __webpack_module_cache__[moduleId] = {
        exports: {}
    };
    __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
    return module.exports;
}
var __webpack_exports__ = __webpack_require__("./src/html/test.html");
exports["default"] = __webpack_exports__["default"]; // undefined
// iterating over string results in `{ 0: '<', 1: 'h', 2: 't', ... }`
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "default"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});

As result it exports something like

{ 0: '<', 1: 'h', 2: 't', ..., default: undefined }

I'm unsure is it rslib issue or rspack or maybe wrong configuration 🤔

Expected result (updated)

import testHtml from './html/test.html';

console.log(testHtml); // `<html ...`

Like https://webpack.js.org/guides/asset-modules/#source-assets

Reproduce link

https://github.com/Shamilik/rslib-html

Reproduce Steps

  1. npm run build
  2. node dist/index.cjs

Shamilik avatar Jul 04 '25 11:07 Shamilik

interesting, how you expect html file after being processed?

fi3ework avatar Jul 04 '25 12:07 fi3ework

@fi3ework Hello. I expect it to be a raw string like

import testHtml from './html/test.html';

console.log(testHtml); // `<html ...`

https://webpack.js.org/guides/asset-modules/#source-assets

Shamilik avatar Jul 04 '25 12:07 Shamilik

And seems like its not working only for cjs

➜  rslib-html git:(main) node dist/index.cjs                              
undefined
➜  rslib-html git:(main) node dist/index.js 
<html>
  <body>
    <h1>Hello World</h1>
  </body>
</html>

➜  rslib-html git:(main) 

Shamilik avatar Jul 04 '25 12:07 Shamilik

Rslib use commonjs-static library type for CJS format, you can set it to commonjs2 instead, see https://rspack.rs/config/output#type-commonjs2.

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

export default defineConfig({
  lib: [
    {
      format: "cjs",
      syntax: ["node 18"],
      bundle: false,
      tools: {
        rspack: {
          output: {
            library: {
              type: "commonjs2",
            },
          },
        },
      },
    },
  ],
});

Timeless0911 avatar Jul 07 '25 12:07 Timeless0911

That should be a bug of commonjs-static.

fi3ework avatar Jul 07 '25 13:07 fi3ework

Related PR: https://github.com/web-infra-dev/rspack/pull/8724

Related code:

  • https://github.com/web-infra-dev/rspack/blob/6ec3c3170369f8a6b32ab8dfed8b21e0e4375ae9/crates/rspack_plugin_asset/src/asset_exports_dependency.rs#L27-L38
  • https://github.com/web-infra-dev/rspack/blob/6ec3c3170369f8a6b32ab8dfed8b21e0e4375ae9/crates/rspack_plugin_library/src/assign_library_plugin.rs#L270-L280
  • https://github.com/web-infra-dev/rspack/blob/6ec3c3170369f8a6b32ab8dfed8b21e0e4375ae9/crates/rspack_plugin_asset/src/lib.rs#L677-L683

For asset module, https://github.com/web-infra-dev/rspack/pull/8724 add an extra default exports info, and commonjs-static get this exports info to generate code like exports["default"] = __webpack_exports__["default"]; but generated module does not have default export which cause this issue.

Timeless0911 avatar Jul 08 '25 08:07 Timeless0911