-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add require.version. #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense — as a streamlined version of require.alias — although the stringly-typed nature of the names and versions means that it’s possible to do odd stuff here, like:
const myRequire = d3.require.version({
"d3": "6",
"d3@5": "5.9.1"
});
myRequire("d3", "d3@5", "d3@4")
Yeah, that would presumably break because you’d require |
That would be one way to go. Another one would be to strip the versions from the keys before appending, allowing you to be specific about versions at a granular level, for example: const myRequire = d3.require.version({
"d3@5": "5.9.1",
"d3@4": "4.2.7",
"d3@3": "3.1.1"
}); ... locking that require down to one specific version of D3 for each major version request. |
In that approach, would it only strip the version range from And even if we did this, I’m not sure it would help: in the common case the argument to require is just the module name. It’s the associated package.json that tells which version is being asked for, and we haven’t loaded the package.json yet when the require is intercepted, so we can’t know what version range is being requested. For example, if you require vega-embed, it will We could, perhaps, allow you to resolve different versions of the same library varying by the base URL (the URL of the thing calling require), but it’s not clear how we’d fit this into a convenient object literal you’d pass to require.version. More likely if you need that level of specificity, you’d pass a custom resolver function to requireFrom. So… I’m inclined to keep things simple. |
I think that in that approach, you’d strip everything after the first But the point is well taken that for anything fancy that you want to achieve, you can always pass a custom resolver function. So keeping this simple seems just fine. The reason I'm requesting: d3.require.version({
"d3@5": "5.9.1"
}) ... is because naively, as a user who hasn’t read the documentation, I would tend to expect that to work. |
It’s a hard game to guess what people will assume without reading the documentation, but my guess is that people might be familiar with the package.json dependencies format, which is essentially what require.version expects as the versions object (and requires the keys to be bare module names). This does make me think, though, there’s another common pattern that the proposed version of require.version in this PR would not support: requires with a path, such as |
Fixes #22.