Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SystemPlugin.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;