Skip to content

Commit

Permalink
fix(path-utils): fix relativePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Mar 18, 2020
1 parent 19711fc commit e22ca71
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/utils/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export function isAbsolutePath(path: string): boolean {
* @returns `true` if `path` is relative, otherwise `false`
*/
export function isRelativePath(path: string): boolean {
return /^\.?\.\//.test(path);
return /^\.?\.[/\\]/.test(path);
}
10 changes: 3 additions & 7 deletions src/utils/sourcemap-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ export class MapModifier {
relative(dir: string = process.cwd()): this {
if (this.map.sources) {
this.map.sources = this.map.sources.map(source => {
if (isAbsolutePath(source)) {
return relativePath(dir, source);
} else if (isRelativePath(source)) {
return relativePath(path.dirname(path.join(dir, source)), source);
} else {
return normalizePath(source);
}
if (isAbsolutePath(source)) return relativePath(dir, source);
else if (isRelativePath(source)) return relativePath(dir, path.join(dir, source));
else return normalizePath(source);
});
}
return this;
Expand Down
10 changes: 2 additions & 8 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import loadModule from "../src/utils/load-module";
import { normalizePath } from "../src/utils/path-utils";
import { getInlineMap, getExtractedMap, stripMap, MapModifier } from "../src/utils/sourcemap-utils";
import path from "path";

Expand Down Expand Up @@ -33,12 +32,7 @@ test("strip map", () => {
});

test("map modifier", () => {
const map = JSON.stringify({
sources: [
normalizePath(path.join(process.cwd(), "..", "foo")),
normalizePath(path.join(process.cwd(), "..", "bar")),
],
});
const map = JSON.stringify({ sources: ["../a/b/../foo/bar.css", "../b/a/../bar/foo.css"] });
const relativeSrc = JSON.stringify(new MapModifier(map).relative().toObject().sources);
expect(relativeSrc).toBe(JSON.stringify(["../foo", "../bar"]));
expect(relativeSrc).toBe(JSON.stringify(["../a/foo/bar.css", "../b/bar/foo.css"]));
});

0 comments on commit e22ca71

Please sign in to comment.