SystemPlugin.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  21. /** @typedef {import("../Compiler")} Compiler */
  22. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  23. /** @typedef {import("../javascript/JavascriptParser")} Parser */
  24. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  25. const PLUGIN_NAME = "SystemPlugin";
  26. class SystemPlugin {
  27. /**
  28. * Apply the plugin
  29. * @param {Compiler} compiler the compiler instance
  30. * @returns {void}
  31. */
  32. apply(compiler) {
  33. compiler.hooks.compilation.tap(
  34. PLUGIN_NAME,
  35. (compilation, { normalModuleFactory }) => {
  36. compilation.hooks.runtimeRequirementInModule
  37. .for(RuntimeGlobals.system)
  38. .tap(PLUGIN_NAME, (module, set) => {
  39. set.add(RuntimeGlobals.requireScope);
  40. });
  41. compilation.hooks.runtimeRequirementInTree
  42. .for(RuntimeGlobals.system)
  43. .tap(PLUGIN_NAME, (chunk, set) => {
  44. compilation.addRuntimeModule(chunk, new SystemRuntimeModule());
  45. });
  46. /**
  47. * @param {Parser} parser parser parser
  48. * @param {JavascriptParserOptions} parserOptions parserOptions
  49. * @returns {void}
  50. */
  51. const handler = (parser, parserOptions) => {
  52. if (parserOptions.system === undefined || !parserOptions.system) {
  53. return;
  54. }
  55. /**
  56. * @param {string} name name
  57. */
  58. const setNotSupported = name => {
  59. parser.hooks.evaluateTypeof
  60. .for(name)
  61. .tap(PLUGIN_NAME, evaluateToString("undefined"));
  62. parser.hooks.expression
  63. .for(name)
  64. .tap(
  65. PLUGIN_NAME,
  66. expressionIsUnsupported(
  67. parser,
  68. name + " is not supported by webpack."
  69. )
  70. );
  71. };
  72. parser.hooks.typeof
  73. .for("System.import")
  74. .tap(
  75. PLUGIN_NAME,
  76. toConstantDependency(parser, JSON.stringify("function"))
  77. );
  78. parser.hooks.evaluateTypeof
  79. .for("System.import")
  80. .tap(PLUGIN_NAME, evaluateToString("function"));
  81. parser.hooks.typeof
  82. .for("System")
  83. .tap(
  84. PLUGIN_NAME,
  85. toConstantDependency(parser, JSON.stringify("object"))
  86. );
  87. parser.hooks.evaluateTypeof
  88. .for("System")
  89. .tap(PLUGIN_NAME, evaluateToString("object"));
  90. setNotSupported("System.set");
  91. setNotSupported("System.get");
  92. setNotSupported("System.register");
  93. parser.hooks.expression.for("System").tap(PLUGIN_NAME, expr => {
  94. const dep = new ConstDependency(
  95. RuntimeGlobals.system,
  96. /** @type {Range} */ (expr.range),
  97. [RuntimeGlobals.system]
  98. );
  99. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  100. parser.state.module.addPresentationalDependency(dep);
  101. return true;
  102. });
  103. parser.hooks.call.for("System.import").tap(PLUGIN_NAME, expr => {
  104. parser.state.module.addWarning(
  105. new SystemImportDeprecationWarning(
  106. /** @type {DependencyLocation} */ (expr.loc)
  107. )
  108. );
  109. return parser.hooks.importCall.call({
  110. type: "ImportExpression",
  111. source:
  112. /** @type {import("estree").Literal} */
  113. (expr.arguments[0]),
  114. loc: expr.loc,
  115. range: expr.range
  116. });
  117. });
  118. };
  119. normalModuleFactory.hooks.parser
  120. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  121. .tap(PLUGIN_NAME, handler);
  122. normalModuleFactory.hooks.parser
  123. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  124. .tap(PLUGIN_NAME, handler);
  125. }
  126. );
  127. }
  128. }
  129. class SystemImportDeprecationWarning extends WebpackError {
  130. /**
  131. * @param {DependencyLocation} loc location
  132. */
  133. constructor(loc) {
  134. super(
  135. "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
  136. "For more info visit https://webpack.js.org/guides/code-splitting/"
  137. );
  138. this.name = "SystemImportDeprecationWarning";
  139. this.loc = loc;
  140. }
  141. }
  142. makeSerializable(
  143. SystemImportDeprecationWarning,
  144. "webpack/lib/dependencies/SystemPlugin",
  145. "SystemImportDeprecationWarning"
  146. );
  147. module.exports = SystemPlugin;
  148. module.exports.SystemImportDeprecationWarning = SystemImportDeprecationWarning;