Skip to content

Commit

Permalink
chore: using commit hash to hash file name
Browse files Browse the repository at this point in the history
  • Loading branch information
btxTruong committed Mar 28, 2024
1 parent b3cc215 commit cd2100c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>TruongBui:React-render</title>
<style>
* {
box-sizing: border-box;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"postbuild": "node scripts/postbuild.cjs",
"lint": "eslint react-render --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"prod": "pm2 start scripts/pm2.config.cjs --no-daemon"
Expand Down
39 changes: 39 additions & 0 deletions scripts/postbuild.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {resolve} = require('node:path');
const {execSync} = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');

const commitHash = execSync('git rev-parse --short HEAD').toString().trim();

const indexHTMLPath = resolve(__dirname, '../dist/index.html');

function fileName(name) {
const parsed = path.parse(name);
parsed.name = `${parsed.name}.${commitHash}`;
parsed.base = `${parsed.name}${parsed.ext}`;
return path.format(parsed);
}

fs.readFile(indexHTMLPath, 'utf8', function(err, data) {
if (err) {
return console.log(err);
}

const indexName = fileName('index.js');
const clientName = fileName('client.js');
const devtoolsName = fileName('devtools.js');

let result = data.replace(new RegExp(indexName, 'g'),
`react-render/${indexName}`);

result = result.replace(new RegExp(clientName, 'g'),
`react-render/${clientName}`);

result = result.replace(new RegExp(devtoolsName, 'g'),
`react-render/${devtoolsName}`);

// Write the updated content back to the file
fs.writeFile(indexHTMLPath, result, 'utf8', function(err) {
if (err) return console.log(err);
});
});
2 changes: 1 addition & 1 deletion src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'react-devtools-inline/backend';
import { initialize as createDevTools } from 'react-devtools-inline/frontend';

const APP_SOURCE = import.meta.env.DEV ? '/src/main.tsx' : 'main.js';
const APP_SOURCE = import.meta.env.DEV ? '/src/main.tsx' : `main.${import.meta.env.VITE_COMMIT_HASH}.js`;

function hookNamesModuleLoaderFunction() {
return import('react-devtools-inline/hookNames');
Expand Down
8 changes: 8 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_COMMIT_HASH: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}
15 changes: 7 additions & 8 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import packageJson from './package.json'
import { execSync } from 'node:child_process';

/*
* We need repo name to correct endpoint of github page
*/
const REPO = packageJson.name
const commitHash = execSync('git rev-parse --short HEAD').toString().trim();

process.env.VITE_COMMIT_HASH = commitHash;

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -20,9 +19,9 @@ export default defineConfig({
index: 'index.html',
},
output: {
chunkFileNames: `${REPO}/[name].[hash].js`,
entryFileNames: `${REPO}/[name].js`,
assetFileNames: `[name].[ext]`,
chunkFileNames: `[name].${commitHash}.js`,
entryFileNames: `[name].${commitHash}.js`,
assetFileNames: `[name].${commitHash}.[ext]`,
sourcemapExcludeSources: true,
},
}
Expand Down

0 comments on commit cd2100c

Please sign in to comment.