Skip to content

Commit

Permalink
refactor: reduce deps
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 30, 2021
1 parent 35a9efe commit 935add4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
19 changes: 14 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"webpack": "^5.0.0"
},
"dependencies": {
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0"
},
"devDependencies": {
Expand Down
11 changes: 7 additions & 4 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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({
Expand Down
46 changes: 46 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NativeModule from 'module';
import path from 'path';

function trueFn() {
return true;
Expand Down Expand Up @@ -54,11 +55,56 @@ 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,
evalModuleCode,
compareModulesByIdentifier,
MODULE_TYPE,
AUTO_PUBLIC_PATH,
stringifyRequest,
};

0 comments on commit 935add4

Please sign in to comment.