Skip to content

Commit

Permalink
Merge pull request #9765 from ilgonmic/incorrect-path-cropping-reques…
Browse files Browse the repository at this point in the history
…t-shortener

Fix incorrect path cropping in RequestShortener
  • Loading branch information
sokra committed Oct 7, 2019
2 parents af50ee3 + 4f3c662 commit d94f32c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/RequestShortener.js
Expand Up @@ -38,7 +38,7 @@ class RequestShortener {
? dirname.substr(0, dirname.length - 1)
: dirname;
if (parentDirectory && parentDirectory !== directory) {
this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
this.parentDirectoryRegExp = createRegExpForPath(`${parentDirectory}/`);
}

if (__dirname.length >= 2) {
Expand Down Expand Up @@ -67,7 +67,7 @@ class RequestShortener {
result = result.replace(this.currentDirectoryRegExp, "!.");
}
if (this.parentDirectoryRegExp) {
result = result.replace(this.parentDirectoryRegExp, "!..");
result = result.replace(this.parentDirectoryRegExp, "!../");
}
if (!this.buildinsAsModule && this.buildinsRegExp) {
result = result.replace(this.buildinsRegExp, "!(webpack)");
Expand Down
22 changes: 22 additions & 0 deletions test/RequestShortener.unittest.js
@@ -0,0 +1,22 @@
"use strict";

const RequestShortener = require("../lib/RequestShortener");

describe("RequestShortener", () => {
it("should create RequestShortener and shorten with ./ file in directory", () => {
const shortener = new RequestShortener("/foo/bar");
expect(shortener.shorten("/foo/bar/some.js")).toEqual("./some.js");
});

it("should create RequestShortener and shorten with ../ file in parent directory", () => {
const shortener = new RequestShortener("/foo/bar");
expect(shortener.shorten("/foo/baz/some.js")).toEqual("../baz/some.js");
});

it("should create RequestShortener and not shorten parent directory neighbor", () => {
const shortener = new RequestShortener("/foo/bar");
expect(shortener.shorten("/foo_baz/bar/some.js")).toEqual(
"/foo_baz/bar/some.js"
);
});
});

0 comments on commit d94f32c

Please sign in to comment.