Refactor UUID generation, remove unused logger and encryption utilities, and clean up request handling
- Updated `generateUUID` function for improved readability and performance. - Deleted `logger.ts`, `other.ts`, `request.ts`, `storage.ts`, `tansParams.ts`, and `validate.ts` as they were no longer needed. - Simplified TypeScript configuration by removing unnecessary paths and aliases. - Enhanced Vite configuration for better project structure and maintainability.
This commit is contained in:
@@ -7,41 +7,70 @@ export interface ElectronBytecodeOptions {
|
||||
keepSource?: boolean
|
||||
}
|
||||
|
||||
function shouldSkipBytecode(): boolean {
|
||||
const flag = String(process.env.SKIP_ELECTRON_BYTECODE ?? '').trim().toLowerCase()
|
||||
return flag === '1' || flag === 'true' || flag === 'yes'
|
||||
}
|
||||
|
||||
function resolveElectronExecutable(): string | null {
|
||||
try {
|
||||
const electronPath = require('electron')
|
||||
if (typeof electronPath === 'string' && fs.pathExistsSync(electronPath)) {
|
||||
return electronPath
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[vite-plugin-electron-encrypt] Skipping bytecode compilation because Electron is unavailable:', error)
|
||||
return null
|
||||
}
|
||||
|
||||
console.warn('[vite-plugin-electron-encrypt] Skipping bytecode compilation because the Electron executable could not be resolved.')
|
||||
return null
|
||||
}
|
||||
|
||||
export default function electronBytecode(options?: ElectronBytecodeOptions): PluginOption {
|
||||
return {
|
||||
name: 'vite-plugin-electron-encrypt',
|
||||
apply: 'build',
|
||||
async closeBundle() {
|
||||
if (process.env.NODE_ENV !== 'production') return
|
||||
if (shouldSkipBytecode()) {
|
||||
console.warn('[vite-plugin-electron-encrypt] Skipping bytecode compilation because SKIP_ELECTRON_BYTECODE is enabled.')
|
||||
return
|
||||
}
|
||||
|
||||
const entryPath = path.resolve(process.cwd(), options?.entry || 'dist-electron/main/main.js');
|
||||
|
||||
if (!fs.pathExistsSync(entryPath)) return
|
||||
|
||||
const electronPath = resolveElectronExecutable()
|
||||
if (!electronPath) return
|
||||
|
||||
const outputPath = entryPath.replace(/\.js$/, '.jsc')
|
||||
const backupPath = `${entryPath}.bak`
|
||||
const tempScriptPath = path.join(path.dirname(entryPath), '_compile.js')
|
||||
const { execFileSync } = require('child_process')
|
||||
|
||||
fs.copyFileSync(entryPath, backupPath)
|
||||
|
||||
// We must compile the bytecode using the ELECTRON executable, not the local Node.js!
|
||||
// Node.js and Electron use different V8 versions.
|
||||
const electronPath = require('electron');
|
||||
const { execFileSync } = require('child_process');
|
||||
|
||||
|
||||
try {
|
||||
// We must compile the bytecode using the ELECTRON executable, not the local Node.js.
|
||||
// Node.js and Electron use different V8 versions.
|
||||
// Run a small script inside Electron to compile the file
|
||||
const compileScript = `
|
||||
const bytenode = require('bytenode');
|
||||
bytenode.compileFile({ filename: ${JSON.stringify(entryPath)}, output: ${JSON.stringify(outputPath)} }).then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });
|
||||
`;
|
||||
const tempScriptPath = path.join(path.dirname(entryPath), '_compile.js');
|
||||
fs.writeFileSync(tempScriptPath, compileScript);
|
||||
|
||||
execFileSync(electronPath, [tempScriptPath], { env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' } });
|
||||
fs.unlinkSync(tempScriptPath);
|
||||
} catch (err) {
|
||||
console.error('Failed to compile bytecode with Electron:', err);
|
||||
fs.copyFileSync(backupPath, entryPath)
|
||||
throw err;
|
||||
} finally {
|
||||
if (fs.pathExistsSync(tempScriptPath)) {
|
||||
fs.unlinkSync(tempScriptPath)
|
||||
}
|
||||
}
|
||||
|
||||
const stub = [
|
||||
|
||||
Reference in New Issue
Block a user