From 24e4f1067c8d064e451a7d64f8c24f4118d8bf37 Mon Sep 17 00:00:00 2001 From: Sibiraj Date: Wed, 3 Oct 2018 18:36:08 +0530 Subject: [PATCH] chore: fix typos and remove unnecessary regexp test --- index.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index e5558400..97208fac 100644 --- a/index.js +++ b/index.js @@ -100,14 +100,11 @@ class HtmlWebpackPlugin { } // `contenthash` is introduced in webpack v4.3 - // which conflicts with the plugin's existing `contenthash` method, hence - // hence is renmaed to `templatehash` to avoid such conflicts - const REGEXP_CONTENTHASH = /\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig; - if (REGEXP_CONTENTHASH.test(this.options.filename)) { - this.options.filename = this.options.filename.replace(REGEXP_CONTENTHASH, (match) => { - return match.replace('contenthash', 'templatehash'); - }); - } + // which conflicts with the plugin's existing `contenthash` method, + // hence it is renamed to `templatehash` to avoid conflicts + this.options.filename = this.options.filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (match) => { + return match.replace('contenthash', 'templatehash'); + }); // Check if webpack is running in production mode // @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14 @@ -289,11 +286,10 @@ class HtmlWebpackPlugin { return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR'; }) .then(html => { - const REGEXP_TEMPLATEHASH = /\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig; // Allow to use [templatehash] as placeholder for the html-webpack-plugin name // See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/ // From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214 - const finalOutputName = childCompilationOutputName.replace(REGEXP_TEMPLATEHASH, (_, hashType, digestType, maxLength) => { + const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => { return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10)); }); // Add the evaluated html code to the webpack assets