Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ExtendedAPIPlugin.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("./Template");
  7. const ConstDependency = require("./dependencies/ConstDependency");
  8. const ParserHelpers = require("./ParserHelpers");
  9. const NullFactory = require("./NullFactory");
  10. const REPLACEMENTS = {
  11. // eslint-disable-next-line camelcase
  12. __webpack_hash__: "__webpack_require__.h",
  13. // eslint-disable-next-line camelcase
  14. __webpack_chunkname__: "__webpack_require__.cn"
  15. };
  16. const REPLACEMENT_TYPES = {
  17. // eslint-disable-next-line camelcase
  18. __webpack_hash__: "string",
  19. // eslint-disable-next-line camelcase
  20. __webpack_chunkname__: "string"
  21. };
  22. class ExtendedAPIPlugin {
  23. apply(compiler) {
  24. compiler.hooks.compilation.tap(
  25. "ExtendedAPIPlugin",
  26. (compilation, { normalModuleFactory }) => {
  27. compilation.dependencyFactories.set(ConstDependency, new NullFactory());
  28. compilation.dependencyTemplates.set(
  29. ConstDependency,
  30. new ConstDependency.Template()
  31. );
  32. const mainTemplate = compilation.mainTemplate;
  33. mainTemplate.hooks.requireExtensions.tap(
  34. "ExtendedAPIPlugin",
  35. (source, chunk, hash) => {
  36. const buf = [source];
  37. buf.push("");
  38. buf.push("// __webpack_hash__");
  39. buf.push(`${mainTemplate.requireFn}.h = ${JSON.stringify(hash)};`);
  40. buf.push("");
  41. buf.push("// __webpack_chunkname__");
  42. buf.push(
  43. `${mainTemplate.requireFn}.cn = ${JSON.stringify(chunk.name)};`
  44. );
  45. return Template.asString(buf);
  46. }
  47. );
  48. mainTemplate.hooks.globalHash.tap("ExtendedAPIPlugin", () => true);
  49. const handler = (parser, parserOptions) => {
  50. Object.keys(REPLACEMENTS).forEach(key => {
  51. parser.hooks.expression
  52. .for(key)
  53. .tap(
  54. "ExtendedAPIPlugin",
  55. ParserHelpers.toConstantDependencyWithWebpackRequire(
  56. parser,
  57. REPLACEMENTS[key]
  58. )
  59. );
  60. parser.hooks.evaluateTypeof
  61. .for(key)
  62. .tap(
  63. "ExtendedAPIPlugin",
  64. ParserHelpers.evaluateToString(REPLACEMENT_TYPES[key])
  65. );
  66. });
  67. };
  68. normalModuleFactory.hooks.parser
  69. .for("javascript/auto")
  70. .tap("ExtendedAPIPlugin", handler);
  71. normalModuleFactory.hooks.parser
  72. .for("javascript/dynamic")
  73. .tap("ExtendedAPIPlugin", handler);
  74. normalModuleFactory.hooks.parser
  75. .for("javascript/esm")
  76. .tap("ExtendedAPIPlugin", handler);
  77. }
  78. );
  79. }
  80. }
  81. module.exports = ExtendedAPIPlugin;