From f88225fe3eaceffa750eafb8fb178fe78e06978f Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Wed, 11 Mar 2015 10:48:52 +0100 Subject: [PATCH] Refactor asset helper to support .css (extract-text-webpack-plugin) and .manifest (appcache-webpack-plugin) files Unfortunately this breaks the old template structure --- default_index.html | 9 ++++++--- index.js | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/default_index.html b/default_index.html index e32adc3..6dd89de 100644 --- a/default_index.html +++ b/default_index.html @@ -1,12 +1,15 @@ - + {%=o.htmlWebpackPlugin.options.title || 'Webpack App'%} + {% for (var css in o.htmlWebpackPlugin.assets.css) { %} + + {% } %} - {% for (var chunk in o.htmlWebpackPlugin.assets) { %} - + {% for (var chunk in o.htmlWebpackPlugin.assets.chunks) { %} + {% } %} diff --git a/index.js b/index.js index 69c2079..fe79799 100644 --- a/index.js +++ b/index.js @@ -57,20 +57,40 @@ HtmlWebpackPlugin.prototype.emitHtml = function(compiler, htmlTemplateContent, t HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webpackStatsJson) { - var assets = {}; + var assets = { + // Will contain all js & css files by chunk + chunks: [], + // Will contain all js files + js: [], + // Will contain all css files + css: [], + // Will contain the html5 appcache manifest files if it exists + manifest: Object.keys(compilation.assets).filter(function(assetFile){ + return path.extname(assetFile) === '.appcache'; + })[0] + }; + var publicPath = compilation.options.output.publicPath || ''; + for (var chunk in webpackStatsJson.assetsByChunkName) { - var chunkValue = webpackStatsJson.assetsByChunkName[chunk]; + assets.chunks[chunk] = {}; + + // Prepend the public path to all chunk files + var chunkFiles = [].concat(webpackStatsJson.assetsByChunkName[chunk]).map(function(chunkFile) { + return publicPath + chunkFile; + }); // Webpack outputs an array for each chunk when using sourcemaps - if (chunkValue instanceof Array) { - // Is the main bundle always the first element? - chunkValue = chunkValue[0]; - } + // But we need only the entry file + var entry = chunkFiles[0]; + assets.chunks[chunk].entry = entry; + assets.js.push(entry); - if (compilation.options.output.publicPath) { - chunkValue = compilation.options.output.publicPath + chunkValue; - } - assets[chunk] = chunkValue; + // Gather all css files + var css = chunkFiles.filter(function(chunkFile){ + return path.extname(chunkFile) === '.css'; + }); + assets.chunks[chunk].css = css; + assets.css = assets.css.concat(css); } return assets;