formatjs icon indicating copy to clipboard operation
formatjs copied to clipboard

Import attributes break message exporting

Open brawaru opened this issue 1 year ago • 4 comments

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

  1. Create file b.mts with contents like:
export function myMacro() {
  return { id: 'woo' }
}
  1. Create file a.mts with 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()
  1. 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

brawaru avatar Aug 07 '24 13:08 brawaru

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

evert avatar Aug 08 '24 03:08 evert

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.

brawaru avatar Aug 08 '24 10:08 brawaru

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.

github-actions[bot] avatar Sep 08 '24 02:09 github-actions[bot]

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 :(

evert avatar Sep 08 '24 02:09 evert

@brawaru I can't repro from the link you posted using the latest @formatjs/cli :-/ I've added an integration test as well

longlho avatar Nov 05 '24 04:11 longlho

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

brawaru avatar Nov 05 '24 07:11 brawaru