2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-06 01:36:43 +08:00
|
|
|
"use strict";
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-01-06 01:36:43 +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
|
|
|
|
2017-01-06 01:36:43 +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) {
|
2017-01-06 01:36:43 +08:00
|
|
|
d.updateHash(hash);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-01-06 01:36:43 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-12-07 17:31:00 +08:00
|
|
|
expressionSource(dependencyTemplates, runtimeTemplate) {
|
2017-01-06 01:36:43 +08:00
|
|
|
const source = new ReplaceSource(new RawSource(this.expression));
|
2018-01-22 20:52:43 +08:00
|
|
|
for(const dep of this.dependencies) {
|
2017-01-06 01:36:43 +08:00
|
|
|
const template = dependencyTemplates.get(dep.constructor);
|
|
|
|
if(!template) throw new Error(`No template for dependency: ${dep.constructor.name}`);
|
2017-12-07 17:31:00 +08:00
|
|
|
template.apply(dep, source, runtimeTemplate, dependencyTemplates);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-01-06 01:36:43 +08:00
|
|
|
return source;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-01-06 01:36:43 +08:00
|
|
|
disconnect() {
|
2018-01-22 20:52:43 +08:00
|
|
|
for(const d of this.dependencies) {
|
2017-01-06 01:36:43 +08:00
|
|
|
d.disconnect();
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-01-06 01:36:43 +08:00
|
|
|
}
|
2015-06-01 05:28:24 +08:00
|
|
|
|
2017-01-10 00:11:34 +08:00
|
|
|
hasDependencies(filter) {
|
|
|
|
if(filter) {
|
|
|
|
if(this.dependencies.some(filter)) return true;
|
|
|
|
} else {
|
|
|
|
if(this.dependencies.length > 0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-01-06 01:36:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DependenciesBlockVariable;
|