webpack/lib/dependencies/HarmonyImportDependency.js

43 lines
1.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 ModuleDependency = require("./ModuleDependency");
function HarmonyImportDependency(request, importedVar, range) {
ModuleDependency.call(this, request);
this.Class = HarmonyImportDependency;
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
};
};
2015-01-13 00:45:30 +08:00
HarmonyImportDependency.Template = function HarmonyImportDependencyTemplate() {};
HarmonyImportDependency.Template.prototype.apply = function(dep, source, outputOptions, requestShortener) {
var comment = "";
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
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) {
content = "/* harmony import */ var " + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");\n";
2015-01-13 00:45:30 +08:00
} else {
content = "";
2015-01-13 00:45:30 +08:00
}
source.replace(dep.range[0], dep.range[1] - 1, "");
source.insert(0, content);
2015-01-13 00:45:30 +08:00
};