SystemPlugin.js 3.9 KB

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