Skip to content

refactor: proper macro browser shim#2433

Merged
andrii-bodnar merged 7 commits intonextfrom
macro-browser-shim
Feb 5, 2026
Merged

refactor: proper macro browser shim#2433
andrii-bodnar merged 7 commits intonextfrom
macro-browser-shim

Conversation

@timofei-iatsenko
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko commented Feb 4, 2026

Description

Previously, the Vite plugin contained hacks to prevent macro implementation code from being pulled into the bundle. These hacks were also used to detect and warn when a macro was used incorrectly (eq. when the macro plugin was not configured).

In this PR, I replaced that approach with a more robust, bundler-agnostic solution for detecting incorrect macro usage. Instead of performing this check inside the Vite plugin, the logic is now implemented using package exports conditions:

  • A special condition that resolves to a browser shim
  • A default condition that resolves to the code required by the babel macro plugin

This allows us to surface a clear warning when macros are used without proper setup, without relying on bundler-specific behavior.

As a result, the solution should work consistently across modern bundlers (Vite, Webpack, Rspack, esbuild, etc.), rather than being limited to Vite only.

Sidechange

I also found few more places where absolute version left instead of workspace:* protocol.

ESM Migration

This change deleted the last place in the babel-plugin-lingui-macro used CJS syntax, and i was able to switch package to ESM only.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Feb 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
js-lingui Ready Ready Preview Feb 5, 2026 7:50am

Request Review

Comment on lines +39 to +42
"./macro": {
"browser": "./macro/browser.cjs",
"types": "./macro/index.d.cts",
"default": "./macro/index.cjs"
Copy link
Collaborator Author

@timofei-iatsenko timofei-iatsenko Feb 4, 2026

Choose a reason for hiding this comment

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

since there are two diffrent implementations under conditions we have to sepcify type explicitly, otherwise typescript is confused

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

size-limit report 📦

Path Size
packages/core/dist/index.mjs 1.77 KB (0%)
packages/detect-locale/dist/index.mjs 618 B (0%)
packages/react/dist/index.mjs 1.21 KB (0%)

Comment on lines -62 to -69
"peerDependencies": {
"babel-plugin-macros": "2 || 3"
},
"peerDependenciesMeta": {
"babel-plugin-macros": {
"optional": true
}
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i moved macro creation from this package to upstream core/react, they also defining the babel-plugin-macros as peer

Comment on lines +134 to +137
t: {
(descriptor: MacroMessageDescriptor): string
(literals: TemplateStringsArray, ...placeholders: any[]): string
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I found that _t function type declaration was public (available for import from the package) which is not correct as it's a helper type for useLingui, i changed the definition so it's no longer exported

@andrii-bodnar andrii-bodnar added this to the v6 milestone Feb 5, 2026
@andrii-bodnar andrii-bodnar merged commit 22ab471 into next Feb 5, 2026
7 checks passed
@andrii-bodnar andrii-bodnar deleted the macro-browser-shim branch February 5, 2026 15:11
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.

2 participants