1+ import path from 'node:path' ;
2+
13import { HeadersProps , UserscriptPlugin } from 'webpack-userscript' ;
4+ import * as fs from 'webpack-userscript/fs' ;
25
36import { compile , watchCompile } from '../util' ;
47import { Volume } from '../volume' ;
@@ -77,13 +80,11 @@ describe('load-headers', () => {
7780 headers : './headers.json' ,
7881 } ) ;
7982
80- const loadFromHeadersFile = jest . spyOn (
81- plugin . features [ 0 ] ,
82- 'loadFromHeadersFile' as any ,
83- ) ;
83+ const readJSONSpy = jest . spyOn ( fs , 'readJSON' ) ;
8484
8585 const entry = './entry.js' ;
8686 let step = 0 ;
87+ let inputFullPath = '' ;
8788
8889 await watchCompile (
8990 input ,
@@ -93,16 +94,16 @@ describe('load-headers', () => {
9394 entry,
9495 plugins : [ plugin ] ,
9596 } ,
96- async ( { output, writeFile } ) => {
97+ async ( { output, writeFile, cwd } ) => {
9798 switch ( ++ step ) {
9899 case 1 :
99- await writeFile ( entry , Fixtures . entryJs ) ;
100100 expect ( output . toJSON ( ) ) . toEqual ( {
101101 '/dist/output.user.js' : Fixtures . entryUserJs (
102102 Fixtures . loadHeadersHeaders ,
103103 ) ,
104104 '/dist/output.meta.js' : Fixtures . loadHeadersHeaders ,
105105 } ) ;
106+ await writeFile ( entry , Fixtures . entryJs ) ;
106107 break ;
107108
108109 case 2 :
@@ -112,6 +113,8 @@ describe('load-headers', () => {
112113 fail ( 'invalid steps' ) ;
113114 }
114115
116+ inputFullPath = cwd ;
117+
115118 return step < 2 ;
116119 } ,
117120 ) ;
@@ -120,7 +123,17 @@ describe('load-headers', () => {
120123 fail ( 'invalid steps' ) ;
121124 }
122125
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 ( ) ;
124137 } ) ;
125138 } ) ;
126139
@@ -189,4 +202,31 @@ describe('load-headers', () => {
189202 } ) ;
190203 } ) ;
191204 } ) ;
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+ } ) ;
192232} ) ;
0 commit comments