@@ -20,6 +20,13 @@ jest.mock('node:fs', () => ({
2020
2121jest . mock ( 'execa' ) ;
2222
23+ function emitAssets ( files : string [ ] ) {
24+ const [ , onAssetEmitted ] = compilerMock . hooks . assetEmitted . tap . mock . calls [ 0 ] ;
25+ for ( const file of files ) {
26+ onAssetEmitted ( file , { outputPath : 'output/path/' } ) ;
27+ }
28+ }
29+
2330const compilerMock = {
2431 context : __dirname ,
2532 options : {
@@ -29,6 +36,9 @@ const compilerMock = {
2936 } ,
3037 hooks : {
3138 assetEmitted : {
39+ tap : jest . fn ( ) ,
40+ } ,
41+ afterEmit : {
3242 tapPromise : jest . fn ( ) ,
3343 } ,
3444 } ,
@@ -56,7 +66,7 @@ describe('HermesBytecodePlugin', () => {
5666
5767 pluginInstance . apply ( compilerMock as unknown as Compiler ) ;
5868
59- expect ( compilerMock . hooks . assetEmitted . tapPromise ) . not . toHaveBeenCalled ( ) ;
69+ expect ( compilerMock . hooks . afterEmit . tapPromise ) . not . toHaveBeenCalled ( ) ;
6070 } ) ;
6171
6272 it ( 'overrides compareBeforeEmit webpack option by default' , ( ) => {
@@ -87,11 +97,10 @@ describe('HermesBytecodePlugin', () => {
8797 fsMock . access . mockResolvedValueOnce ( ) ;
8898
8999 await new Promise < void > ( ( resolve , reject ) => {
90- compilerMock . hooks . assetEmitted . tapPromise . mockImplementationOnce (
100+ compilerMock . hooks . afterEmit . tapPromise . mockImplementationOnce (
91101 ( _ , callback ) => {
92- callback ( 'example.bundle' , { outputPath : 'output/path/' } )
93- . then ( resolve )
94- . catch ( reject ) ;
102+ emitAssets ( [ 'example.bundle' ] ) ;
103+ callback ( ) . then ( resolve ) . catch ( reject ) ;
95104 }
96105 ) ;
97106 pluginInstance . apply ( compilerMock as unknown as Compiler ) ;
@@ -101,9 +110,7 @@ describe('HermesBytecodePlugin', () => {
101110 typeof execa . node
102111 > ;
103112
104- expect ( compilerMock . hooks . assetEmitted . tapPromise ) . toHaveBeenCalledTimes (
105- 1
106- ) ;
113+ expect ( compilerMock . hooks . afterEmit . tapPromise ) . toHaveBeenCalledTimes ( 1 ) ;
107114 expect ( execaMock ) . toHaveBeenCalledTimes ( 1 ) ;
108115 expect ( execaMock . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'path/to/hermesc' ) ;
109116
@@ -127,26 +134,85 @@ describe('HermesBytecodePlugin', () => {
127134 fsMock . access . mockRejectedValueOnce ( new Error ( 'File not found' ) ) ;
128135
129136 await new Promise < void > ( ( resolve , reject ) => {
130- compilerMock . hooks . assetEmitted . tapPromise . mockImplementationOnce (
137+ compilerMock . hooks . afterEmit . tapPromise . mockImplementationOnce (
131138 ( _ , callback ) => {
132- callback ( 'example.bundle' , { outputPath : 'output/path/' } )
133- . then ( resolve )
134- . catch ( reject ) ;
139+ emitAssets ( [ 'example.bundle' ] ) ;
140+ callback ( ) . then ( resolve ) . catch ( reject ) ;
135141 }
136142 ) ;
137143 pluginInstance . apply ( compilerMock as unknown as Compiler ) ;
138144 } ) ;
139145
140146 const execaMock = execa as jest . MockedFunction < typeof execa > ;
141147
142- expect ( compilerMock . hooks . assetEmitted . tapPromise ) . toHaveBeenCalledTimes (
143- 1
144- ) ;
148+ expect ( compilerMock . hooks . afterEmit . tapPromise ) . toHaveBeenCalledTimes ( 1 ) ;
145149 expect ( execaMock ) . toHaveBeenCalledTimes ( 1 ) ;
146150 expect ( execaMock . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'path/to/hermesc' ) ;
147151 } ) ;
148152 } ) ;
149153
154+ it ( 'transforms every matching asset and skips excluded ones' , async ( ) => {
155+ const pluginInstance = new HermesBytecodePlugin ( {
156+ enabled : true ,
157+ test : / \. ( j s ) ? b u n d l e $ / ,
158+ exclude : / i n d e x .b u n d l e $ / ,
159+ reactNativePath : 'path/to/react-native' ,
160+ hermesCLIPath : 'path/to/hermesc' ,
161+ } ) ;
162+
163+ const fsMock = fs . promises as jest . Mocked < typeof fs . promises > ;
164+ fsMock . access . mockResolvedValue ( ) ;
165+
166+ await new Promise < void > ( ( resolve , reject ) => {
167+ compilerMock . hooks . afterEmit . tapPromise . mockImplementationOnce (
168+ ( _ , callback ) => {
169+ emitAssets ( [ 'a.chunk.bundle' , 'b.chunk.bundle' , 'index.bundle' ] ) ;
170+ callback ( ) . then ( resolve ) . catch ( reject ) ;
171+ }
172+ ) ;
173+ pluginInstance . apply ( compilerMock as unknown as Compiler ) ;
174+ } ) ;
175+
176+ const execaMock = execa as jest . MockedFunction < typeof execa > ;
177+ const execaNodeMock = execa . node as jest . MockedFunction < typeof execa . node > ;
178+
179+ expect ( execaMock ) . toHaveBeenCalledTimes ( 2 ) ;
180+ expect ( execaNodeMock ) . toHaveBeenCalledTimes ( 2 ) ;
181+ } ) ;
182+
183+ it ( 'ignores unchanged assets on rebuild' , async ( ) => {
184+ const pluginInstance = new HermesBytecodePlugin ( {
185+ enabled : true ,
186+ test : / \. ( j s ) ? b u n d l e $ / ,
187+ exclude : / i n d e x .b u n d l e $ / ,
188+ reactNativePath : 'path/to/react-native' ,
189+ hermesCLIPath : 'path/to/hermesc' ,
190+ } ) ;
191+
192+ const fsMock = fs . promises as jest . Mocked < typeof fs . promises > ;
193+ fsMock . access . mockResolvedValue ( ) ;
194+
195+ pluginInstance . apply ( compilerMock as unknown as Compiler ) ;
196+ const [ , afterEmit ] = compilerMock . hooks . afterEmit . tapPromise . mock . calls [ 0 ] ;
197+
198+ emitAssets ( [ 'a.chunk.bundle' , 'b.chunk.bundle' ] ) ;
199+ await afterEmit ( ) ;
200+
201+ emitAssets ( [ 'b.chunk.bundle' ] ) ;
202+ await afterEmit ( ) ;
203+
204+ const execaMock = execa as jest . MockedFunction < typeof execa > ;
205+ const transformedBundles = execaMock . mock . calls . map ( ( [ , args ] ) =>
206+ ( args as string [ ] ) . at ( - 1 )
207+ ) ;
208+
209+ expect ( transformedBundles ) . toEqual ( [
210+ path . join ( 'output/path/' , 'a.chunk.bundle' ) ,
211+ path . join ( 'output/path/' , 'b.chunk.bundle' ) ,
212+ path . join ( 'output/path/' , 'b.chunk.bundle' ) ,
213+ ] ) ;
214+ } ) ;
215+
150216 describe ( 'getHermesCLIPath' , ( ) => {
151217 const reactNativePath = 'path/to/react-native' ;
152218
0 commit comments