wallet-core/node_modules/webpack/lib/RequestShortener.js

64 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-05-03 15:35:00 +02:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const path = require("path");
2017-08-14 05:01:11 +02:00
const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
const SEPARATOR_REGEXP = /[/\\]$/;
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g;
const INDEX_JS_REGEXP = /\/index.js(!|\?|\(query\))/g;
const normalizeBackSlashDirection = (request) => {
return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
};
const createRegExpForPath = (path) => {
const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
return new RegExp(`(^|!)${regexpTypePartial}`, "g");
};
2017-05-03 15:35:00 +02:00
class RequestShortener {
constructor(directory) {
2017-08-14 05:01:11 +02:00
directory = normalizeBackSlashDirection(directory);
if(SEPARATOR_REGEXP.test(directory)) directory = directory.substr(0, directory.length - 1);
2017-05-03 15:35:00 +02:00
if(directory) {
2017-08-14 05:01:11 +02:00
this.currentDirectoryRegExp = createRegExpForPath(directory);
2017-05-03 15:35:00 +02:00
}
const dirname = path.dirname(directory);
2017-08-14 05:01:11 +02:00
const endsWithSeperator = SEPARATOR_REGEXP.test(dirname);
2017-05-03 15:35:00 +02:00
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
if(parentDirectory && parentDirectory !== directory) {
2017-08-14 05:01:11 +02:00
this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
2017-05-03 15:35:00 +02:00
}
if(__dirname.length >= 2) {
2017-08-14 05:01:11 +02:00
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
2017-05-03 15:35:00 +02:00
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
this.buildinsAsModule = buildinsAsModule;
2017-08-14 05:01:11 +02:00
this.buildinsRegExp = createRegExpForPath(buildins);
2017-05-03 15:35:00 +02:00
}
}
shorten(request) {
if(!request) return request;
2017-08-14 05:01:11 +02:00
request = normalizeBackSlashDirection(request);
2017-05-03 15:35:00 +02:00
if(this.buildinsAsModule && this.buildinsRegExp)
request = request.replace(this.buildinsRegExp, "!(webpack)");
if(this.currentDirectoryRegExp)
request = request.replace(this.currentDirectoryRegExp, "!.");
if(this.parentDirectoryRegExp)
request = request.replace(this.parentDirectoryRegExp, "!..");
if(!this.buildinsAsModule && this.buildinsRegExp)
request = request.replace(this.buildinsRegExp, "!(webpack)");
2017-08-14 05:01:11 +02:00
request = request.replace(INDEX_JS_REGEXP, "$1");
return request.replace(FRONT_OR_BACK_BANG_REGEXP, "");
2017-05-03 15:35:00 +02:00
}
}
module.exports = RequestShortener;