You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ParserHelpers = require("./ParserHelpers");
  7. const ConstDependency = require("./dependencies/ConstDependency");
  8. const NullFactory = require("./NullFactory");
  9. module.exports = class RequireJsStuffPlugin {
  10. apply(compiler) {
  11. compiler.hooks.compilation.tap(
  12. "RequireJsStuffPlugin",
  13. (compilation, { normalModuleFactory }) => {
  14. compilation.dependencyFactories.set(ConstDependency, new NullFactory());
  15. compilation.dependencyTemplates.set(
  16. ConstDependency,
  17. new ConstDependency.Template()
  18. );
  19. const handler = (parser, parserOptions) => {
  20. if (parserOptions.requireJs !== undefined && !parserOptions.requireJs)
  21. return;
  22. parser.hooks.call
  23. .for("require.config")
  24. .tap(
  25. "RequireJsStuffPlugin",
  26. ParserHelpers.toConstantDependency(parser, "undefined")
  27. );
  28. parser.hooks.call
  29. .for("requirejs.config")
  30. .tap(
  31. "RequireJsStuffPlugin",
  32. ParserHelpers.toConstantDependency(parser, "undefined")
  33. );
  34. parser.hooks.expression
  35. .for("require.version")
  36. .tap(
  37. "RequireJsStuffPlugin",
  38. ParserHelpers.toConstantDependency(
  39. parser,
  40. JSON.stringify("0.0.0")
  41. )
  42. );
  43. parser.hooks.expression
  44. .for("requirejs.onError")
  45. .tap(
  46. "RequireJsStuffPlugin",
  47. ParserHelpers.toConstantDependencyWithWebpackRequire(
  48. parser,
  49. "__webpack_require__.oe"
  50. )
  51. );
  52. };
  53. normalModuleFactory.hooks.parser
  54. .for("javascript/auto")
  55. .tap("RequireJsStuffPlugin", handler);
  56. normalModuleFactory.hooks.parser
  57. .for("javascript/dynamic")
  58. .tap("RequireJsStuffPlugin", handler);
  59. }
  60. );
  61. }
  62. };