UI for Zipcoin Blue

LibraryTemplatePlugin.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
  7. function accessorToObjectAccess(accessor) {
  8. return accessor.map((a) => {
  9. return `[${JSON.stringify(a)}]`;
  10. }).join("");
  11. }
  12. function accessorAccess(base, accessor, joinWith) {
  13. accessor = [].concat(accessor);
  14. return accessor.map((a, idx) => {
  15. a = base ?
  16. base + accessorToObjectAccess(accessor.slice(0, idx + 1)) :
  17. accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1));
  18. if(idx === accessor.length - 1) return a;
  19. if(idx === 0 && typeof base === "undefined") return `${a} = typeof ${a} === "object" ? ${a} : {}`;
  20. return `${a} = ${a} || {}`;
  21. }).join(joinWith || "; ");
  22. }
  23. class LibraryTemplatePlugin {
  24. constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
  25. this.name = name;
  26. this.target = target;
  27. this.umdNamedDefine = umdNamedDefine;
  28. this.auxiliaryComment = auxiliaryComment;
  29. this.exportProperty = exportProperty;
  30. }
  31. apply(compiler) {
  32. compiler.plugin("this-compilation", (compilation) => {
  33. if(this.exportProperty) {
  34. var ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
  35. compilation.apply(new ExportPropertyMainTemplatePlugin(this.exportProperty));
  36. }
  37. switch(this.target) {
  38. case "var":
  39. compilation.apply(new SetVarMainTemplatePlugin(`var ${accessorAccess(false, this.name)}`));
  40. break;
  41. case "assign":
  42. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name)));
  43. break;
  44. case "this":
  45. case "window":
  46. case "global":
  47. if(this.name)
  48. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name)));
  49. else
  50. compilation.apply(new SetVarMainTemplatePlugin(this.target, true));
  51. break;
  52. case "commonjs":
  53. if(this.name)
  54. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name)));
  55. else
  56. compilation.apply(new SetVarMainTemplatePlugin("exports", true));
  57. break;
  58. case "commonjs2":
  59. case "commonjs-module":
  60. compilation.apply(new SetVarMainTemplatePlugin("module.exports"));
  61. break;
  62. case "amd":
  63. var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
  64. compilation.apply(new AmdMainTemplatePlugin(this.name));
  65. break;
  66. case "umd":
  67. case "umd2":
  68. var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
  69. compilation.apply(new UmdMainTemplatePlugin(this.name, {
  70. optionalAmdExternalAsGlobal: this.target === "umd2",
  71. namedDefine: this.umdNamedDefine,
  72. auxiliaryComment: this.auxiliaryComment
  73. }));
  74. break;
  75. case "jsonp":
  76. var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin");
  77. compilation.apply(new JsonpExportMainTemplatePlugin(this.name));
  78. break;
  79. default:
  80. throw new Error(`${this.target} is not a valid Library target`);
  81. }
  82. });
  83. }
  84. }
  85. module.exports = LibraryTemplatePlugin;