SystemPlugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ParserHelpers = require("../ParserHelpers");
  7. const WebpackError = require("../WebpackError");
  8. class SystemPlugin {
  9. constructor(options) {
  10. this.options = options;
  11. }
  12. apply(compiler) {
  13. compiler.hooks.compilation.tap(
  14. "SystemPlugin",
  15. (compilation, { normalModuleFactory }) => {
  16. const handler = (parser, parserOptions) => {
  17. if (
  18. typeof parserOptions.system !== "undefined" &&
  19. !parserOptions.system
  20. )
  21. return;
  22. const shouldWarn = typeof parserOptions.system === "undefined";
  23. const setNotSupported = name => {
  24. parser.hooks.evaluateTypeof
  25. .for(name)
  26. .tap("SystemPlugin", ParserHelpers.evaluateToString("undefined"));
  27. parser.hooks.expression
  28. .for(name)
  29. .tap(
  30. "SystemPlugin",
  31. ParserHelpers.expressionIsUnsupported(
  32. parser,
  33. name + " is not supported by webpack."
  34. )
  35. );
  36. };
  37. parser.hooks.typeof
  38. .for("System.import")
  39. .tap(
  40. "SystemPlugin",
  41. ParserHelpers.toConstantDependency(
  42. parser,
  43. JSON.stringify("function")
  44. )
  45. );
  46. parser.hooks.evaluateTypeof
  47. .for("System.import")
  48. .tap("SystemPlugin", ParserHelpers.evaluateToString("function"));
  49. parser.hooks.typeof
  50. .for("System")
  51. .tap(
  52. "SystemPlugin",
  53. ParserHelpers.toConstantDependency(
  54. parser,
  55. JSON.stringify("object")
  56. )
  57. );
  58. parser.hooks.evaluateTypeof
  59. .for("System")
  60. .tap("SystemPlugin", ParserHelpers.evaluateToString("object"));
  61. setNotSupported("System.set");
  62. setNotSupported("System.get");
  63. setNotSupported("System.register");
  64. parser.hooks.expression.for("System").tap("SystemPlugin", () => {
  65. const systemPolyfillRequire = ParserHelpers.requireFileAsExpression(
  66. parser.state.module.context,
  67. require.resolve("../../buildin/system")
  68. );
  69. return ParserHelpers.addParsedVariableToModule(
  70. parser,
  71. "System",
  72. systemPolyfillRequire
  73. );
  74. });
  75. parser.hooks.call.for("System.import").tap("SystemPlugin", expr => {
  76. if (shouldWarn) {
  77. parser.state.module.warnings.push(
  78. new SystemImportDeprecationWarning(
  79. parser.state.module,
  80. expr.loc
  81. )
  82. );
  83. }
  84. return parser.hooks.importCall.call(expr);
  85. });
  86. };
  87. normalModuleFactory.hooks.parser
  88. .for("javascript/auto")
  89. .tap("SystemPlugin", handler);
  90. normalModuleFactory.hooks.parser
  91. .for("javascript/dynamic")
  92. .tap("SystemPlugin", handler);
  93. }
  94. );
  95. }
  96. }
  97. class SystemImportDeprecationWarning extends WebpackError {
  98. constructor(module, loc) {
  99. super(
  100. "System.import() is deprecated and will be removed soon. Use import() instead.\n" +
  101. "For more info visit https://webpack.js.org/guides/code-splitting/"
  102. );
  103. this.name = "SystemImportDeprecationWarning";
  104. this.module = module;
  105. this.loc = loc;
  106. Error.captureStackTrace(this, this.constructor);
  107. }
  108. }
  109. module.exports = SystemPlugin;