TransformBasicDependencyPlugin.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const pluginCompat = require('./util/plugin-compat');
  2. function freezeDependency(dependency, extra, methods) {
  3. if (extra.schemas.map.has(dependency.constructor)) {
  4. return extra.schemas.map
  5. .get(dependency.constructor)
  6. .freeze(dependency, dependency, extra, methods);
  7. }
  8. if (extra.schemas[dependency.constructor.name]) {
  9. return extra.schemas[dependency.constructor.name].freeze(
  10. dependency,
  11. dependency,
  12. extra,
  13. methods,
  14. );
  15. }
  16. }
  17. function thawDependency(frozen, extra, methods) {
  18. if (extra.schemas[frozen.type]) {
  19. return extra.schemas[frozen.type].thaw(null, frozen, extra, methods);
  20. }
  21. }
  22. class TransformBasicDependencyPlugin {
  23. constructor(options) {
  24. this.options = options;
  25. }
  26. apply(compiler) {
  27. if (this.options.schema < 4) {
  28. const TransformBasicDependencyPluginLegacy = require('./TransformBasicDependencyPluginLegacy');
  29. new TransformBasicDependencyPluginLegacy(this.options).apply(compiler);
  30. } else {
  31. const schemas = require('./schema-4');
  32. let methods;
  33. pluginCompat.tap(
  34. compiler,
  35. '_hardSourceMethods',
  36. 'TransformBasicDependencyPlugin methods',
  37. _methods => {
  38. methods = _methods;
  39. },
  40. );
  41. pluginCompat.tap(
  42. compiler,
  43. '_hardSourceFreezeDependency',
  44. 'TransformBasicDependencyPlugin freeze',
  45. (frozen, dependency, extra) => {
  46. extra.schemas = schemas;
  47. const _frozen = freezeDependency(dependency, extra, methods);
  48. if (_frozen) {
  49. return _frozen;
  50. }
  51. return frozen;
  52. },
  53. );
  54. pluginCompat.tap(
  55. compiler,
  56. '_hardSourceThawDependency',
  57. 'TransformBasicDependencyPlugin',
  58. (dependency, frozen, extra) => {
  59. extra.schemas = schemas;
  60. const _thawed = thawDependency(frozen, extra, methods);
  61. if (_thawed) {
  62. return _thawed;
  63. }
  64. return dependency;
  65. },
  66. );
  67. }
  68. }
  69. }
  70. module.exports = TransformBasicDependencyPlugin;