webpack/lib/DependenciesBlockVariable.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
2013-01-31 01:49:25 +08:00
const ReplaceSource = require("webpack-sources").ReplaceSource;
const RawSource = require("webpack-sources").RawSource;
class DependenciesBlockVariable {
constructor(name, expression, dependencies) {
this.name = name;
this.expression = expression;
this.dependencies = dependencies || [];
}
2013-01-31 01:49:25 +08:00
updateHash(hash) {
hash.update(this.name);
hash.update(this.expression);
2018-01-22 20:52:43 +08:00
for(const d of this.dependencies) {
d.updateHash(hash);
2018-01-22 20:52:43 +08:00
}
}
2013-01-31 01:49:25 +08:00
expressionSource(dependencyTemplates, runtimeTemplate) {
const source = new ReplaceSource(new RawSource(this.expression));
2018-01-22 20:52:43 +08:00
for(const dep of this.dependencies) {
const template = dependencyTemplates.get(dep.constructor);
if(!template) throw new Error(`No template for dependency: ${dep.constructor.name}`);
template.apply(dep, source, runtimeTemplate, dependencyTemplates);
2018-01-22 20:52:43 +08:00
}
return source;
}
2013-01-31 01:49:25 +08:00
disconnect() {
2018-01-22 20:52:43 +08:00
for(const d of this.dependencies) {
d.disconnect();
2018-01-22 20:52:43 +08:00
}
}
hasDependencies(filter) {
if(filter) {
if(this.dependencies.some(filter)) return true;
} else {
if(this.dependencies.length > 0) return true;
}
return false;
}
}
module.exports = DependenciesBlockVariable;