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

No relative require in locales #1989

Closed
wants to merge 2 commits into from
Closed

No relative require in locales #1989

wants to merge 2 commits into from

Conversation

timaschew
Copy link

This PR rewrote all require('../moment') to require('moment')

The reason is to decouple the locales from the moment.js file.
This could cause problems in browser environment, because locale files are not always bundled with moment.js, so there should be no relative relation anymore.

In Node.js envirnoment this works fine, because node will lookup moment in the node_modules directory. Also browserify still works.

This makes it easier for tools which use require in browser environment to load moment. To get it working the namespace/path moment must be registered, instead of ../moment

@timaschew
Copy link
Author

I found another solution instead of using npm link, use the new feature of npm 2: local dependencies: in my branch: https://github.com/timaschew/moment/commits/moment-as-module
see change in package:json

@ichernev
Copy link
Contributor

I'm a bit confused now. Do we need this and why? What works better and what might (possibly) break.

@timaschew
Copy link
Author

You should use it because moment and all locales files should obviously not bundled together in all cases. For instance if you want to load locale files via AMD or in some other dynamic way.
So this means, that the locales should be independent in the aspect of where moment is located.

So that involves that using ../moment in the require path should be avoided.

IMO the version with define a locale module in the package.json is cleaner than using grunt shell + npm link commands. The only condition is: you need npm >= 2.x
But since you need this only for test and development purposes, it should be ok.

@ichernev
Copy link
Contributor

I haven't seen people complaining about the way we import moment inside language files. Also the way you propose to put it seems like the languages and core are in separate repositories. All node projects I've seen have relative dependencies between the files. If we put locale files in another repo, then it would make more sense, but it would also make it more complicated for users.

If this is causing a problem for component, why not maintain a separate component repository that we push on every release.

@timaschew
Copy link
Author

Yes, a separate repos would make sense, but also a little bit overkill.

You talking about Node projects, but I'm talking about browser environment.
For server side it's no problem because file size doesn't matter in this dimension.
But in the browser/client world this is different.
This is not a problem for component only, this also a problem for browserify and every other browser build tool.

AMD has it's own syntax and define a namespace, this is the reason why their implementations like Require.js is not affected with this issue.

@timaschew
Copy link
Author

I haven't seen people complaining about the way we import moment inside language files.

#2007

This and the problem of a dynamic require in the moment.js:

require('./locale/' + name);
is the problem. There is only one environment where this works, it's Node.js.

@ichernev
Copy link
Contributor

Well, the dynamic require is just a nice feature for node.js. Its not something that we want to support, it was just something that we could :)

How is size a problem for browser in this setting (browserify / component). They fetch only the files needed. Also for those environments -- yes, you have to specify the language files you want (or just take all locales) -- I don't see any problem with that. Is there a way around? Requiring a locale used to also set it, but then there are other weird cases we have to handle.

How is making the requires not relative fix any of those?

@ichernev
Copy link
Contributor

The es6 locale is going to provide different builds for different environments, so then (hopefully) everybody will be happy, no matter the build / module system used.

You still didn't say which tools have problems loading moment now, and under what circumstances.

@timaschew
Copy link
Author

You still didn't say which tools have problems loading moment now, and under what circumstances.

it's browserify (#2007) and component (#1966)

@ichernev
Copy link
Contributor

ichernev commented Jul 4, 2015

@timaschew the require logic was severely changed in 2.10.0, can you verify if it works for you now (2.10.3 is latest).

I guess we still require relatively, but amd requires are absolute. I guess we can add a special browserify/component header that works well for it.

@dasa
Copy link
Contributor

dasa commented Mar 31, 2016

I've opened PR #3082 since it is sometimes a pain to define the main for the moment package. Is it still required that AMD use a package dependency?

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

Successfully merging this pull request may close these issues.

None yet

4 participants