Skip to content

Instantly share code, notes, and snippets.

@adekbadek
Last active October 27, 2016 16:00
Show Gist options
  • Save adekbadek/b15d81784f470e7fc925c1ea58c62f03 to your computer and use it in GitHub Desktop.
Save adekbadek/b15d81784f470e7fc925c1ea58c62f03 to your computer and use it in GitHub Desktop.
// https://github.com/webpack/webpack/issues/86#issuecomment-179957661
// in index.pug, write simply [[app]] if your JS is app.js
var webpack = require('webpack')
var path = require('path')
var fs = require('fs')
module.exports = {
entry: {
app: './front/app.js'
},
output: {
path: path.join(__dirname, 'public'),
filename: 'js/app-[chunkhash:8].js',
publicPath: '/'
},
plugins: [
// ...
function () {
this.plugin('done', function (statsData) {
var stats = statsData.toJson()
if (!stats.errors.length) {
var templateFileName = 'front/views/index.pug'
var template = fs.readFileSync(path.join(__dirname, templateFileName), 'utf8')
var fileReplaceRegex = /\[\[([\w-]*)\]\]/g
var matches = template.match(fileReplaceRegex)
for (var i = 0; i < matches.length; i++) {
var placeholderString = matches[i]
var fileName = placeholderString.replace(fileReplaceRegex, '$1')
var hashedFileName = stats.assetsByChunkName[fileName][0]
template = template.replace(placeholderString, '\'' + hashedFileName + '\'')
}
fs.writeFileSync(path.join(__dirname, 'public/index.pug'), template)
}
})
}
],
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment