1+ const { experiments, HtmlRspackPlugin } = require ( "@rspack/core" ) ;
2+ const HtmlWebpackPlugin = require ( "html-webpack-plugin" ) ;
3+ const fs = require ( "fs" ) ;
4+ const path = require ( "path" ) ;
5+
6+ /** @type {import("@rspack/core").Configuration } */
7+ module . exports = ( _ , { testPath } ) => ( [ {
8+ target : "web" ,
9+ output : {
10+ publicPath : "http://localhost:3000/" ,
11+ chunkFilename : "[name].0.js" ,
12+ crossOriginLoading : "anonymous" ,
13+ } ,
14+ plugins : [
15+ new experiments . SubresourceIntegrityPlugin ( ) ,
16+ new HtmlRspackPlugin ( {
17+ filename : "index.html" ,
18+ } ) ,
19+ {
20+ apply ( compiler ) {
21+ compiler . hooks . compilation . tap ( 'TestPlugin' , ( compilation ) => {
22+ HtmlRspackPlugin . getCompilationHooks ( compilation ) . beforeAssetTagGeneration . tap ( 'SubresourceIntegrityPlugin' , ( data ) => {
23+ data . assets . js . push ( "//localhost:3000/chunk.0.js" ) ;
24+ data . assets . js . push ( "http://localhost:3000/chunk.0.js" ) ;
25+ data . assets . js . push ( "//rspack.dev/chunk.0.js" ) ;
26+ data . assets . js . push ( "http://rspack.dev/chunk.0.js" ) ;
27+ } ) ;
28+ } ) ;
29+ }
30+ } ,
31+ {
32+ apply ( compiler ) {
33+ compiler . hooks . done . tap ( 'TestPlugin' , ( ) => {
34+ const htmlContent = fs . readFileSync ( path . resolve ( testPath , "index.html" ) , "utf-8" ) ;
35+ expect ( htmlContent ) . toMatch ( / < s c r i p t c r o s s o r i g i n d e f e r i n t e g r i t y = " .+ " s r c = " \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 0 \. j s " > / ) ;
36+ expect ( htmlContent ) . toMatch ( / < s c r i p t c r o s s o r i g i n d e f e r i n t e g r i t y = " .+ " s r c = " h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 0 \. j s " > / ) ;
37+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r s r c = " \/ \/ r s p a c k .d e v \/ c h u n k \. 0 \. j s " > / ) ;
38+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r s r c = " h t t p : \/ \/ r s p a c k .d e v \/ c h u n k \. 0 \. j s " > / ) ;
39+ } ) ;
40+ }
41+ }
42+ ] ,
43+ } , {
44+ target : "web" ,
45+ output : {
46+ publicPath : "http://localhost:3000/" ,
47+ chunkFilename : "[name].1.js" ,
48+ crossOriginLoading : "anonymous" ,
49+ } ,
50+ plugins : [
51+ new experiments . SubresourceIntegrityPlugin ( {
52+ htmlPlugin : require . resolve ( "html-webpack-plugin" ) ,
53+ } ) ,
54+ new HtmlWebpackPlugin ( {
55+ filename : "index1.html" ,
56+ } ) ,
57+ {
58+ apply ( compiler ) {
59+ compiler . hooks . compilation . tap ( 'TestPlugin' , ( compilation ) => {
60+ HtmlWebpackPlugin . getCompilationHooks ( compilation ) . beforeAssetTagGeneration . tap ( 'SubresourceIntegrityPlugin' , ( data ) => {
61+ data . assets . js . push ( "//localhost:3000/chunk.1.js" ) ;
62+ data . assets . js . push ( "http://localhost:3000/chunk.1.js" ) ;
63+ data . assets . js . push ( "//rspack.dev/chunk.1.js" ) ;
64+ data . assets . js . push ( "http://rspack.dev/chunk.1.js" ) ;
65+ } ) ;
66+ } ) ;
67+ }
68+ } ,
69+ {
70+ apply ( compiler ) {
71+ compiler . hooks . done . tap ( 'TestPlugin' , ( ) => {
72+ const htmlContent = fs . readFileSync ( path . resolve ( testPath , "index1.html" ) , "utf-8" ) ;
73+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 1 \. j s " i n t e g r i t y = " .+ " c r o s s o r i g i n = " a n o n y m o u s " > / ) ;
74+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 1 \. j s " i n t e g r i t y = " .+ " c r o s s o r i g i n = " a n o n y m o u s " > / ) ;
75+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " \/ \/ r s p a c k .d e v \/ c h u n k \. 1 \. j s " > / ) ;
76+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " h t t p : \/ \/ r s p a c k .d e v \/ c h u n k \. 1 \. j s " > / ) ;
77+ } ) ;
78+ }
79+ }
80+ ] ,
81+ } , {
82+ target : "web" ,
83+ output : {
84+ publicPath : "//localhost:3000/" ,
85+ chunkFilename : "[name].2.js" ,
86+ crossOriginLoading : "anonymous" ,
87+ } ,
88+ plugins : [
89+ new experiments . SubresourceIntegrityPlugin ( ) ,
90+ new HtmlRspackPlugin ( {
91+ filename : "index2.html" ,
92+ } ) ,
93+ {
94+ apply ( compiler ) {
95+ compiler . hooks . compilation . tap ( 'TestPlugin' , ( compilation ) => {
96+ HtmlRspackPlugin . getCompilationHooks ( compilation ) . beforeAssetTagGeneration . tap ( 'SubresourceIntegrityPlugin' , ( data ) => {
97+ data . assets . js . push ( "//localhost:3000/chunk.2.js" ) ;
98+ data . assets . js . push ( "http://localhost:3000/chunk.2.js" ) ;
99+ data . assets . js . push ( "//rspack.dev/chunk.2.js" ) ;
100+ data . assets . js . push ( "http://rspack.dev/chunk.2.js" ) ;
101+ } ) ;
102+ } ) ;
103+ }
104+ } ,
105+ {
106+ apply ( compiler ) {
107+ compiler . hooks . done . tap ( 'TestPlugin' , ( ) => {
108+ const htmlContent = fs . readFileSync ( path . resolve ( testPath , "index2.html" ) , "utf-8" ) ;
109+ expect ( htmlContent ) . toMatch ( / < s c r i p t c r o s s o r i g i n d e f e r i n t e g r i t y = " .+ " s r c = " \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 2 \. j s " > / ) ;
110+ expect ( htmlContent ) . toMatch ( / < s c r i p t c r o s s o r i g i n d e f e r i n t e g r i t y = " .+ " s r c = " h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 2 \. j s " > / ) ;
111+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r s r c = " \/ \/ r s p a c k .d e v \/ c h u n k \. 2 \. j s " > / ) ;
112+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r s r c = " h t t p : \/ \/ r s p a c k .d e v \/ c h u n k \. 2 \. j s " > / ) ;
113+ } ) ;
114+ }
115+ }
116+ ] ,
117+ } , {
118+ target : "web" ,
119+ output : {
120+ publicPath : "//localhost:3000/" ,
121+ chunkFilename : "[name].3.js" ,
122+ crossOriginLoading : "anonymous" ,
123+ } ,
124+ plugins : [
125+ new experiments . SubresourceIntegrityPlugin ( {
126+ htmlPlugin : require . resolve ( "html-webpack-plugin" ) ,
127+ } ) ,
128+ new HtmlWebpackPlugin ( {
129+ filename : "index3.html" ,
130+ } ) ,
131+ {
132+ apply ( compiler ) {
133+ compiler . hooks . compilation . tap ( 'TestPlugin' , ( compilation ) => {
134+ HtmlWebpackPlugin . getCompilationHooks ( compilation ) . beforeAssetTagGeneration . tap ( 'SubresourceIntegrityPlugin' , ( data ) => {
135+ data . assets . js . push ( "//localhost:3000/chunk.3.js" ) ;
136+ data . assets . js . push ( "http://localhost:3000/chunk.3.js" ) ;
137+ data . assets . js . push ( "//rspack.dev/chunk.3.js" ) ;
138+ data . assets . js . push ( "http://rspack.dev/chunk.3.js" ) ;
139+ } ) ;
140+ } ) ;
141+ }
142+ } ,
143+ {
144+ apply ( compiler ) {
145+ compiler . hooks . done . tap ( 'TestPlugin' , ( ) => {
146+ const htmlContent = fs . readFileSync ( path . resolve ( testPath , "index3.html" ) , "utf-8" ) ;
147+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 3 \. j s " i n t e g r i t y = " .+ " c r o s s o r i g i n = " a n o n y m o u s " > / ) ;
148+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ c h u n k \. 3 \. j s " i n t e g r i t y = " .+ " c r o s s o r i g i n = " a n o n y m o u s " > / ) ;
149+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " \/ \/ r s p a c k .d e v \/ c h u n k \. 3 \. j s " > / ) ;
150+ expect ( htmlContent ) . toMatch ( / < s c r i p t d e f e r = " d e f e r " s r c = " h t t p : \/ \/ r s p a c k .d e v \/ c h u n k \. 3 \. j s " > / ) ;
151+ } ) ;
152+ }
153+ }
154+ ] ,
155+ } ] ) ;
0 commit comments