Kaynağa Gözat

[functionality] make it work with target assets and chunks

mightyplow 6 yıl önce
ebeveyn
işleme
8518853afb
1 değiştirilmiş dosya ile 19 ekleme ve 8 silme
  1. 19 8
      index.js

+ 19 - 8
index.js

@@ -24,21 +24,32 @@ InjectAssetsPlugin.prototype = {
             const {targets} = this.options;
             const {assets} = compilation;
 
-            targets.forEach(function replaceAssetsForTarget (targetAssetName) {
-                const targetAsset = assets[targetAssetName];
+            targets.forEach(function replaceAssetsForTarget (targetChunkName) {
+                const chunkFiles = getChunkFiles(compilation);
+
+                /*
+                    Tries to find the target in the chunk files. This is the case
+                    when the chunk is an entrypoint in the webpack config. If it's
+                    not in the chunk files, it is assumed that the target is processed
+                    via a plugin.
+                 */
+                const assetFileName = chunkFiles.hasOwnProperty(targetChunkName)
+                    ? chunkFiles[targetChunkName]
+                    : targetChunkName;
+
+                const targetAsset = assets[assetFileName];
 
                 if (!targetAsset) {
-                    console.warn(`the target asset '${targetAsset}' was not found in the compilation 
-                                  and therefore gets skipped`.replace(/\s+/, ' '));
+                    console.warn(`the target asset '${targetChunkName}' was not found in the compilation
+                                  and therefore gets skipped`.replace(/\s+/g, ' '));
                     return;
                 }
 
-                const chunkFiles = getChunkFiles(compilation);
-                const targetSource = targetAsset.source().toString();
+                const targetSource = String(targetAsset.source());
                 const modifiedTarget = replaceAssets(targetSource, chunkFiles);
 
-                assets[targetAssetName] = modifiedTarget
-                    ? createAsset(modifiedTarget, targetAssetName)
+                assets[assetFileName] = modifiedTarget
+                    ? createAsset(modifiedTarget, targetChunkName)
                     : assets[targetAsset];
             });