Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: change to commonjs-static library type of commonjs format #819

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

fi3ework
Copy link
Member

@fi3ework fi3ework commented Mar 5, 2025

Summary

  • New default library type for CommonJS: Rslib changes the default value of output.library.type for CommonJS from commonjs to commonjs-static. Now, individual exports will be set as properties on module.exports and the output can be statically analyzable, thus named exports are importable into ESM via Node.js. This is useful when source code is written in ESM and the output should be compatible with both CJS and ESM. (feat!: change to commonjs-static library type of commonjs format #819)

    Named exports are importable into ESM via Node.js

    • Input:
    export function doSomething() {}
    • Output:
    function doSomething() {}
    // …
    exports.doSomething = __webpack_exports__.doSomething;
    • Consumption (CommonJS):
    const { doSomething } = require('./output.cjs'); // doSomething => [Function: doSomething]
    • Consumption (ESM):
    import { doSomething } from './output.cjs'; // doSomething => [Function: doSomething]

Related Links

commonjs-static will directly skip all unprovided exports, which will lead to lose exports. We need to fix this in webpack / Rspack: add commonjs like runtime code to export dynamically as the fallback of unprovided exports.

fix: #351
fix: #714

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Sorry, something went wrong.

Copy link

netlify bot commented Mar 5, 2025

Deploy Preview for rslib ready!

Name Link
🔨 Latest commit 0c551f9
🔍 Latest deploy log https://app.netlify.com/sites/rslib/deploys/67c8523294f00400088c20f8
😎 Deploy Preview https://deploy-preview-819--rslib.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@fi3ework fi3ework force-pushed the cjs-static branch 3 times, most recently from 3860d4e to 8bbe5da Compare March 26, 2025 07:35
@fi3ework fi3ework requested a review from Timeless0911 March 26, 2025 07:56
@fi3ework fi3ework marked this pull request as ready for review March 26, 2025 07:56
@Timeless0911 Timeless0911 changed the title fix!: change to commonjs-static library type of commonjs format feat!: change to commonjs-static library type of commonjs format Mar 26, 2025
@Timeless0911
Copy link
Contributor

So much snapshot test cases related to cjs runtime tpl.

@Timeless0911 Timeless0911 merged commit f0c97fd into main Mar 26, 2025
17 checks passed
@Timeless0911 Timeless0911 deleted the cjs-static branch March 26, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants