Skip to content

Instantly share code, notes, and snippets.

@msikma
Created September 28, 2015 20:16
Show Gist options
  • Save msikma/7b37bf769f7b09369e87 to your computer and use it in GitHub Desktop.
Save msikma/7b37bf769f7b09369e87 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var fse = require('fs-extra');
var path = require('path');
var mkdirp = require('mkdirp');
// The file we'll write the stats to.
var deployPath = path.resolve(__dirname, '../../../deploy/');
var filepath = deployPath + '/.webpack-stats.json';
// JSON object of the stats.
var json;
// Public path, e.g. 'http://localhost:3000/bundles/'.
var publicPath;
/**
* Get chunks by name and extensions, with the public path prepended.
*
* @param {String} name Chunk name
* @param {String} ext Type extension
* @returns {Array} The filtered chunks
*/
function getChunks(name, ext) {
var chunk = json.assetsByChunkName[name];
// A chunk could be a string or an array, so make sure it is an array.
if (!(Array.isArray(chunk))) {
chunk = [chunk];
}
return chunk
// Filter by extension.
.filter(function filterByExtension(chunkName) {
return path.extname(chunkName) === '.' + ext;
})
// Append the public path.
.map(function appendPublicPath(chunkName) {
return publicPath + chunkName;
});
}
/**
* Write the stats file synchronously.
*
* @param {Object} stats Stat information about the generated chunks
*/
function writeStats(stats) {
json = stats.toJson();
publicPath = this.options.output.publicPath;
// Ensure the deploy directory exists.
fse.ensureDirSync(deployPath);
// Write the chunks to a JSON file.
fs.writeFileSync(filepath, JSON.stringify({
'script': getChunks('main', 'js'),
'css': getChunks('main', 'css')
}));
}
module.exports = writeStats;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment