diff --git a/package-lock.json b/package-lock.json index 4b290126..8814b4a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "version": "1.6.2", "license": "MIT", "dependencies": { - "loader-utils": "^2.0.0", "schema-utils": "^3.0.0" }, "devDependencies": { @@ -4170,6 +4169,7 @@ "version": "5.2.2", "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", "integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==", + "dev": true, "engines": { "node": "*" } @@ -6233,6 +6233,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/emojis-list/-/emojis-list-3.0.0.tgz", "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==", + "dev": true, "engines": { "node": ">= 4" } @@ -11634,6 +11635,7 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "dev": true, "dependencies": { "minimist": "^1.2.5" }, @@ -11982,6 +11984,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.0.tgz", "integrity": "sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==", + "dev": true, "dependencies": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", @@ -12737,7 +12740,8 @@ "node_modules/minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true }, "node_modules/minimist-options": { "version": "4.1.0", @@ -21084,7 +21088,8 @@ "big.js": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", - "integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==" + "integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==", + "dev": true }, "binary-extensions": { "version": "1.13.1", @@ -22678,7 +22683,8 @@ "emojis-list": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/emojis-list/-/emojis-list-3.0.0.tgz", - "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==" + "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==", + "dev": true }, "encodeurl": { "version": "1.0.2", @@ -26773,6 +26779,7 @@ "version": "2.2.0", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "dev": true, "requires": { "minimist": "^1.2.5" } @@ -27030,6 +27037,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.0.tgz", "integrity": "sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==", + "dev": true, "requires": { "big.js": "^5.2.2", "emojis-list": "^3.0.0", @@ -27612,7 +27620,8 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true }, "minimist-options": { "version": "4.1.0", diff --git a/package.json b/package.json index b9381786..bb68432a 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "webpack": "^5.0.0" }, "dependencies": { - "loader-utils": "^2.0.0", "schema-utils": "^3.0.0" }, "devDependencies": { diff --git a/src/loader.js b/src/loader.js index 59535bb5..7cd4ef3a 100644 --- a/src/loader.js +++ b/src/loader.js @@ -1,8 +1,11 @@ import path from 'path'; -import loaderUtils from 'loader-utils'; - -import { findModuleById, evalModuleCode, AUTO_PUBLIC_PATH } from './utils'; +import { + findModuleById, + evalModuleCode, + AUTO_PUBLIC_PATH, + stringifyRequest, +} from './utils'; import schema from './loader-options.json'; import MiniCssExtractPlugin, { pluginName, pluginSymbol } from './index'; @@ -15,7 +18,7 @@ function hotLoader(content, context) { return `${content} if(module.hot) { // ${Date.now()} - var cssReload = require(${loaderUtils.stringifyRequest( + var cssReload = require(${stringifyRequest( context.context, path.join(__dirname, 'hmr/hotModuleReplacement.js') )})(module.id, ${JSON.stringify({ diff --git a/src/utils.js b/src/utils.js index 6f27db13..3b9c447b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,5 @@ import NativeModule from 'module'; +import path from 'path'; function trueFn() { return true; @@ -54,6 +55,50 @@ function compareModulesByIdentifier(a, b) { const MODULE_TYPE = 'css/mini-extract'; const AUTO_PUBLIC_PATH = '__MINI_CSS_EXTRACT_PLUGIN_PUBLIC_PATH__'; +function isAbsolutePath(str) { + return path.posix.isAbsolute(str) || path.win32.isAbsolute(str); +} + +const RELATIVE_PATH_REGEXP = /^\.\.?[/\\]/; + +function isRelativePath(str) { + return RELATIVE_PATH_REGEXP.test(str); +} + +function stringifyRequest(loaderContext, request) { + const splitted = request.split('!'); + const { context } = loaderContext; + + return JSON.stringify( + splitted + .map((part) => { + // First, separate singlePath from query, because the query might contain paths again + const splittedPart = part.match(/^(.*?)(\?.*)/); + const query = splittedPart ? splittedPart[2] : ''; + let singlePath = splittedPart ? splittedPart[1] : part; + + if (isAbsolutePath(singlePath) && context) { + singlePath = path.relative(context, singlePath); + + if (isAbsolutePath(singlePath)) { + // If singlePath still matches an absolute path, singlePath was on a different drive than context. + // In this case, we leave the path platform-specific without replacing any separators. + // @see https://github.com/webpack/loader-utils/pull/14 + return singlePath + query; + } + + if (isRelativePath(singlePath) === false) { + // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules). + singlePath = `./${singlePath}`; + } + } + + return singlePath.replace(/\\/g, '/') + query; + }) + .join('!') + ); +} + export { trueFn, findModuleById, @@ -61,4 +106,5 @@ export { compareModulesByIdentifier, MODULE_TYPE, AUTO_PUBLIC_PATH, + stringifyRequest, };