rslib icon indicating copy to clipboard operation
rslib copied to clipboard

[Feature]: provide option to stop injecting __webpack_exports__ and __WEBPACK_EXTERNAL_MODULE_ variable prefix

Open tats-u opened this issue 9 months ago • 8 comments

What problem does this feature solve?

Both variable prefixes __webpack_exports__ and __WEBPACK_EXTERNAL_MODULE_, as long as the Great Wall or Yangtze, reduce the readability and debuggability of output .js files.

import * as __WEBPACK_EXTERNAL_MODULE_micromark_extension_cjk_friendly_1c72e10b__ from "micromark-extension-cjk-friendly";

function remarkCjkFriendly() {

    const data = this.data();

    const micromarkExtensions = data.micromarkExtensions || (data.micromarkExtensions = []);

    micromarkExtensions.push((0, __WEBPACK_EXTERNAL_MODULE_micromark_extension_cjk_friendly_1c72e10b__.cjkFriendlyExtension)());

}

export { remarkCjkFriendly as default };

If you want to keep the output readable, this specification is a barrier.

What does the proposed API look like?

A boolean property of a object passed to an argument of defineConfig in rslib.config.ts.

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

export default defineConfig({
  lib: [
    {
      format: "esm",
      syntax: "es2024",
      dts: true,
      bundle: false,
      // ***HERE***
    },
  ],
});

By this option, the output will be much more readable:

import { cjkFriendlyExtension } from "micromark-extension-cjk-friendly";

function remarkCjkFriendly() {

    const data = this.data();

    const micromarkExtensions = data.micromarkExtensions || (data.micromarkExtensions = []);

    micromarkExtensions.push((0, cjkFriendlyExtension)());

}

export { remarkCjkFriendly as default };

tats-u avatar Mar 22 '25 15:03 tats-u

  • __WEBPACK_EXTERNAL_MODULE_ is a known issue that will break the readability of the output files, also see _WEBPACK_EXTERNAL_MODULE.
  • Could you specify the issue with __webpack_exports__? I think it's about the CJS format?

fi3ework avatar Mar 24 '25 03:03 fi3ework

@fi3ework

Could you specify the issue with webpack_exports? I think it's about the CJS format?

It appears when you re-export other ESMs with bundle: false.

https://github.com/tats-u/rslib-var-prefix

export { checkCjk } from "./body.js";

import * as __WEBPACK_EXTERNAL_MODULE__body_js_551ed84c__ from "./body.js";
var __webpack_exports__check = __WEBPACK_EXTERNAL_MODULE__body_js_551ed84c__.check;
export { __webpack_exports__check as check };

tats-u avatar Mar 24 '25 12:03 tats-u

I should have reported this as a bug.

tats-u avatar Mar 24 '25 12:03 tats-u

Well, technically it's not a bug as even in this format it keeps the same semantic as the expected one, and tree shaking also performs normally.

But it's terrible for readability.

fi3ework avatar Mar 24 '25 14:03 fi3ework

I see. For Rslib, I think this should be rather the default behavior. Due to this, I migrated from Rslib to tsup in https://github.com/tats-u/markdown-cjk-friendly for a while.

tats-u avatar Mar 24 '25 14:03 tats-u

The output style is defined by webpack / Rspack internal mechanism, but we should tweak it for Rslib.

fi3ework avatar Mar 24 '25 14:03 fi3ework

Is it exposed to Rspack users (including Rslib)? (Which court is the ball in now, Rspack or Rslib?)

tats-u avatar Mar 24 '25 23:03 tats-u

Will be changed in Rspack side.

fi3ework avatar Mar 25 '25 03:03 fi3ework