mirror of https://github.com/webpack/webpack.git
mangle harmony export names
This commit is contained in:
parent
e52b7b2e4d
commit
8cb0ee7b85
|
@ -4,6 +4,8 @@
|
|||
*/
|
||||
var DependenciesBlock = require("./DependenciesBlock");
|
||||
var ModuleReason = require("./ModuleReason");
|
||||
var Template = require("./Template");
|
||||
var HarmonyModulesHelpers = require("./dependencies/HarmonyModulesHelpers");
|
||||
|
||||
var debugId = 1000;
|
||||
|
||||
|
@ -102,13 +104,17 @@ Module.prototype.rewriteChunkInReasons = function(oldChunk, newChunks) {
|
|||
};
|
||||
|
||||
Module.prototype.isUsed = function(exportName) {
|
||||
if(this.used === null) return true;
|
||||
if(!exportName) return this.used;
|
||||
if(this.used === null) return exportName;
|
||||
if(!exportName) return this.used ? true : false;
|
||||
if(!this.used) return false;
|
||||
if(!this.usedExports) return false;
|
||||
if(this.usedExports === true) return true;
|
||||
return this.usedExports.indexOf(exportName) >= 0;
|
||||
}
|
||||
if(this.usedExports === true) return exportName;
|
||||
var idx = this.usedExports.indexOf(exportName);
|
||||
if(idx < 0) return false;
|
||||
if(HarmonyModulesHelpers.isExportedByHarmony(this, exportName))
|
||||
return Template.numberToIdentifer(idx);
|
||||
return exportName;
|
||||
};
|
||||
|
||||
Module.prototype.toString = function() {
|
||||
return "Module[" + (this.id || this.debugId) + "]";
|
||||
|
|
|
@ -20,6 +20,18 @@ Template.toIdentifier = function(str) {
|
|||
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
|
||||
};
|
||||
|
||||
var A_CODE = "a".charCodeAt(0);
|
||||
var Z_CODE = "z".charCodeAt(0);
|
||||
var AZ_COUNT = Z_CODE - A_CODE;
|
||||
var A2_CODE = "A".charCodeAt(0);
|
||||
var Z2_CODE = "Z".charCodeAt(0);
|
||||
var AZ2_COUNT = Z2_CODE - A2_CODE;
|
||||
Template.numberToIdentifer = function numberToIdentifer(n) {
|
||||
if(n < AZ_COUNT) return String.fromCharCode(A_CODE + n);
|
||||
if(n < AZ_COUNT + AZ2_COUNT) return String.fromCharCode(A2_CODE + n + AZ_COUNT);
|
||||
return "_" + (n - AZ_COUNT - AZ2_COUNT);
|
||||
};
|
||||
|
||||
Template.prototype = Object.create(Tapable.prototype);
|
||||
Template.prototype.constructor = Template;
|
||||
|
||||
|
|
|
@ -16,13 +16,20 @@ HarmonyExportExpressionDependency.prototype = Object.create(NullDependency.proto
|
|||
HarmonyExportExpressionDependency.prototype.constructor = HarmonyExportExpressionDependency;
|
||||
HarmonyExportExpressionDependency.prototype.type = "harmony export expression";
|
||||
|
||||
HarmonyExportExpressionDependency.prototype.describeHarmonyExport = function() {
|
||||
return {
|
||||
exportedName: "default",
|
||||
precedence: 1
|
||||
};
|
||||
};
|
||||
|
||||
HarmonyExportExpressionDependency.Template = function HarmonyExportDependencyTemplate() {};
|
||||
|
||||
HarmonyExportExpressionDependency.Template.prototype.apply = function(dep, source) {
|
||||
var used = dep.originModule.isUsed("default");
|
||||
var content;
|
||||
if(used)
|
||||
content = "/* harmony default export */ exports[" + JSON.stringify("default") + "] = ";
|
||||
content = "/* harmony default export */ exports[" + JSON.stringify(used) + "] = ";
|
||||
else
|
||||
content = "/* unused harmony default export */ var _unused_webpack_default_export = ";
|
||||
if(dep.range) {
|
||||
|
|
|
@ -43,23 +43,39 @@ HarmonyExportImportedSpecifierDependency.Template = function HarmonyExportImport
|
|||
HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep, source, outputOptions, requestShortener) {
|
||||
var name = dep.importedVar;
|
||||
var used = dep.originModule.isUsed(dep.name);
|
||||
var importedModule = dep.importDependency.module;
|
||||
var active = HarmonyModulesHelpers.isActive(dep.originModule, dep);
|
||||
var comment = "";
|
||||
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
|
||||
var content;
|
||||
if(!used) {
|
||||
content = "/* unused harmony reexport " + (dep.name || "namespace") + " */";
|
||||
} else if(!active) {
|
||||
content = "/* inactive harmony reexport " + (dep.name || "namespace") + " */";
|
||||
} else if(dep.name === "default" && !(dep.importDependency.module.meta && dep.importDependency.module.meta.harmonyModule)) {
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + comment + name + "_default.a; }});";
|
||||
} else if(dep.name === "default" && !(importedModule && importedModule.meta && importedModule.meta.harmonyModule)) {
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + name + "_default.a; }});";
|
||||
} else if(dep.name && dep.id) {
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + comment + (name + "[" + JSON.stringify(dep.id) + "]") + "; }});";
|
||||
var idUsed = importedModule && importedModule.isUsed(dep.id);
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + (name + "[" + JSON.stringify(idUsed) + "]") + "; }});";
|
||||
} else if(dep.name) {
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + comment + name + "; }});";
|
||||
} else {
|
||||
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + name + "; }});";
|
||||
} else if(Array.isArray(dep.originModule.usedExports)) {
|
||||
var activeExports = HarmonyModulesHelpers.getActiveExports(dep.originModule);
|
||||
content = "/* harmony namespace reexport */ for(var __WEBPACK_IMPORT_KEY__ in " + comment + name + ") ";
|
||||
var items = dep.originModule.usedExports.map(function(id) {
|
||||
if(id === "default") return;
|
||||
if(activeExports.indexOf(id) >= 0) return;
|
||||
var exportUsed = dep.originModule.isUsed(id);
|
||||
var idUsed = importedModule && importedModule.isUsed(id);
|
||||
return [exportUsed, idUsed];
|
||||
}).filter(Boolean);
|
||||
if(items.length > 1) {
|
||||
content = "/* harmony namespace reexport */ " + JSON.stringify(items) + ".forEach(function(i) { " +
|
||||
"Object.defineProperty(exports, i[0], {configurable: false, enumerable: true, get: function() { return " + (name + "[i[1]]") + "; }});" +
|
||||
"});";
|
||||
} else if(items.length === 1) {
|
||||
content = "/* harmony namespace reexport */ Object.defineProperty(exports, " + JSON.stringify(items[0][0]) + ", {configurable: false, enumerable: true, get: function() { return " + (name + "[" + JSON.stringify(items[0][1]) + "]") + "; }});";
|
||||
} else content = "/* unused harmony namespace reexport */";
|
||||
} else if(dep.originModule.usedExports) {
|
||||
var activeExports = HarmonyModulesHelpers.getActiveExports(dep.originModule);
|
||||
content = "/* harmony namespace reexport */ for(var __WEBPACK_IMPORT_KEY__ in " + name + ") ";
|
||||
|
||||
// Filter out exports which are defined by other exports
|
||||
// and filter out default export because it cannot be reexported with *
|
||||
|
|
|
@ -37,9 +37,9 @@ HarmonyExportSpecifierDependency.Template.prototype.apply = function(dep, source
|
|||
} else if(!active) {
|
||||
content = "/* inactive harmony export " + (dep.name || "namespace") + " */";
|
||||
} else if(dep.immutable) {
|
||||
content = "/* harmony export */ exports[" + JSON.stringify(dep.name) + "] = " + dep.id + ";";
|
||||
content = "/* harmony export */ exports[" + JSON.stringify(used) + "] = " + dep.id + ";";
|
||||
} else {
|
||||
content = "/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
|
||||
content = "/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
|
||||
}
|
||||
source.insert(dep.position, content);
|
||||
|
||||
|
|
|
@ -33,7 +33,8 @@ HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source
|
|||
if(dep.id === "default" && !(dep.importDependency.module.meta && dep.importDependency.module.meta.harmonyModule)) {
|
||||
content = "/* harmony import */ " + dep.importedVar + "_default.a";
|
||||
} else if(dep.id) {
|
||||
content = "/* harmony import */ " + dep.importedVar + "[" + JSON.stringify(dep.id) + "]";
|
||||
var used = dep.importDependency.module.isUsed(dep.id);
|
||||
content = "/* harmony import */ " + dep.importedVar + "[" + JSON.stringify(used) + "]";
|
||||
} else {
|
||||
content = "/* harmony namespace import */ " + dep.importedVar;
|
||||
}
|
||||
|
|
|
@ -67,3 +67,14 @@ HarmonyModulesHelpers.getActiveExports = function(module) {
|
|||
}, [])
|
||||
};
|
||||
|
||||
HarmonyModulesHelpers.isExportedByHarmony = function(module, exportedName) {
|
||||
return module.dependencies.some(function(dep) {
|
||||
if(!dep.describeHarmonyExport) return false;
|
||||
var d = dep.describeHarmonyExport();
|
||||
if(!d) return false;
|
||||
var name = d.exportedName;
|
||||
if(!name) return true; // namespace export
|
||||
return name === exportedName; // direct export
|
||||
}, [])
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue