您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CommonJsStuffPlugin.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(parser, "module.l")(
  47. expr
  48. );
  49. });
  50. parser.hooks.expression
  51. .for("module.id")
  52. .tap("CommonJsStuffPlugin", expr => {
  53. parser.state.module.buildMeta.moduleConcatenationBailout =
  54. "module.id";
  55. return ParserHelpers.toConstantDependency(parser, "module.i")(
  56. expr
  57. );
  58. });
  59. parser.hooks.expression
  60. .for("module.exports")
  61. .tap("CommonJsStuffPlugin", () => {
  62. const module = parser.state.module;
  63. const isHarmony =
  64. module.buildMeta && module.buildMeta.exportsType;
  65. if (!isHarmony) return true;
  66. });
  67. parser.hooks.evaluateIdentifier
  68. .for("module.hot")
  69. .tap(
  70. "CommonJsStuffPlugin",
  71. ParserHelpers.evaluateToIdentifier("module.hot", false)
  72. );
  73. parser.hooks.expression
  74. .for("module")
  75. .tap("CommonJsStuffPlugin", () => {
  76. const module = parser.state.module;
  77. const isHarmony =
  78. module.buildMeta && module.buildMeta.exportsType;
  79. let moduleJsPath = path.join(
  80. __dirname,
  81. "..",
  82. "buildin",
  83. isHarmony ? "harmony-module.js" : "module.js"
  84. );
  85. if (module.context) {
  86. moduleJsPath = path.relative(
  87. parser.state.module.context,
  88. moduleJsPath
  89. );
  90. if (!/^[A-Z]:/i.test(moduleJsPath)) {
  91. moduleJsPath = `./${moduleJsPath.replace(/\\/g, "/")}`;
  92. }
  93. }
  94. return ParserHelpers.addParsedVariableToModule(
  95. parser,
  96. "module",
  97. `require(${JSON.stringify(moduleJsPath)})(module)`
  98. );
  99. });
  100. };
  101. normalModuleFactory.hooks.parser
  102. .for("javascript/auto")
  103. .tap("CommonJsStuffPlugin", handler);
  104. normalModuleFactory.hooks.parser
  105. .for("javascript/dynamic")
  106. .tap("CommonJsStuffPlugin", handler);
  107. }
  108. );
  109. }
  110. }
  111. module.exports = CommonJsStuffPlugin;