Skip to content

Commit 44fb71a

Browse files
authored
Merge pull request webpack#3877 from shubheksha/refactor-FlagDependencyExportsPlugin
refactor(ES6): upgrade FlagDependencyExportsPlugin to ES6
2 parents 22ab0ea + 4f052fe commit 44fb71a

1 file changed

Lines changed: 72 additions & 79 deletions

File tree

lib/FlagDependencyExportsPlugin.js

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,94 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function FlagDependencyExportsPlugin() {
5+
"use strict";
66

7-
}
8-
module.exports = FlagDependencyExportsPlugin;
7+
class FlagDependencyExportsPlugin {
98

10-
FlagDependencyExportsPlugin.prototype.apply = function(compiler) {
11-
compiler.plugin("compilation", function(compilation) {
12-
compilation.plugin("finish-modules", function(modules) {
9+
apply(compiler) {
10+
compiler.plugin("compilation", (compilation) => {
11+
compilation.plugin("finish-modules", (modules) => {
1312

14-
var dependencies = {};
13+
const dependencies = Object.create(null);
1514

16-
var module, moduleWithExports;
17-
var queue = modules.filter(function(m) {
18-
return !m.providedExports;
19-
});
20-
for(var i = 0; i < queue.length; i++) {
21-
module = queue[i];
15+
let module;
16+
let moduleWithExports;
17+
const queue = modules.filter((m) => !m.providedExports);
18+
for(let i = 0; i < queue.length; i++) {
19+
module = queue[i];
2220

23-
if(module.providedExports !== true) {
24-
moduleWithExports = false;
25-
processDependenciesBlock(module);
26-
if(!moduleWithExports) {
27-
module.providedExports = true;
28-
notifyDependencies();
21+
if(module.providedExports !== true) {
22+
moduleWithExports = false;
23+
processDependenciesBlock(module);
24+
if(!moduleWithExports) {
25+
module.providedExports = true;
26+
notifyDependencies();
27+
}
2928
}
3029
}
31-
}
3230

33-
function processDependenciesBlock(depBlock) {
34-
depBlock.dependencies.forEach(function(dep) {
35-
processDependency(dep);
36-
});
37-
depBlock.variables.forEach(function(variable) {
38-
variable.dependencies.forEach(function(dep) {
39-
processDependency(dep);
40-
});
41-
});
42-
depBlock.blocks.forEach(function(block) {
43-
processDependenciesBlock(block);
44-
});
45-
}
46-
47-
function processDependency(dep, usedExports) {
48-
var exportDesc = dep.getExports && dep.getExports();
49-
if(!exportDesc) return;
50-
moduleWithExports = true;
51-
var exports = exportDesc.exports;
52-
var exportDeps = exportDesc.dependencies;
53-
if(exportDeps) {
54-
exportDeps.forEach(function(dep) {
55-
var depIdent = dep.identifier();
56-
var array = dependencies["$" + depIdent];
57-
if(!array) array = dependencies["$" + depIdent] = [];
58-
if(array.indexOf(module) < 0)
59-
array.push(module);
31+
function processDependenciesBlock(depBlock) {
32+
depBlock.dependencies.forEach((dep) => processDependency(dep));
33+
depBlock.variables.forEach((variable) => {
34+
variable.dependencies.forEach((dep) => processDependency(dep));
6035
});
36+
depBlock.blocks.forEach(processDependenciesBlock);
6137
}
62-
var changed = false;
63-
if(module.providedExports !== true) {
64-
if(exports === true) {
65-
module.providedExports = true;
66-
changed = true;
67-
} else if(Array.isArray(exports)) {
68-
if(Array.isArray(module.providedExports)) {
69-
changed = addToSet(module.providedExports, exports);
70-
} else {
71-
module.providedExports = exports.slice();
38+
39+
function processDependency(dep, usedExports) {
40+
const exportDesc = dep.getExports && dep.getExports();
41+
if(!exportDesc) return;
42+
moduleWithExports = true;
43+
const exports = exportDesc.exports;
44+
const exportDeps = exportDesc.dependencies;
45+
if(exportDeps) {
46+
exportDeps.forEach((dep) => {
47+
const depIdent = dep.identifier();
48+
let array = dependencies[depIdent];
49+
if(!array) array = dependencies[depIdent] = [];
50+
if(array.indexOf(module) < 0)
51+
array.push(module);
52+
});
53+
}
54+
let changed = false;
55+
if(module.providedExports !== true) {
56+
if(exports === true) {
57+
module.providedExports = true;
7258
changed = true;
59+
} else if(Array.isArray(exports)) {
60+
if(Array.isArray(module.providedExports)) {
61+
changed = addToSet(module.providedExports, exports);
62+
} else {
63+
module.providedExports = exports.slice();
64+
changed = true;
65+
}
7366
}
7467
}
68+
if(changed) {
69+
notifyDependencies();
70+
}
7571
}
76-
if(changed) {
77-
notifyDependencies();
78-
}
79-
}
8072

81-
function notifyDependencies() {
82-
var deps = dependencies["$" + module.identifier()];
83-
if(deps) {
84-
deps.forEach(function(dep) {
85-
queue.push(dep);
86-
});
73+
function notifyDependencies() {
74+
const deps = dependencies[module.identifier()];
75+
if(deps) {
76+
deps.forEach((dep) => queue.push(dep));
77+
}
8778
}
79+
});
80+
81+
function addToSet(a, b) {
82+
let changed = false;
83+
b.forEach((item) => {
84+
if(a.indexOf(item) < 0) {
85+
a.push(item);
86+
changed = true;
87+
}
88+
});
89+
return changed;
8890
}
8991
});
92+
}
93+
}
9094

91-
function addToSet(a, b) {
92-
var changed = false;
93-
b.forEach(function(item) {
94-
if(a.indexOf(item) < 0) {
95-
a.push(item);
96-
changed = true;
97-
}
98-
});
99-
return changed;
100-
}
101-
});
102-
};
95+
module.exports = FlagDependencyExportsPlugin;

0 commit comments

Comments
 (0)