Skip to content

Commit 8fd2957

Browse files
timseTheLarkInn
authored andcommitted
refactor HarmonyImportSpecifierDependency to es6 (webpack#3734)
1 parent 61251de commit 8fd2957

1 file changed

Lines changed: 98 additions & 68 deletions

File tree

‎lib/dependencies/HarmonyImportSpecifierDependency.js‎

Lines changed: 98 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,109 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var NullDependency = require("./NullDependency");
6-
7-
function HarmonyImportSpecifierDependency(importDependency, importedVar, id, name, range) {
8-
NullDependency.call(this);
9-
this.importDependency = importDependency;
10-
this.importedVar = importedVar;
11-
this.id = id;
12-
this.name = name;
13-
this.range = range;
14-
}
15-
module.exports = HarmonyImportSpecifierDependency;
5+
"use strict";
6+
const NullDependency = require("./NullDependency");
167

17-
HarmonyImportSpecifierDependency.prototype = Object.create(NullDependency.prototype);
18-
HarmonyImportSpecifierDependency.prototype.constructor = HarmonyImportSpecifierDependency;
19-
HarmonyImportSpecifierDependency.prototype.type = "harmony import specifier";
20-
21-
HarmonyImportSpecifierDependency.prototype.getReference = function() {
22-
if(!this.importDependency.module) return null;
23-
return {
24-
module: this.importDependency.module,
25-
importedNames: this.id ? [this.id] : true
26-
};
27-
};
28-
29-
HarmonyImportSpecifierDependency.prototype.getWarnings = function() {
30-
var importedModule = this.importDependency.module;
31-
if(importedModule && importedModule.meta && importedModule.meta.harmonyModule) {
32-
if(this.id && importedModule.isProvided(this.id) === false) {
33-
var err = new Error("export '" + this.id + "'" +
34-
(this.id !== this.name ? " (imported as '" + this.name + "')" : "") +
35-
" was not found in '" + this.importDependency.userRequest + "'");
36-
err.hideStack = true;
37-
return [
38-
err
39-
];
8+
class HarmonyImportSpecifierDependency extends NullDependency {
9+
constructor(importDependency, importedVar, id, name, range) {
10+
super();
11+
this.importDependency = importDependency;
12+
this.importedVar = importedVar;
13+
this.id = id;
14+
this.name = name;
15+
this.range = range;
16+
}
17+
18+
get type() {
19+
return "harmony import specifier";
20+
}
21+
22+
getReference() {
23+
if(!this.importDependency.module) return null;
24+
return {
25+
module: this.importDependency.module,
26+
importedNames: this.id ? [this.id] : true
27+
};
28+
}
29+
30+
getWarnings() {
31+
const importedModule = this.importDependency.module;
32+
if(!importedModule || !importedModule.meta || !importedModule.meta.harmonyModule) {
33+
return;
4034
}
35+
36+
if(!this.id) {
37+
return;
38+
}
39+
40+
if(importedModule.isProvided(this.id) !== false) {
41+
return;
42+
}
43+
44+
const idIsNotNameMessage = this.id !== this.name ? ` (imported as '${this.name}')` : "";
45+
const errorMessage = `"export '${this.id}'${idIsNotNameMessage} was not found in '${this.importDependency.userRequest}'`;
46+
const err = new Error(errorMessage);
47+
err.hideStack = true;
48+
return [err];
49+
}
50+
51+
updateHash(hash) {
52+
super.updateHash(hash);
53+
const importedModule = this.importDependency.module;
54+
hash.update((importedModule && importedModule.id) + "");
55+
hash.update((importedModule && this.id) + "");
56+
hash.update((importedModule && this.importedVar) + "");
57+
hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
58+
hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
59+
hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
60+
}
61+
}
62+
63+
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate {
64+
apply(dep, source) {
65+
const content = this.getContent(dep);
66+
source.replace(dep.range[0], dep.range[1] - 1, content);
4167
}
42-
};
43-
44-
HarmonyImportSpecifierDependency.prototype.updateHash = function(hash) {
45-
NullDependency.prototype.updateHash.call(this, hash);
46-
var importedModule = this.importDependency.module;
47-
hash.update((importedModule && importedModule.id) + "");
48-
hash.update((importedModule && this.id) + "");
49-
hash.update((importedModule && this.importedVar) + "");
50-
hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
51-
hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
52-
hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
53-
};
54-
55-
HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDependencyTemplate() {};
56-
57-
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
58-
var content;
59-
var importedModule = dep.importDependency.module;
60-
var defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
61-
if(defaultImport) {
62-
content = dep.importedVar + "_default.a";
63-
} else if(dep.id) {
64-
var used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
65-
content = dep.importedVar + "[" + JSON.stringify(used) + (dep.id !== used ? " /* " + dep.id + " */" : "") + "]";
66-
} else {
67-
content = dep.importedVar;
68+
69+
getContent(dep) {
70+
const importedModule = dep.importDependency.module;
71+
const defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
72+
const shortHandPrefix = this.getShortHandPrefix(dep);
73+
const importedVar = dep.importedVar;
74+
const importedVarSuffix = this.getImportVarSuffix(dep, defaultImport, importedModule);
75+
76+
if(dep.call && defaultImport) {
77+
return `${shortHandPrefix}${importedVar}_default()`;
78+
}
79+
80+
if(dep.call && dep.id) {
81+
return `${shortHandPrefix}__webpack_require__.i(${importedVar}${importedVarSuffix})`
82+
}
83+
84+
return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
6885
}
69-
if(dep.call) {
86+
87+
getImportVarSuffix(dep, defaultImport, importedModule) {
7088
if(defaultImport) {
71-
content = dep.importedVar + "_default()";
72-
} else if(dep.id) {
73-
content = "__webpack_require__.i(" + content + ")";
89+
return "_default.a";
7490
}
91+
92+
if(dep.id) {
93+
const used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
94+
const optionalComment = dep.id !== used ? " /* " + dep.id + " */" : "";
95+
return `[${JSON.stringify(used)}${optionalComment}]`;
96+
}
97+
98+
return "";
7599
}
76-
if(dep.shorthand) {
77-
content = dep.name + ": " + content;
100+
101+
getShortHandPrefix(dep) {
102+
if(!dep.shorthand) {
103+
return "";
104+
}
105+
106+
return dep.name + ": ";
78107
}
79-
source.replace(dep.range[0], dep.range[1] - 1, content);
80-
};
108+
}
109+
110+
module.exports = HarmonyImportSpecifierDependency;

0 commit comments

Comments
 (0)