1
1
import { debug } from "electron-builder-util"
2
2
import { deepAssign } from "electron-builder-util/out/deepAssign"
3
- import { warn } from "electron-builder-util/out/log"
3
+ import { log , warn } from "electron-builder-util/out/log"
4
4
import { readJson } from "fs-extra-p"
5
5
import mime from "mime"
6
6
import * as path from "path"
7
+ import { BuildInfo } from "./packagerApi"
7
8
import { PlatformPackager } from "./platformPackager"
8
9
9
10
export type FileTransformer = ( path : string ) => Promise < null | string | Buffer > | null | string | Buffer
10
11
11
- export async function createTransformer ( projectDir : string , srcDir : string , packager : PlatformPackager < any > ) : Promise < FileTransformer > {
12
- const devDeps = packager . info . devMetadata . dependencies
13
- const deps = packager . info . metadata . dependencies
14
- const useElectronCompile = ( devDeps != null && ( "electron-compile" in devDeps ) ) || ( deps != null && ( "electron-compile" in deps ) )
12
+ function isElectronCompileUsed ( info : BuildInfo ) : boolean {
13
+ const depList = [ ( < any > info . metadata ) . devDependencies , info . metadata . dependencies ]
14
+ if ( info . isTwoPackageJsonProjectLayoutUsed ) {
15
+ depList . push ( ( < any > info . devMetadata ) . devDependencies )
16
+ depList . push ( info . devMetadata . dependencies )
17
+ }
18
+
19
+ for ( const deps of depList ) {
20
+ if ( deps != null && "electron-compile" in deps ) {
21
+ log ( "electron-compile detected — files will be compiled" )
22
+ return true
23
+ }
24
+ }
15
25
26
+ return false
27
+ }
28
+
29
+ export async function createTransformer ( projectDir : string , srcDir : string , packager : PlatformPackager < any > ) : Promise < FileTransformer > {
16
30
const extraMetadata = packager . packagerOptions . extraMetadata
17
31
const mainPackageJson = path . join ( srcDir , "package.json" )
18
32
@@ -30,7 +44,7 @@ export async function createTransformer(projectDir: string, srcDir: string, pack
30
44
}
31
45
}
32
46
33
- return useElectronCompile ? await createElectronCompileTransformer ( projectDir , defaultTransformer ) : defaultTransformer
47
+ return isElectronCompileUsed ( packager . info ) ? await createElectronCompileTransformer ( projectDir , defaultTransformer ) : defaultTransformer
34
48
}
35
49
36
50
async function createElectronCompileTransformer ( projectDir : string , defaultTransformer : FileTransformer ) {
@@ -60,7 +74,11 @@ async function createElectronCompileTransformer(projectDir: string, defaultTrans
60
74
61
75
const cache = compilerHost . cachesForCompilers . get ( compiler )
62
76
const result = await cache . getOrFetch ( file , ( file : string , hashInfo : any ) => compilerHost . compileUncached ( file , hashInfo , compiler ) )
63
- return result . code || result . binaryData
77
+ const code = result . code
78
+ if ( type === "application/javascript" && code != null && ( code . includes ( "require('electron-compile')" ) || code . includes ( 'require("electron-compile")' ) ) ) {
79
+ warn ( "electron-compile should be not used in the production code" )
80
+ }
81
+ return code || result . binaryData
64
82
}
65
83
}
66
84
0 commit comments