Skip to content

feat: Fix interop with cjs-module-lexer - #2377

Open
benasher44 wants to merge 1 commit into
validatorjs:masterfrom
benasher44:basher/babel-cjs
Open

benasher44 wants to merge 1 commit into
validatorjs:masterfrom
benasher44:basher/babel-cjs

Conversation

@benasher44

@benasher44 benasher44 commented Feb 28, 2024 •

Copy link
Copy Markdown

This moves the main validator exports to validator-main.js (avoid conflict with top-level validator.js when running build), which then allows both a default and named exports in the index by re-exporting. By doing this, we get a CJS validator that also can be name-imported in node ESM.

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)
  • References provided in PR (where applicable)

@benasher44
benasher44 force-pushed the basher/babel-cjs branch 2 times, most recently from 951c722 to 23c58ad Compare February 28, 2024 05:23
@benasher44

Copy link
Copy Markdown
Author

Well I got pretty far just with this small change, but now I can't figure out how to get the AMD test to pass… any pointers there?

@benasher44

Copy link
Copy Markdown
Author

I also tried without adding a file to re-export from + just copy pasting all of the exports in the default export as named exports, but that has the same issue (seemingly) breaking AMD.

@WikiRik

WikiRik commented Mar 1, 2024

Copy link
Copy Markdown
Member

When building babel does give a warning;

Using named and default exports together. Consumers of your bundle will have to use validator['default'] to access the default export, which may not be what you want. Use `exports: 'named'` to disable this warning

That's also what seems to be changed in the built files. Apart from the expected additional exports it also returned validator before but now that's part of exports['default']. Unfortunately I have no experience with AMD to help out further

@benasher44

Copy link
Copy Markdown
Author

No problem. Thanks anyway! Yeah that's what I discovered as well. I did all kinds of inspection there and couldn't figure out a different way to get to the original module object shape it was expecting before :(

@WikiRik

WikiRik commented Mar 1, 2024

Copy link
Copy Markdown
Member

Might just be a Babel thing. I know there is an outdated PR from one of the maintainers that tried to overhaul the config (also because we're on old versions of Babel and Rollup); #1869

Maybe a smaller version of that is the way to go, but merging PRs is often quite slow with the current maintainers, see #2376

@benasher44

Copy link
Copy Markdown
Author

It could be, but in my local build, I tried with the latest version of Babel 8, which didn't change anything.

@profnandaa

Copy link
Copy Markdown
Member

I'll take a look at this while on my dev machine. But this is not a disruptive change like what I've seen in the past. Happy to see this land once all t's are crossed.

@benasher44

Copy link
Copy Markdown
Author

@profnandaa any chance you'll have time to look soon?

@kojang123

Copy link
Copy Markdown

Bisakah aplikasi crome itu di perketat suapaya kalaw saya main slot engine tidak kalah tolong dibantu

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ff3424) to head (f760902).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2377   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       115    +1     
  Lines         2599      2598    -1     
  Branches       658       659    +1     
=========================================
- Hits          2599      2598    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@benasher44
benasher44 marked this pull request as ready for review October 7, 2026 02:07
Split src/index.js into a thin re-export over a new src/validator-main.js
so the compiled CommonJS keeps statically analyzable named exports. Node
uses cjs-module-lexer to expose named ESM imports from CJS modules; the
previous single-file default-export build hid every export behind a
dynamically assembled object, so `import { isEmail } from 'validator'`
failed while `import validator from 'validator'` worked.

- src/validator-main.js holds the full module body as named exports,
  byte-identical to master's index.js modulo the export wrapper
- src/index.js re-exports it and re-exports the namespace as default
- validator-main.js added to package files, .gitignore, and the CI
  artifact upload (test jobs download build artifacts)
- build-browser.mjs bundles from a default-only virtual entry, keeping
  the browser bundle's UMD surface (window.validator, AMD, CJS) exactly
  as before
- test/exports.test.js asserts every runtime export is statically
  detectable by cjs-module-lexer, following index.js's re-exports; the
  test registers as skipped where the lexer cannot load (Node 8)
@benasher44

Copy link
Copy Markdown
Author

@profnandaa @WikiRik finally got this working in a backward compatible / sustainable way. mind taking another look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants