Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some bug in v0.8.4 #51

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/electron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ export default {
}
```

you can use `process.env.VITE_DEV_SERVER_URL` when the vite command is called 'serve'

```js
// electron main.js
const win = new BrowserWindow({
title: 'Main window',
})

if (process.env.VITE_DEV_SERVER_URL) {
Copy link
Member

@caoxiemeihao caoxiemeihao Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need wizard spelling in electron main.ts

process.env['VITE_DEV_SERVER_URL']

vitejs/vite#8843 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

win.loadURL(process.env.VITE_DEV_SERVER_URL)
} else {
// load your file
win.loadFile('yourOutputFile.html');
}
```

## API

`electron(config: Configuration)`
Expand Down
1 change: 1 addition & 0 deletions packages/electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare namespace NodeJS {
NODE_ENV: 'development' | 'production'
readonly VITE_DEV_SERVER_HOST: string
readonly VITE_DEV_SERVER_PORT: string
readonly VITE_DEV_SERVER_URL: string
}

interface Process {
Expand Down
1 change: 1 addition & 0 deletions packages/electron/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function resolveBuildConfig(runtime: Runtime): InlineConfig {
// 🚧 Avoid recursive build caused by load config file
configFile: false,
publicDir: false,
mode: viteConfig.mode,

build: {
emptyOutDir: false,
Expand Down
23 changes: 15 additions & 8 deletions packages/electron/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Configuration } from './types'
import type { Plugin } from 'vite'
import type { Plugin, ResolvedConfig } from 'vite'
import { bootstrap } from './serve'
import { build } from './build'
import renderer from 'vite-plugin-electron-renderer'
Expand Down Expand Up @@ -30,12 +30,19 @@ export default function electron(config: Configuration): Plugin[] {
})
},
},
{
name: `${name}:build`,
apply: 'build',
async configResolved(viteConfig) {
await build(config, viteConfig)
},
},
((): Plugin => {
let viteConfig: ResolvedConfig

return {
name: `${name}:build`,
apply: 'build',
configResolved(config) {
viteConfig = config
},
async closeBundle() {
await build(config, viteConfig)
}
}
})()
]
}
63 changes: 51 additions & 12 deletions packages/electron/src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ import {
checkPkgMain,
} from './config'

export const loopbackHosts = new Set([
'localhost',
'127.0.0.1',
'::1',
'0000:0000:0000:0000:0000:0000:0000:0001'
])

export const wildcardHosts = new Set([
'0.0.0.0',
'::',
'0000:0000:0000:0000:0000:0000:0000:0000'
])

function resolveHostname(hostname: string) {
return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? 'localhost' : hostname
}

function resolveEnv(server: ViteDevServer) {
const addressInfo = server.httpServer.address()
const isAddressInfo = (x: any): x is AddressInfo => x?.address

if (isAddressInfo(addressInfo)) {
const { address, port } = addressInfo
const host = resolveHostname(address)

const options = server.config.server
const protocol = options.https ? 'https' : 'http'
const devBase = server.config.base

const path = typeof options.open === 'string' ? options.open : devBase
const url = path.startsWith('http')
? path
: `${protocol}://${host}:${port}${path}`

return { url, host, port }
}
}

export async function bootstrap(config: Configuration, server: ViteDevServer) {
const electronPath = require('electron')
const { config: viteConfig } = server
Expand All @@ -23,57 +61,58 @@ export async function bootstrap(config: Configuration, server: ViteDevServer) {
if (config.preload) {
const preloadRuntime = resolveRuntime('preload', config, viteConfig)
const preloadConfig = mergeConfig(
resolveBuildConfig(preloadRuntime),
{
mode: 'development',
build: {
watch: true,
},
plugins: [{
name: 'electron-preload-watcher',
writeBundle() {
closeBundle() {
server.ws.send({ type: 'full-reload' })
},
}],
} as UserConfig,
resolveBuildConfig(preloadRuntime),
) as InlineConfig

await viteBuild(createWithExternal(preloadRuntime)(preloadConfig))
}

// ---- Electron-Main ----
const address = server.httpServer.address() as AddressInfo
const env = Object.assign(process.env, {
VITE_DEV_SERVER_HOST: address.address,
VITE_DEV_SERVER_PORT: address.port,
})
const env = resolveEnv(server)
if (env) {
Object.assign(process.env, {
VITE_DEV_SERVER_URL: env.url,
VITE_DEV_SERVER_HOST: env.host,
VITE_DEV_SERVER_PORT: env.port,
})
}

const mainRuntime = resolveRuntime('main', config, viteConfig)
const mainConfig = mergeConfig(
resolveBuildConfig(mainRuntime),
{
mode: 'development',
build: {
watch: true,
},
plugins: [
{
name: 'electron-main-watcher',
writeBundle() {
closeBundle() {
if (process.electronApp) {
process.electronApp.removeAllListeners()
process.electronApp.kill()
}

// Start Electron.app
process.electronApp = spawn(electronPath, ['.'], { stdio: 'inherit', env })
process.electronApp = spawn(electronPath, ['.'], { stdio: 'inherit', env: process.env })
// Exit command after Electron.app exits
process.electronApp.once('exit', process.exit)
},
},
checkPkgMain.buildElectronMainPlugin(mainRuntime),
],
} as UserConfig,
resolveBuildConfig(mainRuntime),
) as InlineConfig

await viteBuild(createWithExternal(mainRuntime)(mainConfig))
Expand Down