webpack/lib/dependencies/HarmonyImportDependency.js

57 lines
2.4 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 ModuleDependency = require("./ModuleDependency");
function HarmonyImportDependency(request, importedVar, range) {
ModuleDependency.call(this, request);
this.range = range;
this.importedVar = importedVar;
}
module.exports = HarmonyImportDependency;
HarmonyImportDependency.prototype = Object.create(ModuleDependency.prototype);
2015-10-18 16:44:51 +08:00
HarmonyImportDependency.prototype.constructor = HarmonyImportDependency;
2015-01-13 00:45:30 +08:00
HarmonyImportDependency.prototype.type = "harmony import";
HarmonyImportDependency.prototype.getReference = function() {
if(!this.module) return null;
return {
module: this.module,
importedNames: false
};
};
HarmonyImportDependency.prototype.updateHash = function updateHash(hash) {
ModuleDependency.prototype.updateHash.call(this, hash);
2016-02-17 21:50:44 +08:00
hash.update((this.module && this.module.meta && this.module.meta.harmonyModule) + "");
};
2015-11-02 06:27:53 +08:00
HarmonyImportDependency.makeStatement = function(declare, dep, outputOptions, requestShortener) {
2015-01-13 00:45:30 +08:00
var comment = "";
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
2015-11-02 06:27:53 +08:00
var declaration = declare ? "var " : "";
var content;
2015-01-13 00:45:30 +08:00
if(!dep.module) {
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");\n";
2015-01-13 00:45:30 +08:00
} else if(dep.importedVar) {
2015-11-02 06:27:53 +08:00
content = "/* harmony import */ " + declaration + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");\n";
2016-01-07 06:50:12 +08:00
if(!(dep.module.meta && dep.module.meta.harmonyModule)) {
content += "/* harmony import */ " + declaration + dep.importedVar + "_default = " + dep.importedVar + " && " + dep.importedVar + ".__esModule ? function() { return " + dep.importedVar + "['default'] } : function() { return " + dep.importedVar + "; }\n";
content += "/* harmony import */ Object.defineProperty(" + dep.importedVar + "_default, 'a', { get: " + dep.importedVar + "_default });\n";
}
2015-01-13 00:45:30 +08:00
} else {
content = "";
2015-01-13 00:45:30 +08:00
}
2015-11-02 06:27:53 +08:00
return content;
};
2015-11-02 06:27:53 +08:00
HarmonyImportDependency.Template = function HarmonyImportDependencyTemplate() {};
HarmonyImportDependency.Template.prototype.apply = function(dep, source, outputOptions, requestShortener) {
var content = HarmonyImportDependency.makeStatement(true, dep, outputOptions, requestShortener);
source.replace(dep.range[0], dep.range[1] - 1, "");
source.insert(0, content);
2015-01-13 00:45:30 +08:00
};