Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick required language only. #220

Closed
evpozdniakov opened this issue Oct 23, 2017 · 13 comments
Closed

Pick required language only. #220

evpozdniakov opened this issue Oct 23, 2017 · 13 comments

Comments

@evpozdniakov
Copy link

I was surprised that all the languages are inside my js bundle.

image

It would be great to have English by default and load additional language when Validator.useLang(...) triggered.

@hongyuan1306
Copy link

I run into the same problem, in that in the final bundle, about 7.6K gzipped is taken up by these unused language files:

selection_066

Does anyone have any clue how to exclude the files from being included? I am using webpack2 for packaging.

@hongyuan1306
Copy link

A workaround for those in the same shoe: use webpack IgnorePlugin to include only the necessary language file. For example, to include English only, add the line to webpack configuration file:

new webpack.IgnorePlugin(/^\.\/(?!en)(.+)$/, /validatorjs\/src\/lang/),

@dcworldwide
Copy link

Thankyou sir.

I tried new webpack.ContextReplacementPlugin(/validatorjs[/\]lang$/, /en/) but that didn't work.... I may have missed the src dir

@jacekk
Copy link
Contributor

jacekk commented May 11, 2019

@hongyuan1306 This works like a charm. I guess this issue can be closed now. What do you think? @skaterdav85 @garygreen

@garygreen
Copy link
Collaborator

garygreen commented May 11, 2019

Yeah there's various ways to exclude the lang from the bundle. By default it should only include english. Webpack may include all of them though because of the "wildcard" include so you can use ContextReplacementPlugin - this is what I'm currently using:

new webpack.ContextReplacementPlugin(/validatorjs[\/\\]src[\/\\]lang/, /en/),

(that's webpack 3, not sure if that would work for v4)

@mixtur
Copy link

mixtur commented Sep 26, 2019

All these is a workaround not a solution.
There must be a way not to have these modules in the first place.

@mikeerickson
Copy link
Owner

All these is a workaround not a solution.
There must be a way not to have these modules in the first place.

@mixtur We welcome any input or PRs to address this issue. In the meantime, there are a number of viable solutions supplied around using webpack. When preparing a solution, keep in mind that it needs to work against all the supported languages. Default to US only is not a viable solution as we have a large international user base and their preferred languages need to be taken into consideration

@mikeerickson
Copy link
Owner

@garygreen

Yeah there's various ways to exclude the lang from the bundle. By default it should only include >
english.

While I can appreciate your statement, assuming English default is not the desired solution as we have a large international user base, and defaulting to English just won't work. As I have mentioned to others, I am open to ideas of how this can be handled.

@mixtur
Copy link

mixtur commented Sep 27, 2019

Will date-fns solution be ok for you? When you use date-fns you must explicitly import locale, and then pass it to every call. I believe in your case it may be easier because you only need to create Validator object with locale once.

@garygreen
Copy link
Collaborator

@mikeerickson I agree it's not ideal to assume everyone wants English as the primary language - though that's just how it was implemented by myself years ago as it was the logical decision to include it as the main language and then any additional languages can be included seperately as desired.

Of course the ideal scenario is to support custom builds of some kind so developers can set their primary language, but it's not clear how to achieve this with our current build system using Grunt + Browserify. If we switched to a modern bundler it maybe easier with code splitting, etc. I think the ideal solution would be to have a new way validatorjs can be import ed to your bundle without any languages and then import the languages specially that you want to support.

It's tricky to get this all working in the browser, node, bundlers etc so any PRs/suggestions are welcome.

@garygreen
Copy link
Collaborator

Will date-fns solution be ok for you? Whe you use date-fns you must explicitly import locale, and then pass it to every call

This sounds like what I'm thinking too.

@pepijnolivier
Copy link

Thanks for the regex solution @hongyuan1306
It works. Now that we're a few years later, with webpack 5, the syntax is slightly different.

Leaving it here for anyone that might need this in the future:

new webpack.IgnorePlugin({
    resourceRegExp: /^\.\/(?!en)(.+)$/,
    contextRegExp: /validatorjs\/src\/lang/
}),

@jacekk
Copy link
Contributor

jacekk commented Jul 12, 2022

@pepijnolivier FTR, this is based on the official documentation --> https://webpack.js.org/plugins/ignore-plugin/#using-regular-expressions . Definitely not a hack or sth one could not figure out 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants