webpack/lib/dependencies/HarmonyImportDependency.js

57 lines
2.2 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);
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 " : "";
2016-06-05 01:08:34 +08:00
var newline = declare ? "\n" : " ";
var content;
2015-01-13 00:45:30 +08:00
if(!dep.module) {
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");" + newline;
2015-01-13 00:45:30 +08:00
} else if(dep.importedVar) {
content = "/* harmony import */ " + declaration + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");" + newline;
2016-01-07 06:50:12 +08:00
if(!(dep.module.meta && dep.module.meta.harmonyModule)) {
content += "/* harmony import */ " + declaration + dep.importedVar + "_default = __webpack_require__.n(" + dep.importedVar + ");" + newline;
}
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, "");
2016-02-22 22:55:31 +08:00
source.insert(-1, content);
2015-01-13 00:45:30 +08:00
};