Support webpack publicPath configuration

This commit is contained in:
Charles Blaxland 2014-08-12 23:18:07 +10:00
parent b0f3599d11
commit c10e9336e4
2 changed files with 19 additions and 2 deletions

View File

@ -10,9 +10,10 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('done', function(stats) {
var webpackStatsJson = stats.toJson();
console.log(JSON.stringify(webpackStatsJson, null, 2));
var templateParams = {};
templateParams.webpack = webpackStatsJson;
templateParams.htmlWebpackPlugin = self.htmlWebpackPluginJson(webpackStatsJson);
templateParams.htmlWebpackPlugin = self.htmlWebpackPluginJson(compiler, webpackStatsJson);
var templateFile = self.options.template;
if (!templateFile) {
@ -23,7 +24,7 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
});
};
HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(webpackStatsJson) {
HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(compiler, webpackStatsJson) {
var json = {};
json.assets = {};
for (var chunk in webpackStatsJson.assetsByChunkName) {
@ -34,6 +35,10 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginJson = function(webpackStatsJson) {
// Is the main bundle always the first element?
chunkValue = chunkValue[0];
}
if (compiler.options.output.publicPath) {
chunkValue = compiler.options.output.publicPath + chunkValue;
}
json.assets[chunk] = chunkValue;
}

View File

@ -93,5 +93,17 @@ describe('HtmlWebpackPlugin', function() {
}, [/<script src="index_bundle_[0-9a-f]+\.js"/], done);
});
it('prepends the webpack public path to script src', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js',
publicPath: 'http://cdn.example.com/assets/'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="http://cdn.example.com/assets/index_bundle.js"'], done);
});
});