@@ -5,57 +5,60 @@ const mime = require('mime-types');
5
5
6
6
const local = new URL ( 'http://local/' ) ;
7
7
8
- async function createPage ( { files } ) {
8
+ async function createPage ( { files, processors = { } } ) {
9
9
10
- async function getFile ( url , encoding ) {
10
+ async function getFile ( url ) {
11
11
12
12
const { origin, pathname } = new URL ( url ) ;
13
13
14
14
if ( origin !== local . origin )
15
15
return null
16
16
17
+ let body = files [ pathname ]
18
+ if ( processors [ pathname ] ) {
19
+ body = processors [ pathname ] ( body )
20
+ }
21
+
17
22
const res = {
18
23
contentType : mime . lookup ( Path . extname ( pathname ) ) || '' ,
19
- body : files [ pathname ] ,
24
+ body,
20
25
status : files [ pathname ] === undefined ? 404 : 200 ,
21
26
} ;
22
27
23
28
return res ;
24
29
}
25
30
26
31
const page = await browser . newPage ( ) ;
27
-
28
32
page . setDefaultTimeout ( 3000 ) ;
29
33
30
-
31
34
await page . setRequestInterception ( true ) ;
32
35
page . on ( 'request' , async interceptedRequest => {
33
-
34
- // console.log(interceptedRequest.url())
35
-
36
36
try {
37
37
38
38
const file = await getFile ( interceptedRequest . url ( ) , 'utf-8' ) ;
39
- if ( file !== null ) {
40
-
39
+ if ( file ) {
41
40
return void interceptedRequest . respond ( {
42
41
...file ,
43
42
contentType : file . contentType + '; charset=utf-8' ,
44
43
} ) ;
45
44
}
46
45
47
46
interceptedRequest . continue ( ) ;
48
-
49
47
} catch ( ex ) {
50
-
51
48
page . emit ( 'pageerror' , ex )
52
49
}
53
50
} ) ;
54
51
55
52
const output = [ ] ;
56
53
57
- page . on ( 'console' , async msg => output . push ( { type : msg . type ( ) , content : await Promise . all ( msg . args ( ) . map ( e => e . jsonValue ( ) ) ) } ) ) ;
58
- page . on ( 'pageerror' , error => output . push ( { type : 'pageerror' , content : error } ) ) ;
54
+ page . on ( 'console' , async msg => {
55
+ console . log ( msg )
56
+ output . push ( { type : msg . type ( ) , text : msg . text ( ) , content : await Promise . all ( msg . args ( ) . map ( e => e . jsonValue ( ) ) ) } )
57
+ } ) ;
58
+ page . on ( 'pageerror' , error => {
59
+ console . log ( error )
60
+ output . push ( { type : 'pageerror' , content : error } )
61
+ } ) ;
59
62
60
63
page . on ( 'error' , msg => console . log ( 'ERROR' , msg ) ) ;
61
64
@@ -99,7 +102,6 @@ afterAll(async () => {
99
102
const defaultFiles = {
100
103
'/vue3-sfc-loader.js' : Fs . readFileSync ( Path . join ( __dirname , '../dist/vue3-sfc-loader.js' ) , { encoding : 'utf-8' } ) ,
101
104
'/vue' : Fs . readFileSync ( Path . join ( __dirname , '../node_modules/vue/dist/vue.global.js' ) , { encoding : 'utf-8' } ) ,
102
-
103
105
'/options.js' : `
104
106
105
107
class HttpError extends Error {
@@ -132,7 +134,6 @@ const defaultFiles = {
132
134
},
133
135
134
136
getFile(path) {
135
-
136
137
return fetch(path).then(res => res.ok ? res.text() : Promise.reject(new HttpError(path, res)));
137
138
},
138
139
@@ -151,7 +152,9 @@ const defaultFiles = {
151
152
152
153
export default options;
153
154
` ,
154
-
155
+ '/optionsOverride.js' : `
156
+ export default () => {};
157
+ ` ,
155
158
'/boot.js' : `
156
159
export default ({ options, createApp, mountApp }) => mountApp( createApp(options) );
157
160
` ,
@@ -161,10 +164,12 @@ const defaultFiles = {
161
164
<html><body>
162
165
<script src="vue"></script>
163
166
<script src="vue3-sfc-loader.js"></script>
167
+ <!-- scripts -->
164
168
<script type="module">
165
169
166
170
import boot from '/boot.js'
167
171
import options from '/options.js'
172
+ import optionsOverride from '/optionsOverride.js'
168
173
169
174
const { loadModule } = window['vue3-sfc-loader'];
170
175
@@ -185,6 +190,8 @@ const defaultFiles = {
185
190
186
191
return app.mount('#' + eltId);
187
192
}
193
+
194
+ optionsOverride(options)
188
195
189
196
boot({ options, createApp, mountApp, Vue });
190
197
@@ -199,6 +206,9 @@ const defaultFilesVue2 = {
199
206
'/vue2-sfc-loader.js' : Fs . readFileSync ( Path . join ( __dirname , '../dist/vue2-sfc-loader.js' ) , { encoding : 'utf-8' } ) ,
200
207
'/vue' : Fs . readFileSync ( Path . join ( __dirname , '../node_modules/vue2/dist/vue.js' ) , { encoding : 'utf-8' } ) ,
201
208
'/options.js' : defaultFiles [ '/options.js' ] ,
209
+ '/optionsOverride.js' : `
210
+ export default () => {};
211
+ ` ,
202
212
'/boot.js' : `
203
213
export default ({ options, createApp, mountApp }) => createApp(options).then(app => mountApp(app));
204
214
` ,
@@ -207,10 +217,12 @@ const defaultFilesVue2 = {
207
217
<html><body>
208
218
<script src="vue"></script>
209
219
<script src="vue2-sfc-loader.js"></script>
220
+ <!-- scripts -->
210
221
<script type="module">
211
222
212
223
import boot from '/boot.js'
213
224
import options from '/options.js'
225
+ import optionsOverride from '/optionsOverride.js'
214
226
215
227
const { loadModule } = window['vue2-sfc-loader'];
216
228
@@ -236,6 +248,8 @@ const defaultFilesVue2 = {
236
248
return app.$mount('#' + mountElId);
237
249
}
238
250
251
+ optionsOverride(options)
252
+
239
253
boot({ options, createApp, mountApp, Vue });
240
254
241
255
//window._done && window._done();
0 commit comments