Skip to content

Commit a701db5

Browse files
committed
fix: restore encryption for default preload.js
1 parent 3e8404f commit a701db5

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

playground/encryptor.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
corsEnabled: true,
1414
stream: true,
1515
},
16-
preload: ['preload.js', 'preload2.js'],
16+
preload: ['preload.cjs', 'preload2.js'],
1717
renderer: {
1818
entry: '../renderer',
1919
},

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { log } from 'builder-util'
77
import { compileToBytenode, encAes, encryptMd5, readFileMd5 } from './encrypt'
88
import { buildConfig, loadConfig } from './config'
99
import { buildBundle } from './build'
10+
import { findJsPath } from './utils'
1011
import type { AfterPackContext } from 'electron-builder'
1112

1213
export default function (context: AfterPackContext) {
@@ -141,10 +142,10 @@ export async function run(context: AfterPackContext, options: RunOptions = {}) {
141142
: encryptorConfig.preload
142143

143144
for (const _preloadJsPath of preloadJsPaths) {
144-
const preloadJsName = path.basename(_preloadJsPath, '.js')
145-
const rendererPreloadJsPath = path.join(mainDir, _preloadJsPath)
146-
const preloadJsDir = path.dirname(rendererPreloadJsPath)
147-
if (fs.existsSync(rendererPreloadJsPath)) {
145+
const { name: preloadJsName } = path.parse(_preloadJsPath)
146+
const rendererPreloadJsPath = findJsPath(mainDir, preloadJsName)
147+
if (rendererPreloadJsPath) {
148+
const preloadJsDir = path.dirname(rendererPreloadJsPath)
148149
const rendererPreloadJsCPath = path.join(
149150
preloadJsDir,
150151
`${preloadJsName}-c.jsc`

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
export function findJsPath(dir: string, fileName: string) {
5+
const exts = ['.js', '.cjs', '.mjs']
6+
for (const ext of exts) {
7+
const filePath = path.join(dir, fileName + ext)
8+
if (fs.existsSync(filePath)) {
9+
return filePath.replace(/\\/g, '/')
10+
}
11+
}
12+
return null
13+
}

0 commit comments

Comments
 (0)