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

82.14% Statements 23/28
44.44% Branches 8/18
100% Functions 4/4
82.14% Lines 23/28

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
    471x
    1x

    40x
    40x
    40x

    40x
    140x

    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x

    40x
    40x

    40x

    40x

    40x
    40x
    40x

    40x

     const SynckedFilesPlugin = require("./index");
    module.exports = function () {

      const callback = this.async();
      const loaders = this.query.requestedLoaders;
      let syncPlugin = null;

      this._compiler.options.plugins.forEach(plugin => {
        Iif (plugin instanceof SynckedFilesPlugin) {
          syncPlugin = plugin;
        }
      });

      let requestString = "";
      let syncKey = "";
      let fileLoaderOptions = {};
      loaders.forEach(loader => {
        requestString += `!${loader.loader}?${JSON.stringify(loader.options || {})}`;
        Eif (loader.loader === "file-loader") {
          syncKey += `!${loader.loader}`;
          fileLoaderOptions = loader.options;
        } else {
          syncKey += `!${loader.loader}?${JSON.stringify(loader.options || {})}`;
        }
      });

      requestString = `!${requestString}!${this.resourcePath}`;
      syncKey = `!${syncKey}!${this.resourcePath}`;

      let syncSource;
      const publicPath = `${this._compiler.options.output.publicPath}${fileLoaderOptions ? fileLoaderOptions.publicPath: ""}`;

      Iif(syncPlugin && (syncSource = syncPlugin.get(syncKey))) {
        callback(null, syncSource.replace("__webpack_public_path__", JSON.stringify(publicPath)));
        return;
      }

      this.loadModule(requestString, function  (error, source) {
        Eif (!error) {
          Iif (syncPlugin) {
            syncPlugin.add(syncKey, source.replace(fileLoaderOptions.outputPath, ""));
          }
        }
        callback(error, source);
      });
    };