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

require.toUrl fix for multiple relative segments #1263

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

stanhuff
Copy link

require.toUrl would return the wrong result when passed something like '../../module' because it assumes the last period character is an extension separator in a relative path so long as its index is > 1. This is a possible fix for this case although there may be a more elegant one.

@jrburke
Copy link
Member

jrburke commented Jan 23, 2015

What is a test scenario for this? Relative ID normalization should happen first before extension detection.

The case where I can see it falling down is when the ID normalization would go outside the module ID space. So if a top-level require.toUrl('../../mod') is done, since that would be outside the module ID space, requirejs keeps the starting '..' segments.

This was an unfortunate early design choice and in general I would do it differently now, probably throw an error. It can be avoided by using paths config to point to things outside the top of the module ID space, as this avoids other hazards of that squishy ID/path separation.

So while I can see this as an issue, it is a follow-on cascade issue of not keeping IDs and paths separate, unfortunately enabled by requirejs not throwing. I do not want to change that in the 2.x series, but want to change that for the next major revision.

I'll keep this open for now, but it is a lower priority given the above.

@stanhuff
Copy link
Author

stanhuff commented Feb 4, 2015

My specific situation is that I'm trying to create a url relative to my current module in the same directory. In this case, I'm calling something akin to toUrl("./script.js") - nothing nefarious there. However, what might possibly be outside the expectations of requirejs's "normal" operation is what I have my baseUrl set to in the config. It's set to the directory that is 1 level up from my html page. As such many of my normalized module ids end up being prefixed with a "../" segment. Everything actually works as expected except for toUrl.

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

Successfully merging this pull request may close these issues.

None yet

2 participants