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

Normalize path for windows os #197

Merged
merged 2 commits into from
Dec 11, 2017

Conversation

xcatliu
Copy link
Contributor

@xcatliu xcatliu commented Dec 11, 2017

This pr fixed the path issue in windows os.

When exec parcel build site/index.html, the assets in html will be transformed to another path in dist/index.html like this:

<script src="\docs\89ee6b6452b6649c72b85a19637b8362.js"></script>

However, the \ separator is not a standard separator for web. With this fix, it will be transformed to:

<script src="/docs/89ee6b6452b6649c72b85a19637b8362.js"></script>

normalize-path is a wildly used module to normalize the path
image

@devongovett
Copy link
Member

can you just use the builtin url.normalize for this instead of adding another dependency? https://nodejs.org/api/url.html#url_url_resolve_from_to

@brandon93s brandon93s closed this Dec 11, 2017
@brandon93s brandon93s reopened this Dec 11, 2017
@brandon93s
Copy link
Contributor

accidental close

It actually looks like url.resolve might not handle this correctly, either: repl.it.

normalize-path seems to only handle \\ and not \.

We may want to just roll our own utility for this...

@itsMapleLeaf
Copy link
Contributor

Correct me if I'm wrong, but, as I understand it, the \ as it is in a JS string wouldn't ever show up in a path, right? Because \\ in JS means \, but there's no equivalent for \ since it's a special character, and not an output character. So url.resolve should work fine here

@xcatliu
Copy link
Contributor Author

xcatliu commented Dec 11, 2017

@brandon93s I tried normalize-path and url.resolve, both of them can handle this correctly. As @kingdaro said, \ is an escape symbol in JS string. So \\ means \ in string.

const url = require('url');

console.log(url.resolve('/base', 'path/to/resource'));
// will log /path/to/resource

console.log(url.resolve('/base/', 'path/to/resource'));
// will log /base/path/to/resource

console.log(url.resolve('\\base\\', 'path\\to\\resource'));
// will log /base/path/to/resource

console.log(url.resolve('\\path\\to\\resource', ''));
// will log /path/to/resource

@devongovett devongovett merged commit 0479dee into parcel-bundler:master Dec 11, 2017
@xcatliu xcatliu deleted the normalize-path branch December 11, 2017 10:37
devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Normalize path for windows os

* Use url.resolve instead of normalize-path
devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Normalize path for windows os

* Use url.resolve instead of normalize-path
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.

4 participants