DependenciesBlockVariable.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ReplaceSource = require("webpack-sources").ReplaceSource;
  7. const RawSource = require("webpack-sources").RawSource;
  8. class DependenciesBlockVariable {
  9. constructor(name, expression, dependencies) {
  10. this.name = name;
  11. this.expression = expression;
  12. this.dependencies = dependencies || [];
  13. }
  14. updateHash(hash) {
  15. hash.update(this.name);
  16. hash.update(this.expression);
  17. this.dependencies.forEach(d => {
  18. d.updateHash(hash);
  19. });
  20. }
  21. expressionSource(dependencyTemplates, outputOptions, requestShortener) {
  22. const source = new ReplaceSource(new RawSource(this.expression));
  23. this.dependencies.forEach(dep => {
  24. const template = dependencyTemplates.get(dep.constructor);
  25. if(!template) throw new Error(`No template for dependency: ${dep.constructor.name}`);
  26. template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
  27. });
  28. return source;
  29. }
  30. disconnect() {
  31. this.dependencies.forEach(d => {
  32. d.disconnect();
  33. });
  34. }
  35. hasDependencies(filter) {
  36. if(filter) {
  37. if(this.dependencies.some(filter)) return true;
  38. } else {
  39. if(this.dependencies.length > 0) return true;
  40. }
  41. return false;
  42. }
  43. }
  44. module.exports = DependenciesBlockVariable;