mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-02-26 04:40:10 +08:00
feat: useNodeJsInElectronRenderer()
This commit is contained in:
parent
7ba9a5a368
commit
e0b33df336
@ -1,6 +1,7 @@
|
|||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { builtinModules } from 'module'
|
import { builtinModules } from 'module'
|
||||||
import { defineConfig } from 'vite-plugin-electron'
|
import { defineConfig } from 'vite-plugin-electron'
|
||||||
|
import resolve, { lib2esm } from 'vite-plugin-resolve'
|
||||||
import pkg from './package.json'
|
import pkg from './package.json'
|
||||||
|
|
||||||
const external = [
|
const external = [
|
||||||
@ -41,3 +42,40 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export function useNodeJsInElectronRenderer() {
|
||||||
|
return resolve(
|
||||||
|
/**
|
||||||
|
* Here you can specify other modules
|
||||||
|
* 🚧 You have to make sure that your module is in `dependencies` and not in the` devDependencies`,
|
||||||
|
* which will ensure that the electron-builder can package it correctly
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
// If you use the following modules, the following configuration will work
|
||||||
|
// What they have in common is that they will return - ESM format code snippets
|
||||||
|
|
||||||
|
// ESM format string
|
||||||
|
'electron-store': 'export default require("electron-store");',
|
||||||
|
// Use lib2esm() to easy to convert ESM
|
||||||
|
// Equivalent to
|
||||||
|
/**
|
||||||
|
* sqlite3: () => `
|
||||||
|
* const _M_ = require('sqlite3');
|
||||||
|
* const _D_ = _M_.default || _M_;
|
||||||
|
* export { _D_ as default }
|
||||||
|
* `
|
||||||
|
*/
|
||||||
|
sqlite3: lib2esm('sqlite3', { format: 'cjs' }),
|
||||||
|
serialport: lib2esm(
|
||||||
|
// CJS lib name
|
||||||
|
'serialport',
|
||||||
|
// export memebers
|
||||||
|
[
|
||||||
|
'SerialPort',
|
||||||
|
'SerialPortMock',
|
||||||
|
],
|
||||||
|
{ format: 'cjs' },
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ import { defineConfig } from 'vite'
|
|||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import electron from 'vite-plugin-electron'
|
import electron from 'vite-plugin-electron'
|
||||||
import renderer from 'vite-plugin-electron/renderer'
|
import renderer from 'vite-plugin-electron/renderer'
|
||||||
import electronConfig from './vite.config.electron'
|
import electronConfig, { useNodeJsInElectronRenderer } from './vite.config.electron'
|
||||||
|
|
||||||
rmSync('dist', { recursive: true, force: true }) // v14.14.0
|
rmSync('dist', { recursive: true, force: true }) // v14.14.0
|
||||||
|
|
||||||
@ -14,5 +14,6 @@ export default defineConfig({
|
|||||||
electron(electronConfig),
|
electron(electronConfig),
|
||||||
// Enable use Electron, Node.js API in Renderer-process
|
// Enable use Electron, Node.js API in Renderer-process
|
||||||
renderer(),
|
renderer(),
|
||||||
|
useNodeJsInElectronRenderer(),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user