webpack/lib/FlagDependencyExportsPlugin.js

95 lines
2.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class FlagDependencyExportsPlugin {
apply(compiler) {
compiler.plugin("compilation", (compilation) => {
compilation.plugin("finish-modules", (modules) => {
2017-01-26 06:23:20 +08:00
const dependencies = Object.create(null);
let module;
let moduleWithExports;
const queue = modules.filter((m) => !m.providedExports);
for(let i = 0; i < queue.length; i++) {
module = queue[i];
if(module.providedExports !== true) {
moduleWithExports = false;
processDependenciesBlock(module);
if(!moduleWithExports) {
module.providedExports = true;
notifyDependencies();
}
}
}
function processDependenciesBlock(depBlock) {
depBlock.dependencies.forEach((dep) => processDependency(dep));
depBlock.variables.forEach((variable) => {
variable.dependencies.forEach((dep) => processDependency(dep));
});
2017-01-26 06:23:20 +08:00
depBlock.blocks.forEach(processDependenciesBlock);
}
function processDependency(dep, usedExports) {
const exportDesc = dep.getExports && dep.getExports();
if(!exportDesc) return;
moduleWithExports = true;
const exports = exportDesc.exports;
const exportDeps = exportDesc.dependencies;
if(exportDeps) {
exportDeps.forEach((dep) => {
const depIdent = dep.identifier();
const array = dependencies[depIdent] = dependencies[depIdent] || [];
if(array.indexOf(module) < 0)
array.push(module);
});
}
let changed = false;
if(module.providedExports !== true) {
if(exports === true) {
module.providedExports = true;
2016-09-09 20:20:29 +08:00
changed = true;
} else if(Array.isArray(exports)) {
if(Array.isArray(module.providedExports)) {
changed = addToSet(module.providedExports, exports);
} else {
module.providedExports = exports.slice();
changed = true;
}
2016-09-09 20:20:29 +08:00
}
2016-11-21 08:00:25 +08:00
}
if(changed) {
notifyDependencies();
}
2016-09-09 20:20:29 +08:00
}
2016-09-09 20:50:14 +08:00
function notifyDependencies() {
2017-01-26 06:23:20 +08:00
const deps = dependencies[module.identifier()];
if(deps) {
deps.forEach((dep) => queue.push(dep));
}
}
});
function addToSet(a, b) {
let changed = false;
b.forEach((item) => {
if(a.indexOf(item) < 0) {
a.push(item);
changed = true;
}
});
return changed;
}
});
}
}
module.exports = FlagDependencyExportsPlugin;