Import attributes break message exporting
Which package?
@formatjs/[email protected]
Describe the bug
When import attributes are used in the code, @formatjs/cli fails to extract messages despite tsc being able to successfully check the file.
The following errors are printed to console:
[@formatjs/cli] [WARN] Error: index.mts:2:43 - error TS1005: ';' expected.
2 import { doSomething } from './other.mjs' with { type: 'macro' }
~~~~
index.mts:2:48 - error TS1005: '(' expected.
2 import { doSomething } from './other.mjs' with { type: 'macro' }
~
index.mts:4:1 - error TS1005: ')' expected.
4 export const msg = defineMessage({
~~~~~~
To Reproduce
Codesandbox URL
https://stackblitz.com/edit/node-by7zrp?file=package.json
Reproducible Steps/Repo
- Create file
b.mtswith contents like:
export function myMacro() {
return { id: 'woo' }
}
- Create file
a.mtswith contents like:
import { defineMessage } from '@formatjs/intl'
import { myMacro } from './b.mts' with { type: 'macro' }
export const greeting = defineMessage({
id: 'greeting',
defaultMessage: 'Hello, {name}!',
})
export const smthnElse = myMacro()
- Try to extract messages from
a.mts
pnpm formatjs extract a.mts --out-file output.json
Expected behavior
Extraction completes without errors and yields a file output.json with contents:
{
"greeting": {
"defaultMessage": "Hello, {name}!"
}
}
Screenshots
N/A
Desktop (please complete the following information):
N/A
Smartphone (please complete the following information):
N/A
Additional context
N/D
Also just ran into this! We actually used the assert keyword instead of with, but starting Node 22 assert is dropped. Honestly kinda surprising to see a non-flagged stable feature dropped in Node.
https://github.com/nodejs/node/pull/52104
I'm downgrading to Node 20 as we speak, as formatjs extract not breaking trumps being on Node 22
I believe the solution is to upgrade TypeScript package used by @formatjs/cli-lib and then add module: ts.ModuleKind.Preserve to compilerOptions. Preserve will allow to be import-agnostic, supporting both require and import. Unfortunately, this is not something you can patch in locally, because it seems that @formatjs/cli bundles everything, including TypeScript it is using.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Thank you creating this package. As an open source maintainer myself I know it can be a thankless task.
But bots that automatically close issues before triage is pretty hostile for end users :(
@brawaru I can't repro from the link you posted using the latest @formatjs/cli :-/ I've added an integration test as well
@longlho oh, seems that it's fixed by some update, then. Though, doesn't seem that the relevant code changed at all, perhaps it's just a transitive dependency update that fixed it (TypeScript?). Either way, that's great 🥳 Thank you for looking into this! ❤️ I'll close this as complete.