a zip code crypto-currency system good for red ONLY

DynamicEntryPlugin.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Naoyuki Kanezawa @nkzawa
  4. */
  5. "use strict";
  6. const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
  7. const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
  8. const MultiModuleFactory = require("./MultiModuleFactory");
  9. const MultiEntryPlugin = require("./MultiEntryPlugin");
  10. const SingleEntryPlugin = require("./SingleEntryPlugin");
  11. class DynamicEntryPlugin {
  12. constructor(context, entry) {
  13. this.context = context;
  14. this.entry = entry;
  15. }
  16. apply(compiler) {
  17. compiler.plugin("compilation", (compilation, params) => {
  18. const multiModuleFactory = new MultiModuleFactory();
  19. const normalModuleFactory = params.normalModuleFactory;
  20. compilation.dependencyFactories.set(MultiEntryDependency, multiModuleFactory);
  21. compilation.dependencyFactories.set(SingleEntryDependency, normalModuleFactory);
  22. });
  23. compiler.plugin("make", (compilation, callback) => {
  24. const addEntry = (entry, name) => {
  25. const dep = DynamicEntryPlugin.createDependency(entry, name);
  26. return new Promise((resolve, reject) => {
  27. compilation.addEntry(this.context, dep, name, (err) => {
  28. if(err) return reject(err);
  29. resolve();
  30. });
  31. });
  32. };
  33. Promise.resolve(this.entry()).then((entry) => {
  34. if(typeof entry === "string" || Array.isArray(entry)) {
  35. addEntry(entry, "main").then(() => callback(), callback);
  36. } else if(typeof entry === "object") {
  37. Promise.all(Object.keys(entry).map((name) => {
  38. return addEntry(entry[name], name);
  39. })).then(() => callback(), callback);
  40. }
  41. });
  42. });
  43. }
  44. }
  45. module.exports = DynamicEntryPlugin;
  46. DynamicEntryPlugin.createDependency = function(entry, name) {
  47. if(Array.isArray(entry))
  48. return MultiEntryPlugin.createDependency(entry, name);
  49. else
  50. return SingleEntryPlugin.createDependency(entry, name);
  51. };