RequireJsStuffPlugin.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("./RuntimeGlobals");
  7. const ConstDependency = require("./dependencies/ConstDependency");
  8. const {
  9. toConstantDependency
  10. } = require("./javascript/JavascriptParserHelpers");
  11. /** @typedef {import("./Compiler")} Compiler */
  12. module.exports = class RequireJsStuffPlugin {
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.compilation.tap(
  20. "RequireJsStuffPlugin",
  21. (compilation, { normalModuleFactory }) => {
  22. compilation.dependencyTemplates.set(
  23. ConstDependency,
  24. new ConstDependency.Template()
  25. );
  26. const handler = (parser, parserOptions) => {
  27. if (
  28. parserOptions.requireJs === undefined ||
  29. !parserOptions.requireJs
  30. ) {
  31. return;
  32. }
  33. parser.hooks.call
  34. .for("require.config")
  35. .tap(
  36. "RequireJsStuffPlugin",
  37. toConstantDependency(parser, "undefined")
  38. );
  39. parser.hooks.call
  40. .for("requirejs.config")
  41. .tap(
  42. "RequireJsStuffPlugin",
  43. toConstantDependency(parser, "undefined")
  44. );
  45. parser.hooks.expression
  46. .for("require.version")
  47. .tap(
  48. "RequireJsStuffPlugin",
  49. toConstantDependency(parser, JSON.stringify("0.0.0"))
  50. );
  51. parser.hooks.expression
  52. .for("requirejs.onError")
  53. .tap(
  54. "RequireJsStuffPlugin",
  55. toConstantDependency(
  56. parser,
  57. RuntimeGlobals.uncaughtErrorHandler,
  58. [RuntimeGlobals.uncaughtErrorHandler]
  59. )
  60. );
  61. };
  62. normalModuleFactory.hooks.parser
  63. .for("javascript/auto")
  64. .tap("RequireJsStuffPlugin", handler);
  65. normalModuleFactory.hooks.parser
  66. .for("javascript/dynamic")
  67. .tap("RequireJsStuffPlugin", handler);
  68. }
  69. );
  70. }
  71. };