gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
161 stars 37 forks source link

Plugin crashes when using returnObjects option #117

Closed oaks-view closed 4 years ago

oaks-view commented 4 years ago

I use the plugin in a nextjs project. When i build sometimes it fails with a compilation error like so `Failed to compile.

./pages/index.js Error: /Users/mozenge/Desktop/work/movinga/explorations/helloNnext/pages/index.js:

Build error occurred Error: > Build failed because of webpack errors at build (/Users/mozenge/Desktop/explorations/helloNext/node_modules/next/dist/build/index.js:9:900)

After commenting and uncommenting lines of code I was able to pinpoint failure points, i.e places in the code that caused the compilation errors. I created a sample project with config to test it out easily here: sample repo.

In the pages/index.js build / compilation breaks at line 6 and line 11. Line 6: const marks = Object.values(t('journey.step.area.marks', { returnObjects: true, defaultValue: {} })).map(mark => ({ ...mark, value: Number.parseInt(mark.value, 10), })); Line 11: const min = Number.parseInt(t('journey.step.years-living.min', { defaultValue: 2 }), 10); Running the command npm run build in the sample repo. can show this behavior. So when the lines 6 and 11 are commented out Build doesnt fail.

I couldnt find in the docs where codes like the above are flagged as edge cases.

gilbsgilbs commented 4 years ago

Thanks for reporting this issue. Can you provide a complete error log and try building your reproduction repo from scratch? When I try to build your reproduction project, I get the following error which is unrelated to the plugin:

Creating an optimized production build  

Failed to compile.

./pages/index.js
SyntaxError: /tmp/next-i18next-scanner-test/pages/index.js: Spread children are not supported in React.
  13 |   return (<>
  14 |     <h1>{t('home-h1')}</h1>
> 15 |     <h2>Marks: {...marks}</h2>
     |                ^^^^^^^^^^
  16 |   <h2>Min: {min}</h2>
  17 |   </>
  18 |   );

> Build error occurred
Error: > Build failed because of webpack errors
    at build (/tmp/next-i18next-scanner-test/node_modules/next/dist/build/index.js:9:900)

Also, could you try to change the defaultValue to be a string instead of an integer?

oaks-view commented 4 years ago

Hi. i just updateed the project to to remove the error you pasted(sorry). Now its just the compilation error i reported. For the default value i changed from integer to string also. Find the complete log below

1 verbose cli [ '/Users/mozenge/.nvm/versions/node/v10.16.0/bin/node',
1 verbose cli   '/Users/mozenge/.nvm/versions/node/v10.16.0/bin/npm',
1 verbose cli   'run',
1 verbose cli   'build' ]
2 info using npm@6.9.0
3 info using node@v10.16.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle hello-next@1.0.0~prebuild: hello-next@1.0.0
6 info lifecycle hello-next@1.0.0~build: hello-next@1.0.0
7 verbose lifecycle hello-next@1.0.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle hello-next@1.0.0~build: PATH: /Users/mozenge/.nvm/versions/node/v10.16.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/mozenge/Desktop/work/movinga/explorations/helloNnext/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/mozenge/.nvm/versions/node/v10.16.0/bin:/Users/mozenge/Desktop/services/mongodb/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Users/mozenge/Library/Android/sdk/tools:/Users/mozenge/Library/Android/sdk/platform-tools:/Users/mozenge/Desktop/services/sonar-scanner/bin:/Users/mozenge/Desktop/services/sonarqube-7.2.1/bin/macosx-universal-64:/Users/mozenge/Desktop/go:/Users/mozenge/Desktop/go/bin:/Users/mozenge/Desktop/services/java/apache-maven-3.5.4/bin:/usr/local/opt/rabbitmq/sbin
9 verbose lifecycle hello-next@1.0.0~build: CWD: /Users/mozenge/Desktop/work/movinga/explorations/helloNnext
10 silly lifecycle hello-next@1.0.0~build: Args: [ '-c', 'rm -rf .next && next build' ]
11 silly lifecycle hello-next@1.0.0~build: Returned: code: 1  signal: null
12 info lifecycle hello-next@1.0.0~build: Failed to exec build script
13 verbose stack Error: hello-next@1.0.0 build: `rm -rf .next && next build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/Users/mozenge/.nvm/versions/node/v10.16.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:198:13)
13 verbose stack     at ChildProcess.<anonymous> (/Users/mozenge/.nvm/versions/node/v10.16.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:198:13)
13 verbose stack     at maybeClose (internal/child_process.js:982:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid hello-next@1.0.0
15 verbose cwd /Users/mozenge/Desktop/work/movinga/explorations/helloNnext
16 verbose Darwin 18.7.0
17 verbose argv "/Users/mozenge/.nvm/versions/node/v10.16.0/bin/node" "/Users/mozenge/.nvm/versions/node/v10.16.0/bin/npm" "run" "build"
18 verbose node v10.16.0
19 verbose npm  v6.9.0
20 error code ELIFECYCLE
21 error errno 1
22 error hello-next@1.0.0 build: `rm -rf .next && next build`
22 error Exit status 1
23 error Failed at the hello-next@1.0.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Please note that building the project right now will fail because of the issue at hand. But like i mentioned if we comment out those codes or disable the plugin the build will work successfully.

gilbsgilbs commented 4 years ago

Ok, the issue seems to be with the returnObjects option (it is unsupported right now). The first extraction works, but further extractions make the exporter crash. I think we need to be more permissive with defaultValue and allow any value.

oaks-view commented 4 years ago

Hello @gilbsgilbs any hope for a fix on this soon?

gilbsgilbs commented 4 years ago

Sorry, I'm kind of busy now, but you can tackle it if you want. I don't think it's too hard. It's probably just about removing some constraints.

oaks-view commented 4 years ago

Hi @gilbsgilbs i created a pr here PTAL.