2016-03-22 2 views
1

私たちのアプリケーションには、複数のエントリポイント(AとBなど)があります。エントリポイントAの生成された名前を取得し、それをモジュールBで使用する必要があります。エントリポイントAの名前にはハッシュが含まれている必要があります。それはwebpackの設定といくつかのローダー/プラグインだけで行うことができますか?webpackを使用して別のモジュールのモジュールエントリのパスを取得する方法は?

答えて

2

私のソリューションは、統計情報からファイル名を取得することが可能であるときafter-emit相内のファイルを変更する独自のプラグインを作成することでした:

new PlaceholderToAssetReplacerPlugin({ 
    path: '../dist/static', 
    sourceChunkName: 'embedApiLoader', 
    destinationChunkName: 'embedApi', 
    variable: '__EMBED_API_ASSET__' 
}) 

const replace = require("replace-in-file"); 
const path = require('path'); 

function PlaceholderToAssetReplacerPlugin(options) { 
    this.options = options; 
} 

PlaceholderToAssetReplacerPlugin.prototype.apply = function (compiler) { 
    const self = this; 

    compiler.plugin('after-emit', function (compilation, callback) { 
    const stats = compilation.getStats().toJson({ 
     hash: false, 
     publicPath: true, 
     assets: true, 
     chunks: false, 
     modules: false, 
     source: false, 
     errorDetails: false, 
     timings: false 
    }); 

    const assetsByChunkName = stats.assetsByChunkName; 
    replace({ 
     files: path.join(__dirname, self.options.path, assetsByChunkName[self.options.sourceChunkName]), 
     replace: new RegExp(self.options.variable), 
     with: `"${assetsByChunkName[self.options.destinationChunkName]}"` 
    }, (err, changedFiles) => { 
     if (err) { 
     throw err; 
     } 
     callback(); 
    }); 
    }); 
}; 

module.exports = PlaceholderToAssetReplacerPlugin; 

は、このようにそれを使用します

関連する問題