From d2255cefb7b5b0fcf68a13f65250951b2d0050a5 Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Fri, 31 Oct 2025 19:31:07 +0330 Subject: [PATCH] Added preload post build script --- .github/workflows/deploy.yml | 70 ++++++++++++++++++------------------ package.json | 7 ++-- scripts/inject-preloads.js | 42 ++++++++++++++++++++++ src/main.ts | 1 + vite.config.ts | 3 ++ 5 files changed, 85 insertions(+), 38 deletions(-) create mode 100755 scripts/inject-preloads.js diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 48f3ba8..a9c24cd 100755 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,44 +1,44 @@ -name: Deploy to GitHub Pages +# name: Deploy to GitHub Pages -on: - push: - branches: [main] - pull_request: - branches: [main] +# on: +# push: +# branches: [main] +# pull_request: +# branches: [main] -jobs: - build-and-deploy: - runs-on: ubuntu-latest +# jobs: +# build-and-deploy: +# runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 +# steps: +# - name: Checkout code +# uses: actions/checkout@v4 - - uses: pnpm/action-setup@v4 - name: Install pnpm - with: - version: 10 - run_install: false +# - uses: pnpm/action-setup@v4 +# name: Install pnpm +# with: +# version: 10 +# run_install: false - - name: Install Node.js - uses: actions/setup-node@v4 - with: - node-version: 20 - cache: 'pnpm' +# - name: Install Node.js +# uses: actions/setup-node@v4 +# with: +# node-version: 20 +# cache: 'pnpm' - - name: Install dependencies - run: pnpm install +# - name: Install dependencies +# run: pnpm install - - name: Build project - run: DEPLOY_ENV=GH_PAGES pnpm build +# - name: Build project +# run: DEPLOY_ENV=GH_PAGES pnpm build - - name: Copy index.html to 404.html - run: cp dist/index.html dist/404.html +# - name: Copy index.html to 404.html +# run: cp dist/index.html dist/404.html - - name: Deploy to GitHub Pages - if: github.event_name == 'push' && github.ref == 'refs/heads/main' - uses: peaceiris/actions-gh-pages@v4 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./dist - publish_branch: gh-pages +# - name: Deploy to GitHub Pages +# if: github.event_name == 'push' && github.ref == 'refs/heads/main' +# uses: peaceiris/actions-gh-pages@v4 +# with: +# github_token: ${{ secrets.GITHUB_TOKEN }} +# publish_dir: ./dist +# publish_branch: gh-pages diff --git a/package.json b/package.json index b642350..ca23503 100755 --- a/package.json +++ b/package.json @@ -8,12 +8,13 @@ }, "scripts": { "dev": "vite", - "build": "run-p type-check \"build-only {@}\" --", + "build": "run-p type-check \"build-only {@}\" -- && npm run inject-preloads", "preview": "vite preview", "build-only": "vite build", "type-check": "vue-tsc --build", "lint": "eslint . --fix", - "format": "prettier --write src/" + "format": "prettier --write src/", + "inject-preloads": "node scripts/inject-preloads.js" }, "dependencies": { "@formkit/auto-animate": "^0.9.0", @@ -45,4 +46,4 @@ "vite-plugin-vue-devtools": "^8.0.3", "vue-tsc": "^3.1.2" } -} +} \ No newline at end of file diff --git a/scripts/inject-preloads.js b/scripts/inject-preloads.js new file mode 100755 index 0000000..76a8fbb --- /dev/null +++ b/scripts/inject-preloads.js @@ -0,0 +1,42 @@ +import fs from 'fs' +import path from 'path' + +const distDir = path.resolve('dist') +const manifestPath = path.join(distDir, '.vite/manifest.json') +const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')) + +const indexPath = path.join(distDir, 'index.html') +let html = fs.readFileSync(indexPath, 'utf-8') + +const filesToPreload = new Set() + +function collectFiles(entryKey) { + const chunk = manifest[entryKey] + if (!chunk) return + filesToPreload.add(chunk.file) + for (const imp of chunk.imports || []) { + if (imp === 'index.html') continue // skip entrypoint + const dep = manifest[imp] + if (dep) { + filesToPreload.add(dep.file) + collectFiles(imp) // recurse for transitive deps + } + } +} + +// automatically find all .vue entries that are dynamic (lazy‑loaded) +for (const key of Object.keys(manifest)) { + if (key.endsWith('.vue') && manifest[key].isDynamicEntry) { + collectFiles(key) + } +} + +for (const f of filesToPreload) { + const tag = `` + if (!html.includes(tag)) { + html = html.replace('', ` ${tag}\n`) + } +} + +fs.writeFileSync(indexPath, html) +console.log('Preload tags injected') diff --git a/src/main.ts b/src/main.ts index 16c7001..bbc5294 100755 --- a/src/main.ts +++ b/src/main.ts @@ -15,3 +15,4 @@ app.use(autoAnimatePlugin) app.use(MotionPlugin) app.mount('#app') + diff --git a/vite.config.ts b/vite.config.ts index 430ea0f..0e4336c 100755 --- a/vite.config.ts +++ b/vite.config.ts @@ -20,4 +20,7 @@ export default defineConfig({ '@': fileURLToPath(new URL('./src', import.meta.url)), }, }, + build: { + manifest: true + }, })