webpack/lib/dependencies/HarmonyImportSpecifierDepen...

62 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-01-13 00:45:30 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var NullDependency = require("./NullDependency");
function HarmonyImportSpecifierDependency(importDependency, importedVar, id, name, range) {
2015-01-13 00:45:30 +08:00
NullDependency.call(this);
this.importDependency = importDependency;
2015-01-13 00:45:30 +08:00
this.importedVar = importedVar;
this.id = id;
this.name = name;
this.range = range;
}
module.exports = HarmonyImportSpecifierDependency;
HarmonyImportSpecifierDependency.prototype = Object.create(NullDependency.prototype);
2015-10-18 16:44:51 +08:00
HarmonyImportSpecifierDependency.prototype.constructor = HarmonyImportSpecifierDependency;
2015-01-13 00:45:30 +08:00
HarmonyImportSpecifierDependency.prototype.type = "harmony import specifier";
HarmonyImportSpecifierDependency.prototype.getReference = function() {
if(!this.importDependency.module) return null;
return {
module: this.importDependency.module,
importedNames: this.id ? [this.id] : true
};
}
2016-02-04 07:06:16 +08:00
HarmonyImportSpecifierDependency.prototype.updateHash = function(hash) {
NullDependency.prototype.updateHash.call(this, hash);
2016-02-04 16:59:28 +08:00
var importedModule = this.importDependency.module;
hash.update((importedModule && importedModule.id) + "");
hash.update((importedModule && this.id) + "");
hash.update((importedModule && this.importedVar) + "");
hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
hash.update((importedModule && importedModule.meta && importedModule.meta.harmonyModule) + "");
2016-02-04 16:59:28 +08:00
hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
2016-02-04 07:06:16 +08:00
};
2015-01-13 00:45:30 +08:00
HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDependencyTemplate() {};
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
var content;
2016-02-16 23:56:03 +08:00
var importedModule = dep.importDependency.module;
2016-06-04 20:19:20 +08:00
var defaultImport = dep.id === "default" && !(importedModule && importedModule.meta && importedModule.meta.harmonyModule);
if(defaultImport) {
content = dep.importedVar + "_default.a";
} else if(dep.id) {
2016-02-16 23:56:03 +08:00
var used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
2016-06-04 20:19:20 +08:00
content = dep.importedVar + "[" + JSON.stringify(used) + (dep.id !== used ? " /* " + dep.id + " */" : "") + "]";
2015-01-13 00:45:30 +08:00
} else {
2016-06-04 20:19:20 +08:00
content = dep.importedVar;
2015-01-13 00:45:30 +08:00
}
2016-05-14 20:33:04 +08:00
if(!dep.call) {
source.replace(dep.range[0], dep.range[1] - 1, content);
2016-06-04 20:19:20 +08:00
} else if(defaultImport) {
source.replace(dep.range[0], dep.range[1] - 1, dep.importedVar + "_default()");
2016-05-14 20:33:04 +08:00
} else {
2016-06-04 20:19:20 +08:00
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__.i(" + content + ")");
}
2015-01-13 00:45:30 +08:00
};