make RequestShortener simpler, only use relative path
This commit is contained in:
parent
c80bae2959
commit
d57820c53e
|
@ -208,7 +208,7 @@ class Compiler {
|
|||
|
||||
this.context = context;
|
||||
|
||||
this.requestShortener = new RequestShortener(context);
|
||||
this.requestShortener = new RequestShortener(context, this.root);
|
||||
|
||||
this.cache = new Cache();
|
||||
|
||||
|
|
|
@ -5,78 +5,18 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { join, dirname: fsDirname } = require("./util/fs");
|
||||
|
||||
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 MATCH_RESOURCE_REGEXP = /!=!/;
|
||||
|
||||
/**
|
||||
* @param {string} request the request
|
||||
* @returns {string} the normalized request
|
||||
*/
|
||||
const normalizeBackSlashDirection = request => {
|
||||
return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} path the path to match
|
||||
* @returns {RegExp} the path matcher
|
||||
*/
|
||||
const createRegExpForPath = path => {
|
||||
const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
|
||||
return new RegExp(`(^|!)${regexpTypePartial}`, "g");
|
||||
};
|
||||
const { contextify } = require("./util/identifier");
|
||||
|
||||
class RequestShortener {
|
||||
/**
|
||||
* @param {string} dir the directory
|
||||
* @param {object=} associatedObjectForCache an object to which the cache will be attached
|
||||
*/
|
||||
constructor(dir) {
|
||||
/** @type {RegExp | null} */
|
||||
this.currentDirectoryRegExp = null;
|
||||
/** @type {RegExp | null} */
|
||||
this.parentDirectoryRegExp = null;
|
||||
/** @type {RegExp | null} */
|
||||
this.buildinsRegExp = null;
|
||||
/** @type {boolean} */
|
||||
this.buildinsAsModule = false;
|
||||
|
||||
let directory = normalizeBackSlashDirection(dir);
|
||||
if (SEPARATOR_REGEXP.test(directory)) {
|
||||
directory = directory.substr(0, directory.length - 1);
|
||||
}
|
||||
|
||||
if (directory) {
|
||||
this.currentDirectoryRegExp = createRegExpForPath(directory);
|
||||
}
|
||||
|
||||
const dirname = fsDirname(undefined, directory);
|
||||
const endsWithSeparator = SEPARATOR_REGEXP.test(dirname);
|
||||
const parentDirectory = endsWithSeparator
|
||||
? dirname.substr(0, dirname.length - 1)
|
||||
: dirname;
|
||||
|
||||
if (parentDirectory && parentDirectory !== directory) {
|
||||
this.parentDirectoryRegExp = createRegExpForPath(`${parentDirectory}/`);
|
||||
}
|
||||
|
||||
if (__dirname.length >= 2) {
|
||||
const buildins = normalizeBackSlashDirection(
|
||||
join(undefined, __dirname, "..")
|
||||
);
|
||||
const buildinsAsModule =
|
||||
this.currentDirectoryRegExp &&
|
||||
this.currentDirectoryRegExp.test(buildins);
|
||||
this.buildinsAsModule = buildinsAsModule;
|
||||
this.buildinsRegExp = createRegExpForPath(buildins);
|
||||
}
|
||||
|
||||
/** @type {Map<string, string>} */
|
||||
this.cache = new Map();
|
||||
constructor(dir, associatedObjectForCache) {
|
||||
this.contextify = contextify.bindContextCache(
|
||||
dir,
|
||||
associatedObjectForCache
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -87,28 +27,7 @@ class RequestShortener {
|
|||
if (!request) {
|
||||
return request;
|
||||
}
|
||||
const cacheEntry = this.cache.get(request);
|
||||
if (cacheEntry !== undefined) {
|
||||
return cacheEntry;
|
||||
}
|
||||
let result = normalizeBackSlashDirection(request);
|
||||
if (this.buildinsAsModule && this.buildinsRegExp) {
|
||||
result = result.replace(this.buildinsRegExp, "!(webpack)");
|
||||
}
|
||||
if (this.currentDirectoryRegExp) {
|
||||
result = result.replace(this.currentDirectoryRegExp, "!.");
|
||||
}
|
||||
if (this.parentDirectoryRegExp) {
|
||||
result = result.replace(this.parentDirectoryRegExp, "!../");
|
||||
}
|
||||
if (!this.buildinsAsModule && this.buildinsRegExp) {
|
||||
result = result.replace(this.buildinsRegExp, "!(webpack)");
|
||||
}
|
||||
result = result.replace(INDEX_JS_REGEXP, "$1");
|
||||
result = result.replace(FRONT_OR_BACK_BANG_REGEXP, "");
|
||||
result = result.replace(MATCH_RESOURCE_REGEXP, " = ");
|
||||
this.cache.set(request, result);
|
||||
return result;
|
||||
return this.contextify(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ const DEFAULTS = {
|
|||
requestShortener: (options, context, compilation) =>
|
||||
compilation.compiler.context === options.context
|
||||
? compilation.requestShortener
|
||||
: new RequestShortener(options.context),
|
||||
: new RequestShortener(options.context, compilation.compiler.root),
|
||||
performance: NORMAL_ON,
|
||||
hash: NORMAL_ON,
|
||||
env: NORMAL_OFF,
|
||||
|
|
|
@ -482,12 +482,12 @@ describe("loaders", () => {
|
|||
Object {
|
||||
"errors": Array [
|
||||
Object {
|
||||
"message": "Module build failed (from (webpack)/node_modules/json-loader/index.js):\\nSyntaxError: Unexpected end of JSON input",
|
||||
"message": "Module build failed (from ../../../node_modules/json-loader/index.js):\\nSyntaxError: Unexpected end of JSON input",
|
||||
"moduleId": 0,
|
||||
"moduleIdentifier": "<cwd>/node_modules/json-loader/index.js!<cwd>/test/fixtures/errors/not-a-json.js",
|
||||
"moduleName": "(webpack)/node_modules/json-loader!./not-a-json.js",
|
||||
"moduleName": "../../../node_modules/json-loader/index.js!./not-a-json.js",
|
||||
"moduleTrace": Array [],
|
||||
"stack": "ModuleBuildError: Module build failed (from (webpack)/node_modules/json-loader/index.js):\\nSyntaxError: Unexpected end of JSON input",
|
||||
"stack": "ModuleBuildError: Module build failed (from ../../../node_modules/json-loader/index.js):\\nSyntaxError: Unexpected end of JSON input",
|
||||
},
|
||||
],
|
||||
"warnings": Array [],
|
||||
|
|
|
@ -16,7 +16,7 @@ describe("RequestShortener", () => {
|
|||
it("should create RequestShortener and not shorten parent directory neighbor", () => {
|
||||
const shortener = new RequestShortener("/foo/bar");
|
||||
expect(shortener.shorten("/foo_baz/bar/some.js")).toEqual(
|
||||
"/foo_baz/bar/some.js"
|
||||
"../../foo_baz/bar/some.js"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue