UNPKG

replacer-contenthash-webpack-plugin

Version:

Plugin for webpack replaces contenthash

28 lines (26 loc) 1.04 kB
const path = require('path'); function replacer(template, files) { const format = '[name].[contenthash].[ext]'; let htmlOutput = template; for (let fileInfo of files) { const { filePath, contentHash } = fileInfo; const file = path.basename(filePath); const isRtl = file.split('.').length === 3; const extension = file.split('.').slice(-1).join(''); let fileName = file .split('.') .slice(0, isRtl ? -2 : -1) .join('') .replace(/[^a-z0-9_-]/gi, ''); if(isRtl) fileName += '.rtl'; const regex = new RegExp(`(['"].*)(${fileName}\\.${extension})(["'#])`, 'g'); let formattedFileName; formattedFileName = format .replace(/\[name\]/i, fileName) .replace(/\[contenthash\]/i, contentHash) .replace(/\[ext\]/i, extension); htmlOutput = htmlOutput.replace(regex, `$1${formattedFileName}$3`); } return htmlOutput; } module.exports = replacer;