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
How to load only locales i want? #2517
Comments
You can always take individual locale files from here. The docs describe how to load them But I think what you're asking is how to load them individually via npm? I'm not sure if that's possible without having a separate npm package for each locale. |
I described my problem badly. Another try :) In my app I installed momentjs with
For some reason and I cant find in momentjs source why it require all locales with this What i want is to require only Any suggestion? |
@schovi I'm not familiar with webpack, but you can do the following experiment.
--- /home/iskren/src/moment/locale/fr.js 2015-07-27 21:37:05.000000000 -0700
+++ locale/fr.js 2015-07-30 21:53:41.833619994 -0700
@@ -8,6 +8,8 @@
factory(global.moment)
}(this, function (moment) { 'use strict';
+ console.log("hello world");
+
var fr = moment.defineLocale('fr', {
months : 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_'),
$ node
> require("./moment")
// prints moment properties, but no "hello world"
$ node
> require("./locale/fr.js")
hello world
{} So this means Webpack is at fault and not the way we package moment. |
@mj1856 just to be clear, the locale files are shipped with npm, so he should find them in his |
Thanks for answer, will try to dig in webpack. |
FWIW, the size of the "moment" package shrinks from 364.36kB to 101.97kB (second-largest to fourth-largest package, going from twice-as-large to half-as-big as our company codebase itself) when I don't include the locales (done hackily by commenting out |
I suggest reopening this as it's not fixed. For anyone else looking for a fix, I found one here. In your webpack config, add the following to your plugins array:
This is pretty hacky, dependent on that particular include not changing. An official method would be ideal. Could you perhaps add a |
Actually, closing this as dup of #1435. Didn't see this before somehow. |
They are relevant, but absolutely different issues. The problem that people face here is not documenting moment.js behavior, but how to load only required locales and not all at once (how it is done in moment.js by default). I still don't see not hacky way to do it. |
jut to make a reference to an old topic about this exact issue on in webpack project |
The solution proposed looks great if you only use english. but what about the scenario you want to support all the moment js but only want to load the one you need to the browsers. The Dynamic loading works but it is inefficient in that it bundles all the locales to one bundle. When the bundle is requested it is essentially downloading the entire file. Has anyone found a good way such that that we only dynamically load the one you need? The only solution I see is making a locales.js file and that being a giant if else statement with one branch for each language file. tedious but would solve the issue of only loading the file I need. |
|
For anyone that cares in the future. The solution which resulted to be the least hacky was to use the suggested |
@pbrain19 see this answer to include languages other than english: |
From a browsers perspective it will still bundle and load it to the browser. This is not the ideal behavior we want in the browser. The answer there only explains how to bundle the locales you want. What I was looking for was an ability to support all locales without loading them all to the browser. |
Moment uses dynamic requires, Webpack can't know which locales you want to require during compile time so it loads the entire directory just in case it's required during runtime (where locale usage is defined). // moment.js:1837
function loadLocale(name) {
var oldLocale = null;
// TODO: Find a better way to register and load all the locales in Node
if (!locales[name] && (typeof module !== 'undefined') &&
module && module.exports) {
try {
oldLocale = globalLocale._abbr;
var aliasedRequire = require;
aliasedRequire('./locale/' + name);
getSetGlobalLocale(oldLocale);
} catch (e) {}
}
return locales[name];
} |
The following snippet keeps the default en.js, en-*.js, and moment source in the primary bundles/entry points, while mapping all other locale files into a separate async loaded bundle. To ensure that the locales you need are available, you just need to import them into your es6-compatible file. If you don't know what those are ahead of time, then there are existing runtime delay load things you can use; we use Before:
After:
|
The solution here looks a little cleaner: https://medium.com/@michalozogan/how-to-split-moment-js-locales-to-chunks-with-webpack-de9e25caccea
Then you can just import the ones you want
|
It's me or the webpack "solution" like |
@peterpeterparker for Angular-CLI see: angular/angular-cli#6137 (comment) |
@jvanderbiest cool I may give a try. In the meantime I just remove all the unnecessary locales (the one I don't use) straight in node_modules after each install. Ugly but does the job |
In my project - Angular cli 6 + moment-timezone - this article worked well: https://medium.jonasbandi.net/angular-cli-and-moment-js-a-recipe-for-disaster-and-how-to-fix-it-163a79180173 |
For anyone still dealing with this issue in 2020, there is now a webpack plugin that lets you define which locales you need (defaults to "en" only) and removes the rest: It has a sister package to strip out unnecessary timezones from moment-timezone: Between the two, we shaved off 800 kB of unnecessary size. |
Just wondering if there is anything equivalent to moment-locales-webpack-plugin in Vite land? |
Regarding Vite: I'm not sure why, but this seems to simply not be necessary. Nothing is bundled in the output module, unless you explicitly import it. #5926 import moment from 'moment'
import 'moment/dist/locale/fr'
console.log(moment('2012 juillet', 'YYYY MMM', 'fr').format()) |
I am using Webpack and your npm package. Unfortunately it require all locales at once.
Webpack knows they are optional
but for some reason they are not droped. Is there any way how to require just core with selected locales?
The text was updated successfully, but these errors were encountered: