|
1 | 1 | import NativeModule from 'module';
|
| 2 | +import path from 'path'; |
2 | 3 |
|
3 | 4 | function trueFn() {
|
4 | 5 | return true;
|
@@ -54,11 +55,56 @@ function compareModulesByIdentifier(a, b) {
|
54 | 55 | const MODULE_TYPE = 'css/mini-extract';
|
55 | 56 | const AUTO_PUBLIC_PATH = '__MINI_CSS_EXTRACT_PLUGIN_PUBLIC_PATH__';
|
56 | 57 |
|
| 58 | +function isAbsolutePath(str) { |
| 59 | + return path.posix.isAbsolute(str) || path.win32.isAbsolute(str); |
| 60 | +} |
| 61 | + |
| 62 | +const RELATIVE_PATH_REGEXP = /^\.\.?[/\\]/; |
| 63 | + |
| 64 | +function isRelativePath(str) { |
| 65 | + return RELATIVE_PATH_REGEXP.test(str); |
| 66 | +} |
| 67 | + |
| 68 | +function stringifyRequest(loaderContext, request) { |
| 69 | + const splitted = request.split('!'); |
| 70 | + const { context } = loaderContext; |
| 71 | + |
| 72 | + return JSON.stringify( |
| 73 | + splitted |
| 74 | + .map((part) => { |
| 75 | + // First, separate singlePath from query, because the query might contain paths again |
| 76 | + const splittedPart = part.match(/^(.*?)(\?.*)/); |
| 77 | + const query = splittedPart ? splittedPart[2] : ''; |
| 78 | + let singlePath = splittedPart ? splittedPart[1] : part; |
| 79 | + |
| 80 | + if (isAbsolutePath(singlePath) && context) { |
| 81 | + singlePath = path.relative(context, singlePath); |
| 82 | + |
| 83 | + if (isAbsolutePath(singlePath)) { |
| 84 | + // If singlePath still matches an absolute path, singlePath was on a different drive than context. |
| 85 | + // In this case, we leave the path platform-specific without replacing any separators. |
| 86 | + // @see https://github.com/webpack/loader-utils/pull/14 |
| 87 | + return singlePath + query; |
| 88 | + } |
| 89 | + |
| 90 | + if (isRelativePath(singlePath) === false) { |
| 91 | + // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules). |
| 92 | + singlePath = `./${singlePath}`; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return singlePath.replace(/\\/g, '/') + query; |
| 97 | + }) |
| 98 | + .join('!') |
| 99 | + ); |
| 100 | +} |
| 101 | + |
57 | 102 | export {
|
58 | 103 | trueFn,
|
59 | 104 | findModuleById,
|
60 | 105 | evalModuleCode,
|
61 | 106 | compareModulesByIdentifier,
|
62 | 107 | MODULE_TYPE,
|
63 | 108 | AUTO_PUBLIC_PATH,
|
| 109 | + stringifyRequest, |
64 | 110 | };
|
0 commit comments