Skip to content

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

@Shamilik

Description

@Shamilik

Version

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    RspackRspack related issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions