HarmonyDetectionParserPlugin.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
  7. module.exports = class HarmonyDetectionParserPlugin {
  8. apply(parser) {
  9. parser.plugin("program", (ast) => {
  10. const isHarmony = ast.body.some(statement => {
  11. return /^(Import|Export).*Declaration$/.test(statement.type);
  12. });
  13. if(isHarmony) {
  14. const module = parser.state.module;
  15. const dep = new HarmonyCompatibilityDependency(module);
  16. dep.loc = {
  17. start: {
  18. line: -1,
  19. column: 0
  20. },
  21. end: {
  22. line: -1,
  23. column: 0
  24. },
  25. index: -2
  26. };
  27. module.addDependency(dep);
  28. module.meta.harmonyModule = true;
  29. module.strict = true;
  30. module.exportsArgument = "__webpack_exports__";
  31. }
  32. });
  33. const nonHarmonyIdentifiers = ["define", "exports"];
  34. nonHarmonyIdentifiers.forEach(identifer => {
  35. parser.plugin(`evaluate typeof ${identifer}`, nullInHarmony);
  36. parser.plugin(`typeof ${identifer}`, skipInHarmony);
  37. parser.plugin(`evaluate ${identifer}`, nullInHarmony);
  38. parser.plugin(`expression ${identifer}`, skipInHarmony);
  39. parser.plugin(`call ${identifer}`, skipInHarmony);
  40. });
  41. function skipInHarmony() {
  42. const module = this.state.module;
  43. if(module && module.meta && module.meta.harmonyModule)
  44. return true;
  45. }
  46. function nullInHarmony() {
  47. const module = this.state.module;
  48. if(module && module.meta && module.meta.harmonyModule)
  49. return null;
  50. }
  51. }
  52. };