Skip to content

Commit b4431cb

Browse files
fix: crash with multiple webpack versions (#845)
1 parent b042ce7 commit b4431cb

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import { validate } from "schema-utils";
44

5-
import { getUndoPath } from "webpack/lib/util/identifier";
6-
75
import schema from "./plugin-options.json";
86
import {
97
trueFn,
@@ -12,6 +10,7 @@ import {
1210
ABSOLUTE_PUBLIC_PATH,
1311
SINGLE_DOT_PATH_SEGMENT,
1412
compareModulesByIdentifier,
13+
getUndoPath,
1514
} from "./utils";
1615

1716
export const pluginName = "mini-css-extract-plugin";

src/utils.js

+40
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,45 @@ function stringifyRequest(loaderContext, request) {
102102
);
103103
}
104104

105+
function getUndoPath(filename, outputPath, enforceRelative) {
106+
let depth = -1;
107+
let append = "";
108+
109+
// eslint-disable-next-line no-param-reassign
110+
outputPath = outputPath.replace(/[\\/]$/, "");
111+
112+
for (const part of filename.split(/[/\\]+/)) {
113+
if (part === "..") {
114+
if (depth > -1) {
115+
// eslint-disable-next-line no-plusplus
116+
depth--;
117+
} else {
118+
const i = outputPath.lastIndexOf("/");
119+
const j = outputPath.lastIndexOf("\\");
120+
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
121+
122+
if (pos < 0) {
123+
return `${outputPath}/`;
124+
}
125+
126+
append = `${outputPath.slice(pos + 1)}/${append}`;
127+
128+
// eslint-disable-next-line no-param-reassign
129+
outputPath = outputPath.slice(0, pos);
130+
}
131+
} else if (part !== ".") {
132+
// eslint-disable-next-line no-plusplus
133+
depth++;
134+
}
135+
}
136+
137+
return depth > 0
138+
? `${"../".repeat(depth)}${append}`
139+
: enforceRelative
140+
? `./${append}`
141+
: append;
142+
}
143+
105144
export {
106145
trueFn,
107146
findModuleById,
@@ -112,4 +151,5 @@ export {
112151
ABSOLUTE_PUBLIC_PATH,
113152
SINGLE_DOT_PATH_SEGMENT,
114153
stringifyRequest,
154+
getUndoPath,
115155
};

0 commit comments

Comments
 (0)