LibraryTemplatePlugin.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) {
  25. this.name = name;
  26. this.target = target;
  27. this.umdNamedDefine = umdNamedDefine;
  28. this.auxiliaryComment = auxiliaryComment;
  29. }
  30. apply(compiler) {
  31. compiler.plugin("this-compilation", (compilation) => {
  32. switch(this.target) {
  33. case "var":
  34. compilation.apply(new SetVarMainTemplatePlugin(`var ${accessorAccess(false, this.name)}`));
  35. break;
  36. case "assign":
  37. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name)));
  38. break;
  39. case "this":
  40. case "window":
  41. case "global":
  42. if(this.name)
  43. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name)));
  44. else
  45. compilation.apply(new SetVarMainTemplatePlugin(this.target, true));
  46. break;
  47. case "commonjs":
  48. if(this.name)
  49. compilation.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name)));
  50. else
  51. compilation.apply(new SetVarMainTemplatePlugin("exports", true));
  52. break;
  53. case "commonjs2":
  54. case "commonjs-module":
  55. compilation.apply(new SetVarMainTemplatePlugin("module.exports"));
  56. break;
  57. case "amd":
  58. var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
  59. compilation.apply(new AmdMainTemplatePlugin(this.name));
  60. break;
  61. case "umd":
  62. case "umd2":
  63. var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
  64. compilation.apply(new UmdMainTemplatePlugin(this.name, {
  65. optionalAmdExternalAsGlobal: this.target === "umd2",
  66. namedDefine: this.umdNamedDefine,
  67. auxiliaryComment: this.auxiliaryComment
  68. }));
  69. break;
  70. case "jsonp":
  71. var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin");
  72. compilation.apply(new JsonpExportMainTemplatePlugin(this.name));
  73. break;
  74. default:
  75. throw new Error(`${this.target} is not a valid Library target`);
  76. }
  77. });
  78. }
  79. }
  80. module.exports = LibraryTemplatePlugin;