feat: build

This commit is contained in:
erdongmaqiao 2024-04-06 16:12:39 +08:00
parent 6ae42923f7
commit 64af6c8247
7 changed files with 4294 additions and 1 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ yarn-debug.log*
yarn-error.log* yarn-error.log*
pnpm-debug.log* pnpm-debug.log*
lerna-debug.log* lerna-debug.log*
release
node_modules node_modules
dist dist

2820
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@
"@vitejs/plugin-basic-ssl": "^1.1.0", "@vitejs/plugin-basic-ssl": "^1.1.0",
"@vitejs/plugin-vue": "^5.0.4", "@vitejs/plugin-vue": "^5.0.4",
"electron": "^29.2.0", "electron": "^29.2.0",
"electron-builder": "^24.13.3",
"esbuild": "^0.20.2", "esbuild": "^0.20.2",
"typescript": "^5.2.2", "typescript": "^5.2.2",
"vite": "^5.2.0", "vite": "^5.2.0",

74
plugins/buildPlugin.ts Normal file
View File

@ -0,0 +1,74 @@
import path from 'path'
import fs from 'fs'
class BuildObj {
buildMain() {
import('esbuild').then(build => {
build.buildSync({
entryPoints: ['./src/main/mainEntry.ts'],
bundle: true,
platform: "node",
minify: true,
outfile: './dist/mainEntry.js',
external: ['electron']
})
})
}
preparePackageJson() {
const pkgJsonPath = path.join(process.cwd(), 'package.json')
const localPkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'))
const electronConfig = localPkgJson.devDependencies.electron.replace("^", "")
localPkgJson.main = "mainEntry.js"
delete localPkgJson.scripts
delete localPkgJson.devDependencies
localPkgJson.devDependencies = { electron: electronConfig }
const tarJsonPath = path.join(process.cwd(), "dist", "package.json")
fs.writeFileSync(tarJsonPath, JSON.stringify(localPkgJson))
fs.mkdirSync(path.join(process.cwd(), "dist/node_modules"))
}
buildInstaller() {
const options = {
config: {
directories: {
output: path.join(process.cwd(), "release"),
app: path.join(process.cwd(), 'dist')
},
files: ["**"],
extends: null,
productName: "JueJin",
appId: "com.juejin.desktop",
asar: true,
nsis: {
oneClick: true,
perMachine: true,
allowToChangeInstallationDirectory: false,
createDesktopShortcut: true,
createStartMenuShortcut: true,
shortcutName: "jujinDesktop"
},
// publish: [
// {provider: "generic", url: "http://localhost:5500"}
// ]
},
project: process.cwd()
}
return import('electron-builder').then(builder => {
builder.build(options)
})
}
}
export const buildPlugin = () => {
return {
name: "build-plugin",
closeBundle: () => {
console.log('')
const buildObj = new BuildObj()
buildObj.buildMain()
buildObj.preparePackageJson()
buildObj.buildInstaller()
}
}
}

File diff suppressed because it is too large Load Diff

14
src/main/customScheme.ts Normal file
View File

@ -0,0 +1,14 @@
import { protocol } from 'electron'
// import fs from 'fs'
// import path from 'path'
const schemeConfig = {
standard: true,
supportFetchAPI: true,
bypassCSP: true,
corsEnabled: true,
stream: true
}
protocol.registerSchemesAsPrivileged([
{scheme: "app", privileges: schemeConfig}
])

View File

@ -1,11 +1,12 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import { devPlugin, getReplacer } from './plugins/devPlugin' import { devPlugin, getReplacer } from './plugins/devPlugin'
import { buildPlugin } from './plugins/buildPlugin'
import optimizer from 'vite-plugin-optimizer' import optimizer from 'vite-plugin-optimizer'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [optimizer(getReplacer()), devPlugin(), vue()], plugins: [buildPlugin(), optimizer(getReplacer()), devPlugin(), vue()],
server: { server: {
host: '127.0.0.1' host: '127.0.0.1'
} }