Skip to content

Instantly share code, notes, and snippets.

@giscafer
Created May 23, 2018 03:34
Show Gist options
  • Save giscafer/72b9c73493daa13fefb5f26243c63f07 to your computer and use it in GitHub Desktop.
Save giscafer/72b9c73493daa13fefb5f26243c63f07 to your computer and use it in GitHub Desktop.
generate hash version for building
/*--------------------------------------------------------------
* Copyright (c) Nickbing Lao<[email protected]>. All rights reserved.
* Licensed under the MIT License.
*-------------------------------------------------------------*/
const fs = require("fs");
const path = require("path");
const reg = /(main|styles|scripts|polyfills|inline).[0-9a-z]*.bundle.(css|js)/g;
let indexPagePath = path.join(__dirname, './dist/index.html');
// 读取index 内容,动态添加hash传
let indexContentStr = fs.readFileSync(indexPagePath).toString();
console.log(typeof indexContentStr)
let dateHash = new Date().getTime();
let newContent = indexContentStr.replace(reg, `$&?v=${dateHash}`);
fs.writeFileSync(indexPagePath, newContent);
console.log('hash generate success!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment