Skip to content

Conversation

Shinigami92
Copy link
Member

@Shinigami92 Shinigami92 commented Mar 9, 2025

I just go a great idea over night! 💡
What if we just move all require(*)-cjs calls into .spec.cts files. That way we can more safely use the require-nodejs keyword and ensure that it really works.
But not only this, when we will remove cjs support in v10, we could ensure this way at least for newer versions of nodejs, that it still works with their new require(esm) https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require

@Shinigami92 Shinigami92 added this to the vAnytime milestone Mar 9, 2025
@Shinigami92 Shinigami92 self-assigned this Mar 9, 2025
@Shinigami92 Shinigami92 requested a review from a team as a code owner March 9, 2025 11:04
Copy link

netlify bot commented Mar 9, 2025

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit 84292d0
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/6861a1f82630b10008313ab2
😎 Deploy Preview https://deploy-preview-3436.fakerjs.dev
📱 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 project configuration.

Copy link

codecov bot commented Mar 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.97%. Comparing base (c7086b2) to head (84292d0).
Report is 1 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #3436   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files        2880     2880           
  Lines      220510   220510           
  Branches      952      952           
=======================================
  Hits       220457   220457           
  Misses         53       53           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Shinigami92 Shinigami92 requested a review from a team March 9, 2025 11:16
Copy link
Member

@xDivisionByZerox xDivisionByZerox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just move all require(*)-cjs calls into .spec.cts files. That way we can more safely use the require-nodejs keyword and ensure that it really works.

What do you mean by "ensure that it really works"? Why does it not work right now? 🤔

But not only this, when we will remove cjs support in v10, we could ensure this way at least for newer versions of nodejs, that it still works with their new require(esm) nodejs.org/api/modules.html#loading-ecmascript-modules-using-require

I don't see how moving the test in a separate file ensures this behavior. As far as I can tell this would require our code base to use explicit *.m(t|j)s file extensions, which is not the case right now. Am I missing something? Could you elaborate on this?

@Shinigami92
Copy link
Member Author

What if we just move all require(*)-cjs calls into .spec.cts files. That way we can more safely use the require-nodejs keyword and ensure that it really works.

What do you mean by "ensure that it really works"? Why does it not work right now? 🤔

Before this PR, we used require in a normal ts file which gets processed by vitest/vite and therefore potentially gets optimized (not sure if this really happens!)
When we now use .cts instead, we ensure that the file is running in pure common-js context.
https://github.com/faker-js/faker/pull/3436/files#diff-f8cd4ea46aecbba86ee9cba1a851bca523aca5e63bf9fa1866e3604a9d2dec26L9

const { faker } = require(`../dist/locale/${locale}.cjs`) as {

But not only this, when we will remove cjs support in v10, we could ensure this way at least for newer versions of nodejs, that it still works with their new require(esm) nodejs.org/api/modules.html#loading-ecmascript-modules-using-require

I don't see how moving the test in a separate file ensures this behavior. As far as I can tell this would require our code base to use explicit *.m(t|j)s file extensions, which is not the case right now. Am I missing something? Could you elaborate on this?

No, we wont need to ship .mjs files, because everything is controlled be package.json's "type": "module" and therefore the hole package will get consumed as pure esm package.
In combination with NodeJS's new require(esm), this finally get solved. And our .spec.cts file can cover that.

@Shinigami92
Copy link
Member Author

@xDivisionByZerox in the future (v10) we might need to run this .spec.cts test with Node v20.17+ and the --experimental-require-module or Node v23+
(see https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require and open the History)

And then we need to switch the required calls to depend on v10's esm bundled code

- require('../dist/index.cjs')
+ require('../dist/index.js')

- require(`../dist/locale/${locale}.cjs`)
+ require(`../dist/locale/${locale}.js`)

@Shinigami92 Shinigami92 requested review from xDivisionByZerox and a team April 11, 2025 17:44
@Shinigami92 Shinigami92 force-pushed the test-cjs-require branch 2 times, most recently from 1f87d91 to e1ad736 Compare April 13, 2025 11:56
@Shinigami92 Shinigami92 marked this pull request as draft June 28, 2025 09:36
@Shinigami92 Shinigami92 mentioned this pull request Jun 29, 2025
@Shinigami92
Copy link
Member Author

@xDivisionByZerox just an info, but #3540 (comment) fails with node 18
But this PR here would also still run with node 18, because it actively still tests it with native cjs support and does not fake a require inside an esm file

Do you still wont like this cts test file change?

Copy link
Member

@xDivisionByZerox xDivisionByZerox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xDivisionByZerox just an info, but #3540 (comment) fails with node 18
But this PR here would also still run with node 18, because it actively still tests it with native cjs support and does not fake a require inside an esm file

Do you still wont like this cts test file change?

That was the argument (or proof of concept) I was waiting for 👍

@Shinigami92 Shinigami92 marked this pull request as ready for review June 29, 2025 16:13
@Shinigami92 Shinigami92 requested a review from a team June 29, 2025 16:13
@xDivisionByZerox xDivisionByZerox merged commit a53d5a3 into next Jun 29, 2025
23 checks passed
@xDivisionByZerox xDivisionByZerox deleted the test-cjs-require branch June 29, 2025 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants