mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-17 05:38:04 +00:00
chore: 用esbuild代替vite来打包main
This commit is contained in:
parent
b4590c3c34
commit
c4219afd3d
152 changed files with 669 additions and 747 deletions
99
scripts/build.main.mjs
Normal file
99
scripts/build.main.mjs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import { build } from 'esbuild'
|
||||
import ora from 'ora'
|
||||
import { builtinModules } from 'module'
|
||||
import electron from 'electron'
|
||||
import { spawn } from 'child_process'
|
||||
import path from 'path'
|
||||
import waitOn from 'wait-on'
|
||||
import 'dotenv/config'
|
||||
import pc from 'picocolors'
|
||||
import minimist from 'minimist'
|
||||
|
||||
const argv = minimist(process.argv.slice(2))
|
||||
const TAG = '[script/build.main.ts]'
|
||||
const spinner = ora(`${TAG} Main Process Building...`)
|
||||
|
||||
const options = {
|
||||
entryPoints: ['./src/main/index.ts', './src/main/rendererPreload.ts'],
|
||||
outdir: './dist/main/',
|
||||
platform: 'node',
|
||||
format: 'cjs',
|
||||
bundle: true,
|
||||
external: [
|
||||
...builtinModules.filter(
|
||||
x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x)
|
||||
),
|
||||
'electron',
|
||||
'NeteaseCloudMusicApi',
|
||||
],
|
||||
}
|
||||
|
||||
const runApp = () => {
|
||||
return spawn(
|
||||
electron,
|
||||
[path.resolve(process.cwd(), './dist/main/index.js')],
|
||||
{
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'development',
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (argv.watch) {
|
||||
waitOn(
|
||||
{
|
||||
resources: [
|
||||
`http://127.0.0.1:${process.env.ELECTRON_WEB_SERVER_PORT}/index.html`,
|
||||
],
|
||||
timeout: 5000,
|
||||
},
|
||||
err => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
process.exit(1)
|
||||
} else {
|
||||
let child
|
||||
build({
|
||||
...options,
|
||||
watch: {
|
||||
onRebuild(error) {
|
||||
if (error) {
|
||||
console.error(pc.red('Rebuild Failed:'), error)
|
||||
} else {
|
||||
console.log(pc.green('Rebuild Succeeded'))
|
||||
if (child) child.kill()
|
||||
child = runApp()
|
||||
}
|
||||
},
|
||||
},
|
||||
}).then(() => {
|
||||
console.log(pc.yellow(`⚡ Run App`))
|
||||
if (child) child.kill()
|
||||
child = runApp()
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
spinner.start()
|
||||
build({
|
||||
...options,
|
||||
minify: true,
|
||||
})
|
||||
.then(() => {
|
||||
console.log(TAG, pc.green('Main Process Build Succeeded.'))
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(
|
||||
`\n${TAG} ${pc.red('Main Process Build Failed')}\n`,
|
||||
error,
|
||||
'\n'
|
||||
)
|
||||
})
|
||||
.finally(() => {
|
||||
spinner.stop()
|
||||
})
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import { build } from 'vite'
|
||||
|
||||
await build({
|
||||
configFile: 'packages/main/vite.config.ts',
|
||||
mode: process.env.NODE_ENV === 'debug' ? 'debug' : 'production'
|
||||
})
|
||||
await build({ configFile: 'packages/preload/vite.config.ts' })
|
||||
await build({ configFile: 'packages/renderer/vite.config.ts' })
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
import { builtinModules } from 'module'
|
||||
import { Plugin, build } from 'vite'
|
||||
import resolve from 'vite-plugin-resolve'
|
||||
|
||||
export default function esm2cjs(modules: string[]): Plugin {
|
||||
return resolve(
|
||||
{
|
||||
...modules.reduce(
|
||||
(memo, moduleId) =>
|
||||
Object.assign(memo, {
|
||||
async [moduleId](args) {
|
||||
await build({
|
||||
plugins: [
|
||||
{
|
||||
name: 'vite-plugin[node:mod-to-mod]',
|
||||
enforce: 'pre',
|
||||
resolveId(source) {
|
||||
if (source.startsWith('node:')) {
|
||||
return source.replace('node:', '')
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
build: {
|
||||
outDir: args.dir,
|
||||
minify: false,
|
||||
emptyOutDir: false,
|
||||
lib: {
|
||||
entry: require.resolve(moduleId),
|
||||
formats: ['cjs'],
|
||||
fileName: () => `${moduleId}.js`,
|
||||
},
|
||||
rollupOptions: {
|
||||
external: [...builtinModules],
|
||||
},
|
||||
},
|
||||
})
|
||||
},
|
||||
} as Parameters<typeof resolve>[0]),
|
||||
{}
|
||||
),
|
||||
},
|
||||
{ dir: '.vite-plugin-resolve-esm' }
|
||||
)
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
import { spawn } from 'child_process'
|
||||
import { createServer, build } from 'vite'
|
||||
import electron from 'electron'
|
||||
import util from 'util'
|
||||
import styles from 'ansi-styles'
|
||||
|
||||
/**
|
||||
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
|
||||
*/
|
||||
function watchMain(server) {
|
||||
/**
|
||||
* @type {import('child_process').ChildProcessWithoutNullStreams | null}
|
||||
*/
|
||||
let electronProcess = null
|
||||
const address = server.httpServer.address()
|
||||
const env = Object.assign(process.env, {
|
||||
VITE_DEV_SERVER_HOST: address.address,
|
||||
VITE_DEV_SERVER_PORT: address.port,
|
||||
})
|
||||
|
||||
return build({
|
||||
configFile: 'packages/main/vite.config.ts',
|
||||
mode: 'development',
|
||||
plugins: [
|
||||
{
|
||||
name: 'electron-main-watcher',
|
||||
writeBundle() {
|
||||
electronProcess && electronProcess.kill()
|
||||
electronProcess = spawn(electron, ['.'], { stdio: 'inherit', env })
|
||||
},
|
||||
},
|
||||
],
|
||||
build: {
|
||||
watch: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {(server: import('vite').ViteDevServer) => Promise<import('rollup').RollupWatcher>}
|
||||
*/
|
||||
function watchPreload(server) {
|
||||
return build({
|
||||
configFile: 'packages/preload/vite.config.ts',
|
||||
mode: 'development',
|
||||
plugins: [
|
||||
{
|
||||
name: 'electron-preload-watcher',
|
||||
writeBundle() {
|
||||
server.ws.send({ type: 'full-reload' })
|
||||
},
|
||||
},
|
||||
],
|
||||
build: {
|
||||
watch: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// log prefix
|
||||
function logPrefix(fn) {
|
||||
const funcs = {
|
||||
log: console.log.bind(console),
|
||||
info: console.info.bind(console),
|
||||
warn: console.warn.bind(console),
|
||||
error: console.error.bind(console),
|
||||
debug: (console.debug || console.log).bind(console),
|
||||
}
|
||||
const processLogText = (text, prefix) => {
|
||||
return text.replaceAll('\n', '\n' + prefix)
|
||||
}
|
||||
Object.keys(funcs).forEach(function (k) {
|
||||
console[k] = function () {
|
||||
const string = typeof fn === 'function' ? fn() : fn
|
||||
arguments[0] = util.format(string, processLogText(arguments[0], string))
|
||||
funcs[k].apply(console, arguments)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const color = (hex, text) => {
|
||||
return `${styles.color.ansi(styles.hexToAnsi(hex))}${text}${
|
||||
styles.color.close
|
||||
}`
|
||||
}
|
||||
|
||||
// bootstrap
|
||||
logPrefix(color('#eab308', '[vite] '))
|
||||
console.log('building renderer')
|
||||
const server = await createServer({
|
||||
configFile: 'packages/renderer/vite.config.ts',
|
||||
})
|
||||
await server.listen()
|
||||
|
||||
console.log('building preload')
|
||||
await watchPreload(server)
|
||||
|
||||
console.log('building main')
|
||||
await watchMain(server)
|
||||
Loading…
Add table
Add a link
Reference in a new issue