Skip to content

Commit

Permalink
fix: crash with multiple webpack versions (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Oct 5, 2021
1 parent b042ce7 commit b4431cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import { validate } from "schema-utils";

import { getUndoPath } from "webpack/lib/util/identifier";

import schema from "./plugin-options.json";
import {
trueFn,
Expand All @@ -12,6 +10,7 @@ import {
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
compareModulesByIdentifier,
getUndoPath,
} from "./utils";

export const pluginName = "mini-css-extract-plugin";
Expand Down
40 changes: 40 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,45 @@ function stringifyRequest(loaderContext, request) {
);
}

function getUndoPath(filename, outputPath, enforceRelative) {
let depth = -1;
let append = "";

// eslint-disable-next-line no-param-reassign
outputPath = outputPath.replace(/[\\/]$/, "");

for (const part of filename.split(/[/\\]+/)) {
if (part === "..") {
if (depth > -1) {
// eslint-disable-next-line no-plusplus
depth--;
} else {
const i = outputPath.lastIndexOf("/");
const j = outputPath.lastIndexOf("\\");
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);

if (pos < 0) {
return `${outputPath}/`;
}

append = `${outputPath.slice(pos + 1)}/${append}`;

// eslint-disable-next-line no-param-reassign
outputPath = outputPath.slice(0, pos);
}
} else if (part !== ".") {
// eslint-disable-next-line no-plusplus
depth++;
}
}

return depth > 0
? `${"../".repeat(depth)}${append}`
: enforceRelative
? `./${append}`
: append;
}

export {
trueFn,
findModuleById,
Expand All @@ -112,4 +151,5 @@ export {
ABSOLUTE_PUBLIC_PATH,
SINGLE_DOT_PATH_SEGMENT,
stringifyRequest,
getUndoPath,
};

0 comments on commit b4431cb

Please sign in to comment.