HarmonyImportDependency.js
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const ModuleDependency = require("./ModuleDependency");
class HarmonyImportDependency extends ModuleDependency {
constructor(request, importedVar, range) {
super(request);
this.range = range;
this.importedVar = importedVar;
}
get type() {
return "harmony import";
}
getReference() {
if(!this.module) return null;
return {
module: this.module,
importedNames: false
};
}
updateHash(hash) {
super.updateHash(hash);
hash.update((this.module && (!this.module.meta || this.module.meta.harmonyModule)) + "");
}
}
HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate {
apply(dep, source, outputOptions, requestShortener) {
const content = makeImportStatement(true, dep, outputOptions, requestShortener);
source.replace(dep.range[0], dep.range[1] - 1, "");
source.insert(-1, content);
}
};
function getOptionalComment(pathinfo, shortenedRequest) {
if(!pathinfo) {
return "";
}
return `/*! ${shortenedRequest} */ `;
}
function makeImportStatement(declare, dep, outputOptions, requestShortener) {
const comment = getOptionalComment(outputOptions.pathinfo, requestShortener.shorten(dep.request));
const declaration = declare ? "var " : "";
const newline = declare ? "\n" : " ";
if(!dep.module) {
const stringifiedError = JSON.stringify(`Cannot find module "${dep.request}"`);
return `throw new Error(${stringifiedError});${newline}`;
}
if(dep.importedVar) {
const isHarmonyModule = dep.module.meta && dep.module.meta.harmonyModule;
const content = `/* harmony import */ ${declaration}${dep.importedVar} = __webpack_require__(${comment}${JSON.stringify(dep.module.id)});${newline}`;
if(isHarmonyModule) {
return content;
}
return `${content}/* harmony import */ ${declaration}${dep.importedVar}_default = __webpack_require__.n(${dep.importedVar});${newline}`;
}
return "";
}
HarmonyImportDependency.makeImportStatement = makeImportStatement;
module.exports = HarmonyImportDependency;