lingui / js-lingui

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

Issue with babel configuration and babel macros plugin #1012

Closed JSteunou closed 3 years ago

JSteunou commented 3 years ago

Describe the bug

Using webpack to bundle, I have a babel config for build and I though to be able to use the same configuration for linguijs extraction configuration. But there is an issue with babel macros plugin loaded twice and triggering a bunch of error with lingui cli extract. I had to make one config for lingui without macros, and one for webpack with macros.

To Reproduce

Step by step new project following documentation

Expected behavior

babel plugin macros should be added by user in its config and not automatically added by lingui with cli extract so there is no double, no errors

Additional context Add any other context about the problem here.

{
        plugins: [
            '@babel/plugin-syntax-dynamic-import',
            '@babel/plugin-proposal-class-properties',
            '@babel/plugin-proposal-nullish-coalescing-operator',
            '@babel/plugin-proposal-optional-chaining',
        ],
        presets: [
            [
                '@babel/preset-env',
                {
                    useBuiltIns: 'usage',
                    corejs: '3.9',
                },
            ],
            [
                '@babel/preset-react',
                {
                    runtime: 'automatic',
                },
            ],
            ['@babel/preset-typescript'],
        ],
    }
semoal commented 3 years ago

That's strange. In my company projects we're using the same babel config for lingui & bundling. Could you provide us a minimal repo, so we can debug?

JSteunou commented 3 years ago

We are using yarn (v1) workspace, with lingui on one workspace only for now, maybe that's very specific to our setup.

I will try to provide you a minimal repo as soon as I can.

JSteunou commented 3 years ago

In the meantime for those having the same issue, 2 workarounds:

  1. one config for webpack, one for linguijs, the 1st extending the 2nd to add babel plugin macros
  2. or naming the instance to avoid collision with the one already loaded ['macros', {}, 'the-name-you-want'] and keeping only one config.

@semoal I cannot help to think the root of the issue could be here https://github.com/lingui/js-lingui/blob/main/packages/cli/src/api/extractors/babel.ts#L33

no time to make a minimal repo today sorry :(