From b4431cb60a6eadcf8c2b614f494faf899c73aaa0 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 5 Oct 2021 23:54:42 +0300 Subject: [PATCH] fix: crash with multiple webpack versions (#845) --- src/index.js | 3 +-- src/utils.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 467853a8..9a1b17de 100644 --- a/src/index.js +++ b/src/index.js @@ -2,8 +2,6 @@ import { validate } from "schema-utils"; -import { getUndoPath } from "webpack/lib/util/identifier"; - import schema from "./plugin-options.json"; import { trueFn, @@ -12,6 +10,7 @@ import { ABSOLUTE_PUBLIC_PATH, SINGLE_DOT_PATH_SEGMENT, compareModulesByIdentifier, + getUndoPath, } from "./utils"; export const pluginName = "mini-css-extract-plugin"; diff --git a/src/utils.js b/src/utils.js index f69456b6..ea569c45 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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, @@ -112,4 +151,5 @@ export { ABSOLUTE_PUBLIC_PATH, SINGLE_DOT_PATH_SEGMENT, stringifyRequest, + getUndoPath, };