Skip to content

Commit 44eb323

Browse files
committed
Handle periods in directory names when interpolating paths
1 parent 19bd9ee commit 44eb323

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

lib/interpolateName.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,15 @@ function interpolateName(loaderContext, name, options) {
3737
let directory = "";
3838
let folder = "";
3939
if(loaderContext.resourcePath) {
40+
const parsed = path.parse(loaderContext.resourcePath);
4041
let resourcePath = loaderContext.resourcePath;
41-
const idx = resourcePath.lastIndexOf(".");
42-
const i = resourcePath.lastIndexOf("\\");
43-
const j = resourcePath.lastIndexOf("/");
44-
const p = i < 0 ? j : j < 0 ? i : i < j ? i : j;
45-
if(idx >= 0) {
46-
ext = resourcePath.substr(idx + 1);
47-
resourcePath = resourcePath.substr(0, idx);
42+
43+
if(parsed.ext) {
44+
ext = parsed.ext.substr(1);
4845
}
49-
if(p >= 0) {
50-
basename = resourcePath.substr(p + 1);
51-
resourcePath = resourcePath.substr(0, p + 1);
46+
if(parsed.dir) {
47+
basename = parsed.name;
48+
resourcePath = parsed.dir + path.sep;
5249
}
5350
if(typeof context !== "undefined") {
5451
directory = path.relative(context, resourcePath + "_").replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1");

test/interpolateName.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ describe("interpolateName()", () => {
2828
["/app/flash.txt", "[hash]", "test content", "9473fdd0d880a43c21b7778d34872157"],
2929
["/app/img/image.png", "[sha512:hash:base64:7].[ext]", "test content", "2BKDTjl.png"],
3030
["/app/dir/file.png", "[path][name].[ext]?[hash]", "test content", "/app/dir/file.png?9473fdd0d880a43c21b7778d34872157"],
31-
["/vendor/test/images/loading.gif", path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"]
31+
["/vendor/test/images/loading.gif", path => path.replace(/\/?vendor\/?/, ""), "test content", "test/images/loading.gif"],
32+
["/pathWith.period/filename.js", "js/[name].[ext]", "test content", "js/filename.js"],
33+
["/pathWith.period/filenameWithoutExt", "js/[name].[ext]", "test content", "js/filenameWithoutExt.bin"]
3234
].forEach(test => {
3335
it("should interpolate " + test[0] + " " + test[1], () => {
3436
const interpolatedName = loaderUtils.interpolateName({ resourcePath: test[0] }, test[1], { content: test[2] });

0 commit comments

Comments
 (0)