Skip to content

Commit 935add4

Browse files
refactor: reduce deps
1 parent 35a9efe commit 935add4

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

package-lock.json

+14-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"webpack": "^5.0.0"
4343
},
4444
"dependencies": {
45-
"loader-utils": "^2.0.0",
4645
"schema-utils": "^3.0.0"
4746
},
4847
"devDependencies": {

src/loader.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import path from 'path';
22

3-
import loaderUtils from 'loader-utils';
4-
5-
import { findModuleById, evalModuleCode, AUTO_PUBLIC_PATH } from './utils';
3+
import {
4+
findModuleById,
5+
evalModuleCode,
6+
AUTO_PUBLIC_PATH,
7+
stringifyRequest,
8+
} from './utils';
69
import schema from './loader-options.json';
710

811
import MiniCssExtractPlugin, { pluginName, pluginSymbol } from './index';
@@ -15,7 +18,7 @@ function hotLoader(content, context) {
1518
return `${content}
1619
if(module.hot) {
1720
// ${Date.now()}
18-
var cssReload = require(${loaderUtils.stringifyRequest(
21+
var cssReload = require(${stringifyRequest(
1922
context.context,
2023
path.join(__dirname, 'hmr/hotModuleReplacement.js')
2124
)})(module.id, ${JSON.stringify({

src/utils.js

+46
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NativeModule from 'module';
2+
import path from 'path';
23

34
function trueFn() {
45
return true;
@@ -54,11 +55,56 @@ function compareModulesByIdentifier(a, b) {
5455
const MODULE_TYPE = 'css/mini-extract';
5556
const AUTO_PUBLIC_PATH = '__MINI_CSS_EXTRACT_PLUGIN_PUBLIC_PATH__';
5657

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+
57102
export {
58103
trueFn,
59104
findModuleById,
60105
evalModuleCode,
61106
compareModulesByIdentifier,
62107
MODULE_TYPE,
63108
AUTO_PUBLIC_PATH,
109+
stringifyRequest,
64110
};

0 commit comments

Comments
 (0)