aralroca / next-translate-plugin

Next-translate plugin for i18n in Next.js 🌍 - Load page translations and use them in an easy way!
MIT License
30 stars 17 forks source link

Misunderstanding devDependency documentation. #56

Closed MartinDavi closed 1 year ago

MartinDavi commented 1 year ago

Hey, I'm sorry - this isn't a bug but I don't have a twitter account to ask a question.

In the documentation it says to list this plugin as a devDependency. But because it's required in the next.config.js and we're self-hosting the next server, then it can't really be a devDependency as the next.config.js is read by the next server at runtime if I'm understanding correctly.

The reason I ask is because it installs typescript as a dependency which is coming in at 64MB. It would be great to not have to install that. For sure we need this package as part of our build step, but without it at runtime, next.js throws:

throw new Error(`Specified i18n.locales should be an Array received ${typeof i18n.locales}.\nSee more info here: https://nextjs.org/docs/messages/invalid-i18n-config`);

as it doesn't know to look for the i18n.js when running next start.

Please help correct my misunderstanding if there's a way for me to not have it as a runtime dependency! Thanks so much for the package, it's working great.

aralroca commented 1 year ago

next-translate-plugin is only needed during the build, the Webpack plugin (Turbopack in the future) is not executed in runtime in production. And we are using the TypeScript parser to parse the files during the build, this is why we are importing TypeScript, however, I've plans to try to migrate to SWC parser that is already accessible in Next.js builds without the need for an external parser. However, the size should not matter because these KBs are not imported into the bundler.

MartinDavi commented 1 year ago

@aralroca Thanks for the quick response.

This is what I'm not clear on: after building, if I go into the next.config.js and remove the const nextTranslate = require('next-translate-plugin'); and the call to nextTranslate(), I get the following error message when running next start:

throw new Error(`Specified i18n.locales should be an Array received ${typeof i18n.locales}.\nSee more info here: https://nextjs.org/docs/messages/invalid-i18n-config`);

I imagine the call to nextTranslate was was adding the locales and defaultLocales to my next.config.js from my i18n.js, and without it the server throws that error when starting.

Also, if this was working, does this mean I have to supply a different next.config.js for building than for server runtime?

Edit: This may all be because the project is part of an nx monorepo - when building in nx it includes the next.config.js in the build output. I'll dig into that and see if it's my issue.

Another Edit: Yeah this seems maybe nx specific. I'll update here if it turns out not to be. Thanks!