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.

116 lines
3.2 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 path = require("path");
  7. const ParserHelpers = require("./ParserHelpers");
  8. class CommonJsStuffPlugin {
  9. apply(compiler) {
  10. compiler.hooks.compilation.tap(
  11. "CommonJsStuffPlugin",
  12. (compilation, { normalModuleFactory }) => {
  13. const handler = (parser, parserOptions) => {
  14. parser.hooks.expression
  15. .for("require.main.require")
  16. .tap(
  17. "CommonJsStuffPlugin",
  18. ParserHelpers.expressionIsUnsupported(
  19. parser,
  20. "require.main.require is not supported by webpack."
  21. )
  22. );
  23. parser.hooks.expression
  24. .for("module.parent.require")
  25. .tap(
  26. "CommonJsStuffPlugin",
  27. ParserHelpers.expressionIsUnsupported(
  28. parser,
  29. "module.parent.require is not supported by webpack."
  30. )
  31. );
  32. parser.hooks.expression
  33. .for("require.main")
  34. .tap(
  35. "CommonJsStuffPlugin",
  36. ParserHelpers.toConstantDependencyWithWebpackRequire(
  37. parser,
  38. "__webpack_require__.c[__webpack_require__.s]"
  39. )
  40. );
  41. parser.hooks.expression
  42. .for("module.loaded")
  43. .tap("CommonJsStuffPlugin", expr => {
  44. parser.state.module.buildMeta.moduleConcatenationBailout =
  45. "module.loaded";
  46. return ParserHelpers.toConstantDependency(
  47. parser,
  48. "module.l"
  49. )(expr);
  50. });
  51. parser.hooks.expression
  52. .for("module.id")
  53. .tap("CommonJsStuffPlugin", expr => {
  54. parser.state.module.buildMeta.moduleConcatenationBailout =
  55. "module.id";
  56. return ParserHelpers.toConstantDependency(
  57. parser,
  58. "module.i"
  59. )(expr);
  60. });
  61. parser.hooks.expression
  62. .for("module.exports")
  63. .tap("CommonJsStuffPlugin", () => {
  64. const module = parser.state.module;
  65. const isHarmony =
  66. module.buildMeta && module.buildMeta.exportsType;
  67. if (!isHarmony) return true;
  68. });
  69. parser.hooks.evaluateIdentifier
  70. .for("module.hot")
  71. .tap(
  72. "CommonJsStuffPlugin",
  73. ParserHelpers.evaluateToIdentifier("module.hot", false)
  74. );
  75. parser.hooks.expression
  76. .for("module")
  77. .tap("CommonJsStuffPlugin", () => {
  78. const module = parser.state.module;
  79. const isHarmony =
  80. module.buildMeta && module.buildMeta.exportsType;
  81. let moduleJsPath = path.join(
  82. __dirname,
  83. "..",
  84. "buildin",
  85. isHarmony ? "harmony-module.js" : "module.js"
  86. );
  87. if (module.context) {
  88. moduleJsPath = path.relative(
  89. parser.state.module.context,
  90. moduleJsPath
  91. );
  92. if (!/^[A-Z]:/i.test(moduleJsPath)) {
  93. moduleJsPath = `./${moduleJsPath.replace(/\\/g, "/")}`;
  94. }
  95. }
  96. return ParserHelpers.addParsedVariableToModule(
  97. parser,
  98. "module",
  99. `require(${JSON.stringify(moduleJsPath)})(module)`
  100. );
  101. });
  102. };
  103. normalModuleFactory.hooks.parser
  104. .for("javascript/auto")
  105. .tap("CommonJsStuffPlugin", handler);
  106. normalModuleFactory.hooks.parser
  107. .for("javascript/dynamic")
  108. .tap("CommonJsStuffPlugin", handler);
  109. }
  110. );
  111. }
  112. }
  113. module.exports = CommonJsStuffPlugin;