From dfb4f5717904c4bd70b24184740dec807aa27eff Mon Sep 17 00:00:00 2001 From: Julien Renaux Date: Mon, 13 Jul 2015 11:43:18 +0200 Subject: [PATCH] Adding the file size to the chunk object As far as I know, getting the file size within the template is not possible. The file size is important in my case in order to have a progress loader via http request so I can do somehting like that: ``` function ajax(url, onProgress, callback, data, x) { try { x = new(this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0'); x.open(data ? 'POST' : 'GET', url, 1); x.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); x.onreadystatechange = function() { x.readyState > 3 && callback && callback(x.responseText, x); }; x.addEventListener("progress", onProgress, false); x.send(data) } catch (e) { window.console && console.log(e); } }; function onLoad(responseText) { var script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); if (script.text) { script.text = content; // IE } else { script.appendChild(document.createTextNode(responseText)); } document.getElementsByTagName('head')[0].appendChild(script); document.getElementById('appLoader').className = 'finished'; } function onProgress(e) { var progress = Math.floor((e.loaded / {%=o.htmlWebpackPlugin.files.chunks.main.size %} || 0) * 100); document.getElementById('appLoader').setAttribute("value", progress); document.getElementById('appLoaderPercent').innerText = document.createTextNode(progress).textContent; } ajax('./{%=o.htmlWebpackPlugin.files.chunks.main.entry %}', onProgress, onLoad); ``` --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index cfaabbc..5094518 100644 --- a/index.js +++ b/index.js @@ -245,6 +245,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webp // Webpack outputs an array for each chunk when using sourcemaps // But we need only the entry file var entry = chunkFiles[0]; + assets.chunks[chunkName].size = chunk.size; assets.chunks[chunkName].entry = entry; assets.js.push(entry);