SystemPlugin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 WebpackError = require("../WebpackError");
  8. const {
  9. evaluateToString,
  10. expressionIsUnsupported,
  11. toConstantDependency
  12. } = require("../javascript/JavascriptParserHelpers");
  13. const makeSerializable = require("../util/makeSerializable");
  14. const ConstDependency = require("./ConstDependency");
  15. const SystemRuntimeModule = require("./SystemRuntimeModule");
  16. /** @typedef {import("../Compiler")} Compiler */
  17. class SystemPlugin {
  18. /**
  19. * Apply the plugin
  20. * @param {Compiler} compiler the compiler instance
  21. * @returns {void}
  22. */
  23. apply(compiler) {
  24. compiler.hooks.compilation.tap(
  25. "SystemPlugin",
  26. (compilation, { normalModuleFactory }) => {
  27. compilation.hooks.runtimeRequirementInModule
  28. .for(RuntimeGlobals.system)
  29. .tap("SystemPlugin", (module, set) => {
  30. set.add(RuntimeGlobals.requireScope);
  31. });
  32. compilation.hooks.runtimeRequirementInTree
  33. .for(RuntimeGlobals.system)
  34. .tap("SystemPlugin", (chunk, set) => {
  35. compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
  36. });
  37. const handler = (parser, parserOptions) => {
  38. if (parserOptions.system === undefined || !parserOptions.system) {
  39. return;
  40. }
  41. const setNotSupported = name => {
  42. parser.hooks.evaluateTypeof
  43. .for(name)
  44. .tap("SystemPlugin", evaluateToString("undefined"));
  45. parser.hooks.expression
  46. .for(name)
  47. .tap(
  48. "SystemPlugin",
  49. expressionIsUnsupported(
  50. parser,
  51. name + " is not supported by webpack."
  52. )
  53. );
  54. };
  55. parser.hooks.typeof
  56. .for("System.import")
  57. .tap(
  58. "SystemPlugin",
  59. toConstantDependency(parser, JSON.stringify("function"))
  60. );
  61. parser.hooks.evaluateTypeof
  62. .for("System.import")
  63. .tap("SystemPlugin", evaluateToString("function"));
  64. parser.hooks.typeof
  65. .for("System")
  66. .tap(
  67. "SystemPlugin",
  68. toConstantDependency(parser, JSON.stringify("object"))
  69. );
  70. parser.hooks.evaluateTypeof
  71. .for("System")
  72. .tap("SystemPlugin", evaluateToString("object"));
  73. setNotSupported("System.set");
  74. setNotSupported("System.get");
  75. setNotSupported("System.register");
  76. parser.hooks.expression.for("System").tap("SystemPlugin", expr => {
  77. const dep = new ConstDependency(RuntimeGlobals.system, expr.range, [
  78. RuntimeGlobals.system
  79. ]);
  80. dep.loc = expr.loc;
  81. parser.state.module.addPresentationalDependency(dep);
  82. return true;
  83. });
  84. parser.hooks.call.for("System.import").tap("SystemPlugin", expr => {
  85. parser.state.module.addWarning(
  86. new SystemImportDeprecationWarning(expr.loc)
  87. );
  88. return parser.hooks.importCall.call({
  89. type: "ImportExpression",
  90. source: expr.arguments[0],
  91. loc: expr.loc,
  92. range: expr.range
  93. });
  94. });
  95. };
  96. normalModuleFactory.hooks.parser
  97. .for("javascript/auto")
  98. .tap("SystemPlugin", handler);
  99. normalModuleFactory.hooks.parser
  100. .for("javascript/dynamic")
  101. .tap("SystemPlugin", handler);
  102. }
  103. );
  104. }
  105. }
  106. class SystemImportDeprecationWarning extends WebpackError {
  107. constructor(loc) {
  108. super(
  109. "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
  110. "For more info visit https://webpack.js.org/guides/code-splitting/"
  111. );
  112. this.name = "SystemImportDeprecationWarning";
  113. this.loc = loc;
  114. }
  115. }
  116. makeSerializable(
  117. SystemImportDeprecationWarning,
  118. "webpack/lib/dependencies/SystemPlugin",
  119. "SystemImportDeprecationWarning"
  120. );
  121. module.exports = SystemPlugin;
  122. module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;