# All files / src/webpack/plugins/synced-files-plugin index.js

45.71% Statements 16/35
17.65% Branches 6/34
57.14% Functions 4/7
51.61% Lines 16/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    673x
    3x

    1x
    1x
    1x
    1x
    1x

    4x

    1x

    1x

    1x

    1x
    1x

    1x
    1x

    3x
     const path = require("path");
    const fs = require("fs");

    class SyncedFilesPlugin {

      constructor(options = {}) {
        this.outputFileName = options.outputFileName || "synced-files.json";
        this.outputPath = options.outputPath || "";
        this.deleteAfterCompile = !!options.deleteAfterCompile;
        this.hooks = {};
        this.syncedFiles = {};
      }

      static loader(requestedLoaders) {
        return [{
          loader: require.resolve("./loader"),
          options: {
            requestedLoaders
          }
        }];
      }
      apply(compiler) {
        compiler.hooks.beforeRun.tap({name: "LoadPreviouslySyncedFilesMap"}, () => {
          const outputPath = this.outputPath || compiler.outputPath;
          try {
            const outputFile = path.join(path.resolve(outputPath), this.outputFileName);
            if (!path.resolve(outputPath) || !fs.existsSync(outputFile)) return;
            this.syncedFiles = JSON.parse(fs.readFileSync(outputFile, "utf-8"));

          } catch(ex) {/* Ignore error! */}
        });

        compiler.hooks.done.tap({name: "ExtractEmittedFileMap"}, (webpackStatsObj) => {

          Iif (webpackStatsObj.hasErrors()) return false;

          // get emitted assets when done with compilation
          const webpackStats = webpackStatsObj.toJson();
          let outputPath = this.outputPath || webpackStats.outputPath;

          Eif (!fs.existsSync(path.resolve(outputPath))) {
            throw `Invalid path: ${outputPath}. Cannot emit ${this.outputFileName}`;
          }

          const outputFile = path.join(path.resolve(outputPath), this.outputFileName);

          if (this.deleteAfterCompile) {
            fs.existsSync(outputFile) && fs.unlinkSync(outputFile);
            return;
          }
          if (webpackStats && Object.keys(webpackStats).length && webpackStats["assetsByChunkName"]) {
            fs.writeFileSync(outputFile, JSON.stringify(this.syncedFiles), "utf-8");
          }
        });
      }
      add(key, value) {
        if (!key || !value) return;
        this.syncedFiles[key] = value;
      }
      get(key) {
        if (!key || !this.syncedFiles[key]) return "";
        return this.syncedFiles[key];
      }
    }

    module.exports = SyncedFilesPlugin;