You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

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