1
+ import path from 'node:path' ;
2
+
1
3
import { HeadersProps , UserscriptPlugin } from 'webpack-userscript' ;
4
+ import * as fs from 'webpack-userscript/fs' ;
2
5
3
6
import { compile , watchCompile } from '../util' ;
4
7
import { Volume } from '../volume' ;
@@ -77,13 +80,11 @@ describe('load-headers', () => {
77
80
headers : './headers.json' ,
78
81
} ) ;
79
82
80
- const loadFromHeadersFile = jest . spyOn (
81
- plugin . features [ 0 ] ,
82
- 'loadFromHeadersFile' as any ,
83
- ) ;
83
+ const readJSONSpy = jest . spyOn ( fs , 'readJSON' ) ;
84
84
85
85
const entry = './entry.js' ;
86
86
let step = 0 ;
87
+ let inputFullPath = '' ;
87
88
88
89
await watchCompile (
89
90
input ,
@@ -93,16 +94,16 @@ describe('load-headers', () => {
93
94
entry,
94
95
plugins : [ plugin ] ,
95
96
} ,
96
- async ( { output, writeFile } ) => {
97
+ async ( { output, writeFile, cwd } ) => {
97
98
switch ( ++ step ) {
98
99
case 1 :
99
- await writeFile ( entry , Fixtures . entryJs ) ;
100
100
expect ( output . toJSON ( ) ) . toEqual ( {
101
101
'/dist/output.user.js' : Fixtures . entryUserJs (
102
102
Fixtures . loadHeadersHeaders ,
103
103
) ,
104
104
'/dist/output.meta.js' : Fixtures . loadHeadersHeaders ,
105
105
} ) ;
106
+ await writeFile ( entry , Fixtures . entryJs ) ;
106
107
break ;
107
108
108
109
case 2 :
@@ -112,6 +113,8 @@ describe('load-headers', () => {
112
113
fail ( 'invalid steps' ) ;
113
114
}
114
115
116
+ inputFullPath = cwd ;
117
+
115
118
return step < 2 ;
116
119
} ,
117
120
) ;
@@ -120,7 +123,17 @@ describe('load-headers', () => {
120
123
fail ( 'invalid steps' ) ;
121
124
}
122
125
123
- expect ( loadFromHeadersFile ) . toHaveBeenCalledOnce ( ) ;
126
+ const headersJsonPath = path . join ( inputFullPath , 'headers.json' ) ;
127
+
128
+ // headers.json has only been read once
129
+ expect (
130
+ readJSONSpy . mock . calls . reduce (
131
+ ( count , call ) => ( call [ 0 ] === headersJsonPath ? ++ count : count ) ,
132
+ 0 ,
133
+ ) ,
134
+ ) . toEqual ( 1 ) ;
135
+
136
+ readJSONSpy . mockRestore ( ) ;
124
137
} ) ;
125
138
} ) ;
126
139
@@ -189,4 +202,31 @@ describe('load-headers', () => {
189
202
} ) ;
190
203
} ) ;
191
204
} ) ;
205
+
206
+ describe ( 'i18n' , ( ) => {
207
+ it ( 'headers object' , async ( ) => {
208
+ const output = await compile ( input , {
209
+ ...Fixtures . webpackConfig ,
210
+ plugins : [
211
+ new UserscriptPlugin ( {
212
+ headers : {
213
+ name : 'load-headers' ,
214
+ } ,
215
+ i18n : {
216
+ en : {
217
+ name : 'load-headers-en' ,
218
+ } ,
219
+ } ,
220
+ } ) ,
221
+ ] ,
222
+ } ) ;
223
+
224
+ expect ( output . toJSON ( ) ) . toEqual ( {
225
+ '/dist/output.user.js' : Fixtures . entryUserJs (
226
+ Fixtures . loadHeadersHeaders ,
227
+ ) ,
228
+ '/dist/output.meta.js' : Fixtures . loadHeadersHeaders ,
229
+ } ) ;
230
+ } ) ;
231
+ } ) ;
192
232
} ) ;
0 commit comments