@@ -5,6 +5,17 @@ import type { Compiler as RspackCompiler } from '@rspack/core';
55import jwt from 'jsonwebtoken' ;
66import type { Compiler as WebpackCompiler } from 'webpack' ;
77import { type CodeSigningPluginConfig , validateConfig } from './config.js' ;
8+ import { embedPublicKey } from './embedPublicKey.js' ;
9+
10+ function resolveProjectPath (
11+ projectRoot : string ,
12+ configPath ?: string
13+ ) : string | undefined {
14+ if ( ! configPath ) return undefined ;
15+ return path . isAbsolute ( configPath )
16+ ? configPath
17+ : path . resolve ( projectRoot , configPath ) ;
18+ }
819
920export class CodeSigningPlugin {
1021 /**
@@ -55,6 +66,82 @@ export class CodeSigningPlugin {
5566 ) ;
5667 }
5768
69+ private embedPublicKeyInNativeProjects ( compiler : RspackCompiler ) {
70+ if ( ! this . config . publicKeyPath ) {
71+ return ;
72+ }
73+
74+ const logger = compiler . getInfrastructureLogger ( 'RepackCodeSigningPlugin' ) ;
75+ const projectRoot = compiler . context ;
76+
77+ const publicKeyPath = resolveProjectPath (
78+ projectRoot ,
79+ this . config . publicKeyPath
80+ ) ;
81+
82+ if ( ! publicKeyPath ) return ;
83+
84+ if ( ! fs . existsSync ( publicKeyPath ) ) {
85+ logger . warn (
86+ `Public key not found at ${ publicKeyPath } . ` +
87+ 'Skipping automatic embedding into native project files.'
88+ ) ;
89+ return ;
90+ }
91+
92+ const result = embedPublicKey ( {
93+ publicKeyPath,
94+ projectRoot,
95+ iosInfoPlistPath : resolveProjectPath (
96+ projectRoot ,
97+ this . config . nativeProjectPaths ?. ios
98+ ) ,
99+ androidStringsXmlPath : resolveProjectPath (
100+ projectRoot ,
101+ this . config . nativeProjectPaths ?. android
102+ ) ,
103+ } ) ;
104+
105+ if ( result . error ) {
106+ logger . warn ( result . error ) ;
107+ return ;
108+ }
109+
110+ if ( result . ios . modified ) {
111+ logger . info ( `Embedded public key in iOS Info.plist: ${ result . ios . path } ` ) ;
112+ } else if ( result . ios . error ) {
113+ logger . warn ( `Failed to embed public key in iOS: ${ result . ios . error } ` ) ;
114+ } else if ( result . ios . path ) {
115+ logger . debug (
116+ `Public key already up-to-date in iOS Info.plist: ${ result . ios . path } `
117+ ) ;
118+ } else {
119+ logger . warn (
120+ 'Could not find iOS Info.plist. Skipping auto-embedding for iOS. ' +
121+ 'Use nativeProjectPaths.ios or manually add the public key to Info.plist.'
122+ ) ;
123+ }
124+
125+ if ( result . android . modified ) {
126+ logger . info (
127+ `Embedded public key in Android strings.xml: ${ result . android . path } `
128+ ) ;
129+ } else if ( result . android . error ) {
130+ logger . warn (
131+ `Failed to embed public key in Android: ${ result . android . error } `
132+ ) ;
133+ } else if ( result . android . path ) {
134+ logger . debug (
135+ `Public key already up-to-date in Android strings.xml: ${ result . android . path } `
136+ ) ;
137+ } else {
138+ logger . warn (
139+ 'Could not find Android strings.xml. Skipping auto-embedding for Android. ' +
140+ 'Use nativeProjectPaths.android or manually add the public key to strings.xml.'
141+ ) ;
142+ }
143+ }
144+
58145 apply ( compiler : RspackCompiler ) : void ;
59146 apply ( compiler : WebpackCompiler ) : void ;
60147
@@ -78,15 +165,31 @@ export class CodeSigningPlugin {
78165 */
79166 const TOKEN_BUFFER_SIZE = 1280 ;
80167 /**
81- * Used to denote beginning of the code-signing section of the bundle
168+ * Used to denote the beginning of the code-signing section of the bundle
82169 * alias for "Repack Code-Signing Signature Begin"
83170 */
84171 const BEGIN_CS_MARK = '/* RCSSB */' ;
85172
86- const privateKeyPath = path . isAbsolute ( this . config . privateKeyPath )
87- ? this . config . privateKeyPath
88- : path . resolve ( compiler . context , this . config . privateKeyPath ) ;
89- const privateKey = fs . readFileSync ( privateKeyPath ) ;
173+ const privateKeyPath = resolveProjectPath (
174+ compiler . context ,
175+ this . config . privateKeyPath
176+ ) ;
177+
178+ if ( ! privateKeyPath ) {
179+ throw new Error ( '[RepackCodeSigningPlugin] privateKeyPath is required.' ) ;
180+ }
181+
182+ let privateKey : Buffer ;
183+ try {
184+ privateKey = fs . readFileSync ( privateKeyPath ) ;
185+ } catch ( err : unknown ) {
186+ const message = err instanceof Error ? err . message : String ( err ) ;
187+ throw new Error (
188+ `Failed to read private key from ${ privateKeyPath } : ${ message } `
189+ ) ;
190+ }
191+
192+ this . embedPublicKeyInNativeProjects ( compiler ) ;
90193
91194 const excludedChunks = Array . isArray ( this . config . excludeChunks )
92195 ? this . config . excludeChunks
0 commit comments