-
Notifications
You must be signed in to change notification settings - Fork 9
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 applySourceMap #3
Open
onigoetz
wants to merge
4
commits into
jridgewell:main
Choose a base branch
from
onigoetz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
const urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.-]*)(?::(\d+))?(.*)$/; | ||
const dataUrlRegexp = /^data:.+,.+$/; | ||
|
||
interface ParsedURL { | ||
scheme: string; | ||
auth: string; | ||
host: string; | ||
port: string; | ||
path: string; | ||
} | ||
|
||
export function urlParse(aUrl: string): ParsedURL | null { | ||
const match = aUrl.match(urlRegexp); | ||
if (!match) { | ||
return null; | ||
} | ||
return { | ||
scheme: match[1], | ||
auth: match[2], | ||
host: match[3], | ||
port: match[4], | ||
path: match[5], | ||
}; | ||
} | ||
|
||
export function urlGenerate(aParsedUrl: ParsedURL) { | ||
let url = ''; | ||
if (aParsedUrl.scheme) { | ||
url += aParsedUrl.scheme + ':'; | ||
} | ||
url += '//'; | ||
if (aParsedUrl.auth) { | ||
url += aParsedUrl.auth + '@'; | ||
} | ||
if (aParsedUrl.host) { | ||
url += aParsedUrl.host; | ||
} | ||
if (aParsedUrl.port) { | ||
url += ':' + aParsedUrl.port; | ||
} | ||
if (aParsedUrl.path) { | ||
url += aParsedUrl.path; | ||
} | ||
return url; | ||
} | ||
|
||
export function isAbsolute(aPath: string) { | ||
return aPath.charAt(0) === '/' || urlRegexp.test(aPath); | ||
} | ||
|
||
/** | ||
* Normalizes a path, or the path portion of a URL: | ||
* | ||
* - Replaces consecutive slashes with one slash. | ||
* - Removes unnecessary '.' parts. | ||
* - Removes unnecessary '<dir>/..' parts. | ||
* | ||
* Based on code in the Node.js 'path' core module. | ||
* | ||
* @param aPath The path or url to normalize. | ||
*/ | ||
export function normalize(aPath: string) { | ||
let path = aPath; | ||
const url = urlParse(aPath); | ||
if (url) { | ||
if (!url.path) { | ||
return aPath; | ||
} | ||
path = url.path; | ||
} | ||
const isAbsoluteBool = isAbsolute(path); | ||
// Split the path into parts between `/` characters. This is much faster than | ||
// using `.split(/\/+/g)`. | ||
const parts = []; | ||
let start = 0; | ||
let i = 0; | ||
while (true) { | ||
start = i; | ||
i = path.indexOf('/', start); | ||
if (i === -1) { | ||
parts.push(path.slice(start)); | ||
break; | ||
} else { | ||
parts.push(path.slice(start, i)); | ||
while (i < path.length && path[i] === '/') { | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
for (let part, up = 0, i = parts.length - 1; i >= 0; i--) { | ||
part = parts[i]; | ||
if (part === '.') { | ||
parts.splice(i, 1); | ||
} else if (part === '..') { | ||
up++; | ||
} else if (up > 0) { | ||
if (part === '') { | ||
// The first part is blank if the path is absolute. Trying to go | ||
// above the root is a no-op. Therefore we can remove all '..' parts | ||
// directly after the root. | ||
parts.splice(i + 1, up); | ||
up = 0; | ||
} else { | ||
parts.splice(i, 2); | ||
up--; | ||
} | ||
} | ||
} | ||
path = parts.join('/'); | ||
|
||
if (path === '') { | ||
path = isAbsoluteBool ? '/' : '.'; | ||
} | ||
|
||
if (url) { | ||
url.path = path; | ||
return urlGenerate(url); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* Joins two paths/URLs. | ||
* | ||
* @param aRoot The root path or URL. | ||
* @param aPath The path or URL to be joined with the root. | ||
* | ||
* - If aPath is a URL or a data URI, aPath is returned, unless aPath is a | ||
* scheme-relative URL: Then the scheme of aRoot, if any, is prepended | ||
* first. | ||
* - Otherwise aPath is a path. If aRoot is a URL, then its path portion | ||
* is updated with the result and aRoot is returned. Otherwise the result | ||
* is returned. | ||
* - If aPath is absolute, the result is aPath. | ||
* - Otherwise the two paths are joined with a slash. | ||
* - Joining for example 'http://' and 'www.example.com' is also supported. | ||
*/ | ||
export function join(aRoot: string, aPath: string) { | ||
onigoetz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (aRoot === '') { | ||
aRoot = '.'; | ||
} | ||
if (aPath === '') { | ||
aPath = '.'; | ||
} | ||
const aPathUrl = urlParse(aPath); | ||
const aRootUrl = urlParse(aRoot); | ||
if (aRootUrl) { | ||
aRoot = aRootUrl.path || '/'; | ||
} | ||
|
||
// `join(foo, '//www.example.org')` | ||
if (aPathUrl && !aPathUrl.scheme) { | ||
if (aRootUrl) { | ||
aPathUrl.scheme = aRootUrl.scheme; | ||
} | ||
return urlGenerate(aPathUrl); | ||
} | ||
|
||
if (aPathUrl || aPath.match(dataUrlRegexp)) { | ||
return aPath; | ||
} | ||
|
||
// `join('http://', 'www.example.com')` | ||
if (aRootUrl && !aRootUrl.host && !aRootUrl.path) { | ||
aRootUrl.host = aPath; | ||
return urlGenerate(aRootUrl); | ||
} | ||
|
||
const joined = | ||
aPath.charAt(0) === '/' ? aPath : normalize(aRoot.replace(/\/+$/, '') + '/' + aPath); | ||
|
||
if (aRootUrl) { | ||
aRootUrl.path = joined; | ||
return urlGenerate(aRootUrl); | ||
} | ||
return joined; | ||
} | ||
|
||
/** | ||
* Make a path relative to a URL or another path. | ||
* | ||
* @param aRoot The root path or URL. | ||
* @param aPath The path or URL to be made relative to aRoot. | ||
*/ | ||
export function relative(aRoot: string, aPath: string) { | ||
onigoetz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (aRoot === '') { | ||
aRoot = '.'; | ||
} | ||
|
||
aRoot = aRoot.replace(/\/$/, ''); | ||
|
||
// It is possible for the path to be above the root. In this case, simply | ||
// checking whether the root is a prefix of the path won't work. Instead, we | ||
// need to remove components from the root one by one, until either we find | ||
// a prefix that fits, or we run out of components to remove. | ||
let level = 0; | ||
while (aPath.indexOf(aRoot + '/') !== 0) { | ||
const index = aRoot.lastIndexOf('/'); | ||
if (index < 0) { | ||
return aPath; | ||
} | ||
|
||
// If the only part of the root that is left is the scheme (i.e. http://, | ||
// file:///, etc.), one or more slashes (/), or simply nothing at all, we | ||
// have exhausted all components, so the path is not relative to the root. | ||
aRoot = aRoot.slice(0, index); | ||
if (aRoot.match(/^([^/]+:\/)?\/*$/)) { | ||
return aPath; | ||
} | ||
|
||
++level; | ||
} | ||
|
||
// Make sure we add a "../" for each component we removed from the root. | ||
return Array(level + 1).join('../') + aPath.substr(aRoot.length + 1); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So the expectation is that the
sourceFile
is always fully resolved, and we need to make it back into an unresolved path?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.
My understanding is that the new map needs to be resolved relative to the existing sourcemap.
Removing this block will break all the tests with relative paths.
I've also checked locally with my pending PR to postcss and that's the same behaviour as
source-map-js