2014-07-18 19:31:50 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-02-23 01:00:31 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const ModuleFilenameHelpers = exports;
|
2014-07-18 19:31:50 +08:00
|
|
|
|
|
|
|
ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
|
|
|
|
ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
|
|
|
|
ModuleFilenameHelpers.RESOURCE = "[resource]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
|
|
|
|
ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH = /\[abs(olute)?-?resource-?path\]/gi;
|
|
|
|
ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
|
|
|
|
ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
|
|
|
|
ModuleFilenameHelpers.LOADERS = "[loaders]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
|
|
|
|
ModuleFilenameHelpers.QUERY = "[query]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
|
|
|
|
ModuleFilenameHelpers.ID = "[id]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
|
|
|
|
ModuleFilenameHelpers.HASH = "[hash]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
|
2017-10-18 06:48:04 +08:00
|
|
|
ModuleFilenameHelpers.NAMESPACE = "[namespace]";
|
|
|
|
ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
|
2014-07-18 19:31:50 +08:00
|
|
|
|
|
|
|
function getAfter(str, token) {
|
2017-02-23 01:00:31 +08:00
|
|
|
const idx = str.indexOf(token);
|
2014-07-18 19:31:50 +08:00
|
|
|
return idx < 0 ? "" : str.substr(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBefore(str, token) {
|
2017-02-23 01:00:31 +08:00
|
|
|
const idx = str.lastIndexOf(token);
|
2014-07-18 19:31:50 +08:00
|
|
|
return idx < 0 ? "" : str.substr(0, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHash(str) {
|
2017-02-23 01:00:31 +08:00
|
|
|
const hash = require("crypto").createHash("md5");
|
2014-07-18 19:31:50 +08:00
|
|
|
hash.update(str);
|
|
|
|
return hash.digest("hex").substr(0, 4);
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:00:39 +08:00
|
|
|
function asRegExp(test) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof test === "string") test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
2015-03-21 02:00:39 +08:00
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2017-10-19 06:24:59 +08:00
|
|
|
ModuleFilenameHelpers.createFilename = function createFilename(module, options, requestShortener) {
|
|
|
|
let opts;
|
|
|
|
if(typeof options === "object") {
|
|
|
|
opts = options;
|
|
|
|
} else {
|
|
|
|
opts = {
|
|
|
|
moduleFilenameTemplate: arguments[1],
|
|
|
|
namespace: ""
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-23 01:00:31 +08:00
|
|
|
let absoluteResourcePath;
|
|
|
|
let hash;
|
|
|
|
let identifier;
|
|
|
|
let moduleId;
|
|
|
|
let shortIdentifier;
|
2017-02-23 17:41:42 +08:00
|
|
|
if(module === undefined) module = "";
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof module === "string") {
|
2016-01-19 08:52:28 +08:00
|
|
|
shortIdentifier = requestShortener.shorten(module);
|
|
|
|
identifier = shortIdentifier;
|
|
|
|
moduleId = "";
|
|
|
|
absoluteResourcePath = module.split("!").pop();
|
|
|
|
hash = getHash(identifier);
|
2014-07-18 19:31:50 +08:00
|
|
|
} else {
|
2016-01-19 08:52:28 +08:00
|
|
|
shortIdentifier = module.readableIdentifier(requestShortener);
|
|
|
|
identifier = requestShortener.shorten(module.identifier());
|
|
|
|
moduleId = module.id;
|
2017-03-18 20:38:47 +08:00
|
|
|
absoluteResourcePath = module.identifier().split("!").pop();
|
2016-01-19 08:52:28 +08:00
|
|
|
hash = getHash(identifier);
|
2014-07-18 19:31:50 +08:00
|
|
|
}
|
2017-02-23 01:00:31 +08:00
|
|
|
const resource = shortIdentifier.split("!").pop();
|
|
|
|
const loaders = getBefore(shortIdentifier, "!");
|
|
|
|
const allLoaders = getBefore(identifier, "!");
|
|
|
|
const query = getAfter(resource, "?");
|
|
|
|
const resourcePath = resource.substr(0, resource.length - query.length);
|
2017-10-19 06:24:59 +08:00
|
|
|
if(typeof opts.moduleFilenameTemplate === "function") {
|
|
|
|
return opts.moduleFilenameTemplate({
|
2015-07-07 20:39:48 +08:00
|
|
|
identifier: identifier,
|
2015-07-07 22:36:25 +08:00
|
|
|
shortIdentifier: shortIdentifier,
|
|
|
|
resource: resource,
|
|
|
|
resourcePath: resourcePath,
|
|
|
|
absoluteResourcePath: absoluteResourcePath,
|
|
|
|
allLoaders: allLoaders,
|
|
|
|
query: query,
|
|
|
|
moduleId: moduleId,
|
2017-10-18 06:48:04 +08:00
|
|
|
hash: hash,
|
2017-10-19 06:24:59 +08:00
|
|
|
namespace: opts.namespace
|
2015-07-07 20:39:48 +08:00
|
|
|
});
|
|
|
|
}
|
2017-10-19 06:24:59 +08:00
|
|
|
return opts.moduleFilenameTemplate
|
2014-07-18 19:31:50 +08:00
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, identifier)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE, shortIdentifier)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE, resource)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE_PATH, resourcePath)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH, absoluteResourcePath)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS, allLoaders)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_LOADERS, loaders)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_QUERY, query)
|
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_ID, moduleId)
|
2017-10-18 06:48:04 +08:00
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_HASH, hash)
|
2017-10-19 06:24:59 +08:00
|
|
|
.replace(ModuleFilenameHelpers.REGEXP_NAMESPACE, opts.namespace);
|
2014-07-18 19:31:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ModuleFilenameHelpers.createFooter = function createFooter(module, requestShortener) {
|
2015-10-28 18:34:03 +08:00
|
|
|
if(!module) module = "";
|
2015-07-16 06:19:23 +08:00
|
|
|
if(typeof module === "string") {
|
2014-07-18 19:31:50 +08:00
|
|
|
return [
|
2016-06-07 17:21:36 +08:00
|
|
|
"// WEBPACK FOOTER //",
|
2017-02-23 01:00:31 +08:00
|
|
|
`// ${requestShortener.shorten(module)}`
|
2014-07-18 19:31:50 +08:00
|
|
|
].join("\n");
|
|
|
|
} else {
|
|
|
|
return [
|
2016-06-07 17:21:36 +08:00
|
|
|
"//////////////////",
|
|
|
|
"// WEBPACK FOOTER",
|
2017-02-23 01:00:31 +08:00
|
|
|
`// ${module.readableIdentifier(requestShortener)}`,
|
|
|
|
`// module id = ${module.id}`,
|
2017-04-19 04:30:18 +08:00
|
|
|
`// module chunks = ${module.mapChunks(c => c.id).join(" ")}`
|
2014-07-18 19:31:50 +08:00
|
|
|
].join("\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-26 23:54:00 +08:00
|
|
|
ModuleFilenameHelpers.replaceDuplicates = function replaceDuplicates(array, fn, comparator) {
|
2017-02-23 17:41:42 +08:00
|
|
|
const countMap = Object.create(null);
|
|
|
|
const posMap = Object.create(null);
|
2017-02-23 01:00:31 +08:00
|
|
|
array.forEach((item, idx) => {
|
2014-07-26 23:54:00 +08:00
|
|
|
countMap[item] = (countMap[item] || []);
|
|
|
|
countMap[item].push(idx);
|
2014-07-18 19:31:50 +08:00
|
|
|
posMap[item] = 0;
|
|
|
|
});
|
2015-07-16 06:19:23 +08:00
|
|
|
if(comparator) {
|
2017-02-23 01:00:31 +08:00
|
|
|
Object.keys(countMap).forEach(item => {
|
2014-07-26 23:54:00 +08:00
|
|
|
countMap[item].sort(comparator);
|
|
|
|
});
|
|
|
|
}
|
2017-02-23 01:00:31 +08:00
|
|
|
return array.map((item, i) => {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(countMap[item].length > 1) {
|
|
|
|
if(comparator && countMap[item][0] === i)
|
2014-07-26 23:54:00 +08:00
|
|
|
return item;
|
2014-07-18 19:31:50 +08:00
|
|
|
return fn(item, i, posMap[item]++);
|
|
|
|
} else return item;
|
|
|
|
});
|
|
|
|
};
|
2015-03-21 02:00:39 +08:00
|
|
|
|
|
|
|
ModuleFilenameHelpers.matchPart = function matchPart(str, test) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(!test) return true;
|
2015-03-21 02:00:39 +08:00
|
|
|
test = asRegExp(test);
|
2015-07-16 06:19:23 +08:00
|
|
|
if(Array.isArray(test)) {
|
2015-03-21 02:00:39 +08:00
|
|
|
return test.map(asRegExp).filter(function(regExp) {
|
|
|
|
return regExp.test(str);
|
|
|
|
}).length > 0;
|
|
|
|
} else {
|
|
|
|
return test.test(str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ModuleFilenameHelpers.matchObject = function matchObject(obj, str) {
|
2015-07-16 06:19:23 +08:00
|
|
|
if(obj.test)
|
|
|
|
if(!ModuleFilenameHelpers.matchPart(str, obj.test)) return false;
|
|
|
|
if(obj.include)
|
|
|
|
if(!ModuleFilenameHelpers.matchPart(str, obj.include)) return false;
|
|
|
|
if(obj.exclude)
|
|
|
|
if(ModuleFilenameHelpers.matchPart(str, obj.exclude)) return false;
|
2015-03-21 02:00:39 +08:00
|
|
|
return true;
|
|
|
|
};
|
2017-10-18 06:48:04 +08:00
|
|
|
|
|
|
|
ModuleFilenameHelpers.setModuleNamespace = function setModuleNamespace(moduleNamespace) {
|
|
|
|
this._moduleNamespace = moduleNamespace;
|
|
|
|
};
|