vue-cli-plugin-electron-haunv 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/plugin.js DELETED
@@ -1,40 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * Vite plugin for Electron development (electron:serve)
5
- * @param {object} options
6
- * @returns {import('vite').Plugin}
7
- */
8
- function electronServePlugin(options = {}) {
9
- return {
10
- name: 'vite-plugin-electron-builder:serve',
11
- apply: 'serve',
12
- async configureServer(server) {
13
- server.httpServer?.once('listening', async () => {
14
- const address = server.httpServer?.address()
15
- const port = typeof address === 'object' ? address?.port : 8080
16
- process.env.VITE_DEV_SERVER_URL = `http://localhost:${port}`
17
- const { serve } = require('./serve')
18
- await serve(options)
19
- })
20
- },
21
- }
22
- }
23
-
24
- /**
25
- * Vite plugin for Electron production build (electron:build)
26
- * @param {object} options
27
- * @returns {import('vite').Plugin}
28
- */
29
- function electronBuildPlugin(options = {}) {
30
- return {
31
- name: 'vite-plugin-electron-builder:build',
32
- apply: 'build',
33
- async closeBundle() {
34
- const { buildMain } = require('./build')
35
- await buildMain(options)
36
- },
37
- }
38
- }
39
-
40
- module.exports = { electronServePlugin, electronBuildPlugin }
package/lib/protocol.js DELETED
@@ -1,55 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * Create a secure custom protocol handler for Electron.
5
- *
6
- * This registers a custom file:// protocol (e.g. app://) that serves
7
- * renderer files without path traversal vulnerabilities.
8
- *
9
- * Usage in your background.js (main process):
10
- *
11
- * import { createProtocol } from 'vite-plugin-electron-builder/lib/protocol'
12
- *
13
- * app.whenReady().then(() => {
14
- * createProtocol('app')
15
- * win.loadURL('app://./index.html')
16
- * })
17
- *
18
- * @param {string} scheme - The protocol name, e.g. 'app'
19
- * @param {string} [customPathToFiles] - Override path to bundled renderer files
20
- */
21
- function createProtocol(scheme, customPathToFiles) {
22
- const { protocol } = require('electron')
23
- const path = require('path')
24
- const fs = require('fs')
25
- const { URL } = require('url')
26
-
27
- // Determine the base path for renderer files
28
- const basePath = customPathToFiles
29
- ? path.resolve(customPathToFiles)
30
- : path.resolve(__dirname, '..', 'bundled')
31
-
32
- protocol.registerFileProtocol(scheme, (request, callback) => {
33
- // Parse the URL and get the pathname
34
- const url = new URL(request.url)
35
- // Decode URI and normalize the path
36
- const decodedPath = decodeURIComponent(url.pathname)
37
- // Resolve to absolute path within basePath (prevents traversal)
38
- const filePath = path.normalize(path.join(basePath, decodedPath))
39
-
40
- // Security: ensure the resolved path is still inside basePath
41
- if (!filePath.startsWith(basePath + path.sep) && filePath !== basePath) {
42
- callback({ error: -6 }) // net::ERR_FILE_NOT_FOUND
43
- return
44
- }
45
-
46
- // Check if file exists, fall back to index.html for SPA routing
47
- if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
48
- callback({ path: filePath })
49
- } else {
50
- callback({ path: path.join(basePath, 'index.html') })
51
- }
52
- })
53
- }
54
-
55
- module.exports = { createProtocol }
package/lib/serve.js DELETED
@@ -1,99 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const { execa } = require('execa')
5
- const chokidar = require('chokidar')
6
- const chalk = require('chalk')
7
-
8
- let electronProcess = null
9
- let manualRestart = false
10
-
11
- /**
12
- * Start Electron in development mode, watching for main process changes.
13
- * @param {object} options
14
- * @param {string} [options.mainProcessFile] - path to compiled main process entry (default: 'dist-electron/main.js')
15
- * @param {string[]} [options.mainProcessWatch] - extra globs to watch for restarts
16
- * @param {string} [options.outputDir] - output directory for compiled main process (default: 'dist_electron')
17
- */
18
- async function serve(options = {}) {
19
- const outputDir = options.outputDir || 'dist_electron'
20
- const mainFile = options.mainProcessFile || path.join(outputDir, 'main.js')
21
-
22
- await compileMain(options)
23
- startElectron(mainFile)
24
-
25
- // Watch main process source files
26
- const watchPatterns = options.mainProcessWatch || [
27
- path.join(process.cwd(), 'src/main/**/*.{js,ts}'),
28
- path.join(process.cwd(), 'src/background.{js,ts}'),
29
- path.join(process.cwd(), 'src/preload/**/*.{js,ts}'),
30
- ]
31
-
32
- const watcher = chokidar.watch(watchPatterns, {
33
- ignoreInitial: true,
34
- ignored: /node_modules/,
35
- })
36
-
37
- watcher.on('change', async (file) => {
38
- console.log(chalk.blue(`[electron-builder] Main process changed: ${file}`))
39
- console.log(chalk.blue('[electron-builder] Recompiling...'))
40
- await compileMain(options)
41
- restartElectron(mainFile)
42
- })
43
- }
44
-
45
- /**
46
- * Compile the Electron main process using esbuild (via electron-builder internals or standalone)
47
- */
48
- async function compileMain(options = {}) {
49
- const { buildMain } = require('./build')
50
- try {
51
- await buildMain({ ...options, mode: 'development' })
52
- console.log(chalk.green('[electron-builder] Main process compiled successfully'))
53
- } catch (err) {
54
- console.error(chalk.red('[electron-builder] Main process compilation failed:'), err.message)
55
- }
56
- }
57
-
58
- /**
59
- * Launch the Electron process
60
- */
61
- function startElectron(mainFile) {
62
- const electronBin = require('electron')
63
- electronProcess = execa(String(electronBin), [mainFile], {
64
- stdio: 'inherit',
65
- env: {
66
- ...process.env,
67
- NODE_ENV: 'development',
68
- },
69
- })
70
-
71
- electronProcess.on('close', (code) => {
72
- if (!manualRestart) {
73
- console.log(chalk.yellow(`[electron-builder] Electron exited with code ${code}`))
74
- process.exit(code ?? 0)
75
- }
76
- manualRestart = false
77
- })
78
-
79
- electronProcess.catch((err) => {
80
- if (!err.killed) {
81
- console.error(chalk.red('[electron-builder] Electron process error:'), err)
82
- }
83
- })
84
- }
85
-
86
- /**
87
- * Kill and restart the Electron process
88
- */
89
- function restartElectron(mainFile) {
90
- if (electronProcess) {
91
- manualRestart = true
92
- electronProcess.kill()
93
- electronProcess = null
94
- }
95
- console.log(chalk.green('[electron-builder] Restarting Electron...'))
96
- startElectron(mainFile)
97
- }
98
-
99
- module.exports = { serve }
package/lib/vitePlugin.js DELETED
@@ -1,65 +0,0 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const fs = require('fs-extra')
5
- const { buildMain } = require('./build')
6
- const { getConfig } = require('./config')
7
- const chalk = require('chalk')
8
-
9
- /**
10
- * Creates the Vite plugin that integrates Electron into the Vite dev/build pipeline.
11
- * This is the primary export — add it to your vite.config.js plugins array.
12
- *
13
- * @param {import('..').ElectronBuilderOptions} pluginOptions
14
- * @returns {import('vite').Plugin}
15
- */
16
- function createElectronPlugin(pluginOptions = {}) {
17
- const options = getConfig(pluginOptions)
18
-
19
- return {
20
- name: 'vite-plugin-electron-builder',
21
-
22
- /**
23
- * Adjust base path so Electron can load the renderer via file:// or app:// protocol.
24
- */
25
- config(config, { command }) {
26
- if (command === 'serve') {
27
- return {
28
- base: './',
29
- server: { ...config.server },
30
- }
31
- }
32
- return {
33
- base: './',
34
- build: {
35
- outDir: path.join(options.outputDir, 'bundled'),
36
- ...config.build,
37
- },
38
- }
39
- },
40
-
41
- /**
42
- * Warn early if the main process entry file is missing.
43
- */
44
- async buildStart() {
45
- const mainFile = path.resolve(process.cwd(), options.mainProcessFile)
46
- if (!fs.existsSync(mainFile)) {
47
- this.warn(
48
- `[electron-builder] Main process file not found: ${mainFile}. ` +
49
- `Set mainProcessFile in createElectronPlugin options.`
50
- )
51
- }
52
- },
53
-
54
- /**
55
- * After the renderer bundle is emitted, compile the main process with esbuild.
56
- */
57
- async closeBundle() {
58
- console.log(chalk.cyan('\n[electron-builder] Compiling main process...'))
59
- await buildMain(options)
60
- console.log(chalk.green('[electron-builder] Main process compiled.\n'))
61
- },
62
- }
63
- }
64
-
65
- module.exports = { createElectronPlugin }