Skip to content

Commit

Permalink
chore: bundle html file into main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Z233 committed Aug 20, 2023
1 parent 5d97726 commit c2e349f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

- name: Rename artifact
run:
mv download/plugin.zip download/${{ github.event.repository.name }}-${{ github.ref_name }}.zip
unzip download/plugin.zip -d download && rm download/plugin.zip

- name: Create release
uses: 'marvinpinto/action-automatic-releases@latest'
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"scripts": {
"dev": "npm run clear && cross-env NODE_ENV=development run-p dev:*",
"dev:js": "npm run build:js -- --mode development",
"dev:web": "vite dev --config vite.config.window.ts",
"dev:web": "vite dev --config vite.config.web.ts",
"build": "cross-env NODE_ENV=production run-s clear build:web build:js ",
"build:js": "vite build --config vite.config.ts",
"build:web": "vite build --config vite.config.window.ts",
"build:web": "vite build --config vite.config.web.ts",
"release": "run-s build pack",
"pack": "rimraf plugin.zip && bestzip plugin.zip window main.js styles.css manifest.json",
"pack": "rimraf plugin.zip && bestzip plugin.zip main.js styles.css manifest.json",
"clear": "rimraf assets window main.js styles.css",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"typecheck": "tsc --noEmit"
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="vite/client" />

declare const __DEV__: boolean
declare const __MINI_TRACKER_HTML__: string

interface ImportMetaEnv {
readonly VITE_DEV_SERVER_URL: string
Expand Down
8 changes: 1 addition & 7 deletions src/window/mini-tracker/mini-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ export class MiniTracker {
if (__DEV__) {
this.win.loadURL(import.meta.env.VITE_DEV_SERVER_URL + 'window/mini-tracker/index.html')
} else {
const filePath = normalizePath(`${windowFolderPath}/mini-tracker/index.html`)

const fs = require('node:fs/promises')
const buffer = await fs.readFile(filePath)
const content = buffer.toString('utf8')

this.win.loadURL(`data:text/html;charset=UTF-8,${encodeURIComponent(content)}`)
this.win.loadURL(`data:text/html;charset=UTF-8,__MINI_TRACKER_HTML__`)
}

this.tracker.addObserver(this.trackerObserver)
Expand Down
17 changes: 13 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ import { isDev } from './scripts/utils'
import { preact } from '@preact/preset-vite'
import UnoCSS from 'unocss/vite'
import presetWind from 'unocss/preset-wind'
import fs from 'fs'
// import react from '@vitejs/plugin'

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source, please visit the github repository of this plugin
*/
`

const readMiniTrackerHtml = () => {
const html = fs.readFileSync('window/mini-tracker/index.html', 'utf-8')
return html
}

// https://vitejs.dev/config/
export default defineConfig({
esbuild: {
banner,
},
define: {
__DEV__: isDev,
__MINI_TRACKER_HTML__: isDev ? '' : encodeURIComponent(readMiniTrackerHtml()),
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
},
build: {
Expand All @@ -29,9 +36,11 @@ export default defineConfig({
entry: 'src/main.ts',
formats: ['cjs'],
},
watch: isDev ? {
buildDelay: 3000
} : undefined,
watch: isDev
? {
buildDelay: 3000,
}
: undefined,
outDir: '.',
emptyOutDir: false,
rollupOptions: {
Expand Down
File renamed without changes.

0 comments on commit c2e349f

Please sign in to comment.