1
1
import * as fs from 'fs'
2
2
import * as path from 'path'
3
+ import type { Compilation , Compiler } from '@rspack/core'
3
4
import { describe , it , expect , vi , beforeEach } from 'vitest'
4
5
import { AddAssetsToCompilation } from '../../steps/add-assets-to-compilation'
5
6
import * as utils from '../../html-lib/utils'
@@ -38,58 +39,61 @@ vi.mock('@rspack/core', () => {
38
39
} )
39
40
40
41
describe ( 'AddAssetsToCompilation' , ( ) => {
41
- let compilation : any
42
- let compiler : any
43
- let emitAssetSpy : any
44
- let getAssetSpy : any
45
- let warningsPushSpy : any
46
- let existsSyncSpy : any
47
- let readFileSyncSpy : any
48
- let processAssetsCallback : any
42
+ let compilation : Partial < Compilation >
43
+ let compiler : Partial < Compiler >
44
+ let emitAssetSpy : ReturnType < typeof vi . fn >
45
+ let getAssetSpy : ReturnType < typeof vi . fn >
46
+ let warningsPushSpy : ReturnType < typeof vi . fn >
47
+ let existsSyncSpy : ReturnType < typeof vi . spyOn >
48
+ let readFileSyncSpy : ReturnType < typeof vi . spyOn >
49
+ let processAssetsCallback : ( ) => void
49
50
50
51
beforeEach ( ( ) => {
51
52
vi . clearAllMocks ( )
52
53
53
54
emitAssetSpy = vi . fn ( )
54
55
getAssetSpy = vi . fn ( )
55
56
warningsPushSpy = vi . fn ( )
56
- existsSyncSpy = vi . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true )
57
+ existsSyncSpy = vi . spyOn ( fs , 'existsSync' ) . mockReturnValue ( true ) as any
57
58
readFileSyncSpy = vi
58
59
. spyOn ( fs , 'readFileSync' )
59
- . mockReturnValue ( Buffer . from ( 'file-content' ) )
60
+ . mockReturnValue ( Buffer . from ( 'file-content' ) ) as any
60
61
61
62
compilation = {
62
63
hooks : {
63
64
processAssets : {
64
- tap : ( _opts : any , fn : any ) => {
65
- processAssetsCallback = fn
65
+ tap : ( _opts : unknown , fn : ( assets : Record < string , any > ) => void ) => {
66
+ processAssetsCallback = ( ) => fn ( { } )
66
67
}
67
- }
68
- } ,
68
+ } as any
69
+ } as any ,
69
70
getAsset : getAssetSpy ,
70
71
emitAsset : emitAssetSpy ,
71
- warnings : { push : warningsPushSpy } ,
72
+ warnings : { push : warningsPushSpy } as any ,
72
73
errors : [ ]
73
74
}
74
75
75
76
compiler = {
76
77
hooks : {
77
78
thisCompilation : {
78
- tap : ( _name : string , fn : any ) => {
79
- fn ( compilation )
79
+ tap : (
80
+ _name : string ,
81
+ fn : ( compilation : Compilation , params : any ) => void
82
+ ) => {
83
+ fn ( compilation as Compilation , { } )
80
84
}
81
- }
82
- }
85
+ } as any
86
+ } as any
83
87
}
84
88
85
89
// Default path mocking for all tests
86
- vi . spyOn ( path , 'join' ) . mockImplementation ( ( ...args ) => {
90
+ vi . spyOn ( path , 'join' ) . mockImplementation ( ( ...args : string [ ] ) => {
87
91
return args . filter ( Boolean ) . join ( '/' )
88
92
} )
89
- vi . spyOn ( path . posix , 'join' ) . mockImplementation ( ( ...args ) => {
93
+ vi . spyOn ( path . posix , 'join' ) . mockImplementation ( ( ...args : string [ ] ) => {
90
94
return args . filter ( Boolean ) . join ( '/' )
91
95
} )
92
- vi . spyOn ( path , 'basename' ) . mockImplementation ( ( p ) => {
96
+ vi . spyOn ( path , 'basename' ) . mockImplementation ( ( p : string ) => {
93
97
if ( p === 'resource.html' ) return 'resource.html'
94
98
if ( p === 'nested/page.html' ) return 'page.html'
95
99
return p . split ( '/' ) . pop ( ) || ''
@@ -107,7 +111,7 @@ describe('AddAssetsToCompilation', () => {
107
111
if ( name === 'resource.html' ) {
108
112
return {
109
113
source : {
110
- source : ( ) => '<html><img src="foo .png"></html>'
114
+ source : ( ) => '<html><img src="extensionjs .png"></html>'
111
115
}
112
116
}
113
117
}
@@ -117,18 +121,18 @@ describe('AddAssetsToCompilation', () => {
117
121
vi . mocked ( utils . getAssetsFromHtml ) . mockReturnValue ( {
118
122
css : [ ] ,
119
123
js : [ ] ,
120
- static : [ 'foo .png' ]
124
+ static : [ 'extensionjs .png' ]
121
125
} )
122
126
vi . mocked ( utils . isFromIncludeList ) . mockReturnValue ( false )
123
127
vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
124
128
125
- plugin . apply ( compiler )
126
- processAssetsCallback ( )
129
+ plugin . apply ( compiler as Compiler )
130
+ if ( processAssetsCallback ) processAssetsCallback ( )
127
131
128
- expect ( emitAssetSpy ) . toHaveBeenCalled ( )
129
- expect ( readFileSyncSpy ) . toHaveBeenCalledWith ( 'foo .png' )
132
+ expect ( emitAssetSpy ) . toHaveBeenCalledTimes ( 1 )
133
+ expect ( readFileSyncSpy ) . toHaveBeenCalledWith ( 'extensionjs .png' )
130
134
expect ( emitAssetSpy ) . toHaveBeenCalledWith (
131
- 'assets/foo .png' ,
135
+ 'assets/extensionjs .png' ,
132
136
expect . any ( Object )
133
137
)
134
138
expect ( warningsPushSpy ) . not . toHaveBeenCalled ( )
@@ -160,7 +164,7 @@ describe('AddAssetsToCompilation', () => {
160
164
vi . mocked ( utils . isFromIncludeList ) . mockReturnValue ( false )
161
165
vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
162
166
163
- plugin . apply ( compiler )
167
+ plugin . apply ( compiler as Compiler )
164
168
processAssetsCallback ( )
165
169
166
170
expect ( path . posix . join ) . toHaveBeenCalledWith (
@@ -203,8 +207,8 @@ describe('AddAssetsToCompilation', () => {
203
207
vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
204
208
existsSyncSpy . mockReturnValue ( false )
205
209
206
- plugin . apply ( compiler )
207
- processAssetsCallback ( )
210
+ plugin . apply ( compiler as Compiler )
211
+ if ( processAssetsCallback ) processAssetsCallback ( )
208
212
209
213
expect ( warningsPushSpy ) . toHaveBeenCalled ( )
210
214
expect ( emitAssetSpy ) . not . toHaveBeenCalled ( )
@@ -238,7 +242,7 @@ describe('AddAssetsToCompilation', () => {
238
242
} )
239
243
vi . mocked ( webpackUtils . shouldExclude ) . mockReturnValue ( false )
240
244
241
- plugin . apply ( compiler )
245
+ plugin . apply ( compiler as Compiler )
242
246
processAssetsCallback ( )
243
247
244
248
expect ( warningsPushSpy ) . not . toHaveBeenCalled ( )
0 commit comments