Skip to content

fix: crash with custom webpack plugin #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};