lingui / js-lingui

🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
https://lingui.dev
MIT License
4.45k stars 376 forks source link

Can't import typescript extractor after updat to 3.16.0 #1348

Closed belaczek closed 1 year ago

belaczek commented 1 year ago

Describe the bug After updating lingui to version 3.16.0, the typescript extractor is no longer exposed by package.json and cannot be imported. Our project relies on it, as the default babel exporter alone has problems with experimental decorators.

To Reproduce Steps to reproduce the behavior, possibly with minimal code sample, e.g:

  1. install lingui 3.16.0
  2. provide following custom config in lingui.config.ts
  extractors: [
    require.resolve('@lingui/cli/api/extractors/typescript'),
  ] as any,

Expected behavior lingui extract scripts works with imported typescript exporter.

Additional context Add any other context about the problem here.

thekip commented 1 year ago

Would be fixed by https://github.com/lingui/js-lingui/pull/1349

@belaczek what exactly wrong with current extractor and typescript experimental decorators? I will try to take this into account in new version of extractor.

belaczek commented 1 year ago

Hi and thanks for reaching back. Our project is somehow specific and full of dependency injections and we have to use ts-loader along with babel-loader since babel alone fails on circular dependencies while ts-loader can handle it well.

When I run lingui extract with the default extractor, it fails with the following error:

Cannot process file xxx.tsx: Duplicate declaration "Trans"

I tried to solve this by playing around with our babel rules and some other tips mentioned [here](https://github.com/lingui/js-lingui/issues/952), but with not much luck. In the end, the only working solution was following:

  1. Set typescript extractor in lingui.config.ts:
extractors: [
  require.resolve('@lingui/cli/api/extractors/typescript'),
] as any,
  1. Create empty babel preset for lingui:
{
  "env": {
    "production": {
      "presets": ["@nrwl/react/babel"]
    },
    "development": {
      "presets": ["@nrwl/react/babel"],
      "plugins": ["react-refresh/babel", "macros"]
    },
    // We want an empty babel config for lingui extract and compile scripts.
    // It uses babel internally and the @nrwl/react/babel preset makes the compilation fail for some reason.
    // The lingui script already injects its own needed plugins anyway.
    "locales": {
      "presets": [],
      "plugins": []
    }
  }
}
  1. Run the extract script as BABEL_ENV=locales lingui extract --config lingui.config.ts.
thekip commented 1 year ago

Hi @belaczek we just merged into next branch https://github.com/lingui/js-lingui/pull/1367 This should resolve issues described in the message above. This is not released yet and would be included in next major v4 release. But i want to ask you to try a development build on your project and share your feedback. Did it fix the issues or not.

You need to follow contribution guide to be able to use it in your project.

Thanks in advance.

belaczek commented 1 year ago

Hi, I just tested v4 and the issue is fixed. No explicit extractor is needed now. Thank you very much for resolution 👍