-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Windows tests for new postcss plugin (#14085)
This PR fixes the new `postcss-fix-relative-paths` plugin for Windows paths. The issue was that we mixed Windows-style path separators from the absolute file paths with the Posix-style separators from globs. This caused the `dirname` functions to resolve to the wrong files. To solve this, we now make the difference very clear by calling the content a `glob`. For globs, we always expect Posix-style path separators and for the case of making a glob absolute (by prefixing the directory), we now convert them into Posix-style explicitly. This PR also fixes an issue where negative rules (e.g. `!./**/*.ts`) were not properly rewritten. --------- Co-authored-by: Jordan Pittman <jordan@cryptica.me>
- Loading branch information
1 parent
b078327
commit 7ee134a
Showing
4 changed files
with
71 additions
and
18 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/internal-postcss-fix-relative-paths/src/fixtures/example-project/src/index.css
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@content "./**/*.ts"; | ||
@content "!./**/*.ts"; | ||
@plugin "./plugin.js"; | ||
@plugin "./what\"s-this.js"; |
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
47 changes: 47 additions & 0 deletions
47
packages/internal-postcss-fix-relative-paths/src/normalize-path.ts
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,47 @@ | ||
// Inlined version of `normalize-path` <https://github.com/jonschlinkert/normalize-path> | ||
// Copyright (c) 2014-2018, Jon Schlinkert. | ||
// Released under the MIT License. | ||
function normalizePathBase(path: string, stripTrailing?: boolean) { | ||
if (typeof path !== 'string') { | ||
throw new TypeError('expected path to be a string') | ||
} | ||
|
||
if (path === '\\' || path === '/') return '/' | ||
|
||
var len = path.length | ||
if (len <= 1) return path | ||
|
||
// ensure that win32 namespaces has two leading slashes, so that the path is | ||
// handled properly by the win32 version of path.parse() after being normalized | ||
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces | ||
var prefix = '' | ||
if (len > 4 && path[3] === '\\') { | ||
var ch = path[2] | ||
if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') { | ||
path = path.slice(2) | ||
prefix = '//' | ||
} | ||
} | ||
|
||
var segs = path.split(/[/\\]+/) | ||
if (stripTrailing !== false && segs[segs.length - 1] === '') { | ||
segs.pop() | ||
} | ||
return prefix + segs.join('/') | ||
} | ||
|
||
export function normalizePath(originalPath: string) { | ||
let normalized = normalizePathBase(originalPath) | ||
|
||
// Make sure Windows network share paths are normalized properly | ||
// They have to begin with two slashes or they won't resolve correctly | ||
if ( | ||
originalPath.startsWith('\\\\') && | ||
normalized.startsWith('/') && | ||
!normalized.startsWith('//') | ||
) { | ||
return `/${normalized}` | ||
} | ||
|
||
return normalized | ||
} |