أنماط الإضافات (Plugins)

تتيح الإضافات إمكانية إجراء تعديلات غير محدودة على نظام بناء Webpack. وهذا يسمح بإنشاء أنواع أصول مخصصة (custom asset)، وإجراء تعديلات فريدة على عملية البناء، أو حتى تحسين بيئة تشغيل Webpack باستخدام البرمجيات الوسيطة. فيما يلي بعض ميزات Webpack التي تُصبح مفيدة عند كتابة الإضافات.

استكشاف الـ assets، والـ chunks ، والـ modules، والـ dependencies

بمجرد إغلاق عملية التجميع، يمكن التنقل عبر جميع الهياكل الموجودة داخله.

class MyPlugin {
  apply(compiler) {
    compiler.hooks.emit.tapAsync("MyPlugin", (compilation, callback) => {
      // Explore each chunk (build output):
      for (const chunk of compilation.chunks) {
        // Explore each module within the chunk (built inputs):
        for (const module of chunk.getModules()) {
          // Explore each source file path that was included into the module:
          if (module.buildInfo && module.buildInfo.fileDependencies) {
            for (const fileDependency of module.buildInfo.fileDependencies) {
              // we've learned a lot about the source structure now...
              console.log(fileDependency);
            }
          }
        }

        // Explore each asset filename generated by the chunk:
        for (const filename of chunk.files) {
          // Get the asset source for each file generated by the chunk:
          const source = compilation.assets[filename].source();
        }
      }

      callback();
    });
  }
}

export default MyPlugin;
  • compilation.modules: مجموعة من الـ modules (built inputs) في عملية التجميع. تتولى كل module إدارة عملية بناء ملف خام من source library الخاصة بك.
  • module.fileDependencies: مصفوفة (array) بمسارات ملفات المصدر المُضمّنة في الـ module. يشمل ذلك ملف جافا سكريبت المصدر نفسه (مثال: index.js)، وجميع ملفات الأصول التابعة (stylesheets, images, الخ..) التي تتطلبها. تُفيد مراجعة التبعيات في معرفة ملفات المصدر التي تنتمي إلى الوحدة.

  • compilation.chunks: مجموعة من الأجزاء (build outputs) في عملية التجميع. يُدير كل جزء منها عملية تكوين الـ assets النهائية المُعالجة.

  • chunk.getModules(): مصفوفة (array) من الـ Modules المضمنة في الـ Chunk. وبالتالي، يمكنك البحث في تبعيات كل module لتعلم ما هي الـ source files الأولية التي يتم تغذيتها في الـ Chunk ما.
  • chunk.files: مجموعة من أسماء ملفات الـ output التي تم إنشاؤها بواسطة الـ chunk. يمكنك الوصول إلى مصادر الـ asset هذه من الجدول compilation.assets.

Monitoring the watch graph

أثناء تشغيل برنامج وسيط Webpack، تتضمن كل عملية تجميع مجموعة fileDependencies (التي تحدد الملفات التي تتم مراقبتها) والـ fileTimestamps Map التي تربط مسارات الملفات التي تتم مراقبتها بالطابع الزمني. تُعد هذه المجموعات مفيدة للغاية لاكتشاف الملفات التي تغيرت أثناء عملية التجميع.

class MyPlugin {
  constructor() {
    this.startTime = Date.now();
    this.prevTimestamps = new Map();
  }

  apply(compiler) {
    compiler.hooks.emit.tapAsync("MyPlugin", (compilation, callback) => {
      const changedFiles = [...compilation.fileTimestamps.keys()].filter(
        (watchfile) =>
          (this.prevTimestamps.get(watchfile) || this.startTime) <
          (compilation.fileTimestamps.get(watchfile) || Infinity),
      );

      this.prevTimestamps = compilation.fileTimestamps;
      callback();
    });
  }
}

export default MyPlugin;

يمكنك أيضًا إدخال مسارات ملفات جديدة في الـ watch graph لتلقي إشعارات التجميع عند تغيير هذه الملفات. أضف مسارات الملفات الصالحة إلى compilation.fileDependencies Set لإضافتها إلى الـ watched files.

Changed chunks

على غرار الـ watch graph، يمكنك مراقبة الـ chunks المتغيرة داخل الـ compilation من خلال تتبع الـ content hashes الخاصة بها.

class MyPlugin {
  constructor() {
    this.chunkVersions = {};
  }

  apply(compiler) {
    compiler.hooks.emit.tapAsync("MyPlugin", (compilation, callback) => {
      const changedChunks = [];

      for (const chunk of compilation.chunks) {
        const key = chunk.id ?? chunk.name;
        const currentHash = chunk.contentHash.javascript;

        if (currentHash === undefined) continue;

        const oldHash = this.chunkVersions[key];
        this.chunkVersions[key] = currentHash;

        if (currentHash !== oldHash) {
          changedChunks.push(chunk);
        }
      }

      if (changedChunks.length > 0) {
        console.log(
          "Changed chunks:",
          changedChunks.map((chunk) => chunk.id),
        );
      }

      callback();
    });
  }
}

export default MyPlugin;
Edit this page·

1 Contributor

semon009