SupportMiniCssExtractPlugin.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const pluginCompat = require('./util/plugin-compat');
  2. const mixinCssDependency = function(CssDependency) {
  3. const Dependency = Object.getPrototypeOf(CssDependency.prototype).constructor;
  4. CssDependency.prototype.updateHash = function(hash) {
  5. Dependency.prototype.updateHash.call(this, hash);
  6. hash.update(this.content);
  7. };
  8. };
  9. class SupportMiniCssExtractPlugin {
  10. apply(compiler) {
  11. let CssDependency;
  12. pluginCompat.tap(
  13. compiler,
  14. 'make',
  15. 'SupportMiniCssExtractPlugin',
  16. ({ dependencyFactories }) => {
  17. const Dependencies = dependencyFactories.keys();
  18. for (const Dep of Dependencies) {
  19. if (Dep.name === 'CssDependency') {
  20. CssDependency = Dep;
  21. mixinCssDependency(CssDependency);
  22. break;
  23. }
  24. }
  25. },
  26. );
  27. pluginCompat.tap(
  28. compiler,
  29. '_hardSourceFreezeDependency',
  30. 'HardMiniCssExtractPlugin freeze',
  31. (frozen, dependency, extra) => {
  32. if (dependency.constructor === CssDependency) {
  33. return {
  34. type: 'CssDependency',
  35. line: {
  36. identifier: dependency.identifier,
  37. content: dependency.content,
  38. media: dependency.media,
  39. sourceMap: dependency.sourceMap,
  40. },
  41. context: dependency.context,
  42. identifierIndex: dependency.identifierIndex,
  43. };
  44. }
  45. return frozen;
  46. },
  47. );
  48. pluginCompat.tap(
  49. compiler,
  50. '_hardSourceThawDependency',
  51. 'HardMiniCssExtractPlugin',
  52. (dependency, { type, line, context, identifierIndex }, extra) => {
  53. if (type === 'CssDependency') {
  54. return new CssDependency(line, context, identifierIndex);
  55. }
  56. return dependency;
  57. },
  58. );
  59. }
  60. }
  61. module.exports = SupportMiniCssExtractPlugin;