@@ -62,6 +62,13 @@ class Loader {
62
62
this . _dynamicInstantiate = undefined ;
63
63
// The index for assigning unique URLs to anonymous module evaluation
64
64
this . evalIndex = 0 ;
65
+
66
+ this . _fetchListener = undefined ;
67
+ }
68
+
69
+ setFetchListener ( listener ) {
70
+ console . log ( 'fetch listener added' , listener ) ;
71
+ this . _fetchListener = listener ;
65
72
}
66
73
67
74
async resolve ( specifier , parentURL ) {
@@ -152,7 +159,73 @@ class Loader {
152
159
}
153
160
}
154
161
162
+ async resolveToURLOnly ( specifier , parentURL ) {
163
+ try {
164
+ const { url } = await this . resolve ( specifier , parentURL ) ;
165
+ return url ;
166
+ } catch ( err ) {
167
+ const UNKNOWN_EXTENSION_PATTERN = / ^ U n k n o w n f i l e e x t e n s i o n " \. (?: [ ^ " ] * ) " f o r ( .+ ?) i m p o r t e d f r o m / ;
168
+ const MODULE_NOT_FOUND_PATTERN = / ^ C a n n o t f i n d m o d u l e ( .+ ?) i m p o r t e d f r o m / ;
169
+
170
+ if ( err . code === 'ERR_UNKNOWN_FILE_EXTENSION' ) {
171
+ const m = err . message . match ( UNKNOWN_EXTENSION_PATTERN ) ;
172
+ if ( m ) {
173
+ return pathToFileURL ( m [ 1 ] ) . href ;
174
+ }
175
+ } else if ( err . code === 'ERR_MODULE_NOT_FOUND' ) {
176
+ const m = err . message . match ( MODULE_NOT_FOUND_PATTERN ) ;
177
+ if ( m ) {
178
+ return pathToFileURL ( m [ 1 ] ) . href ;
179
+ }
180
+ }
181
+ throw err ;
182
+ }
183
+ }
184
+
185
+ async getModuleJobFromFetch ( specifier , parentURL ) {
186
+ const url = await this . resolveToURLOnly ( specifier , parentURL ) ;
187
+ let job = this . moduleMap . get ( url ) ;
188
+ // CommonJS will set functions for lazy job evaluation.
189
+ if ( typeof job === 'function' ) {
190
+ this . moduleMap . set ( url , job = job ( ) ) ;
191
+ }
192
+ if ( job !== undefined ) {
193
+ return job ;
194
+ }
195
+
196
+ const request = new Request ( url ) ;
197
+ const event = new FetchEvent ( 'fetch' , { request } ) ;
198
+ console . log ( 'calling fetch listener' , event ) ;
199
+ this . _fetchListener ( event ) ;
200
+
201
+ const loaderInstance = async ( url ) => {
202
+ const response = await ( event . responsePromise || fetch ( request ) ) ;
203
+
204
+ // TODO: Add last-minute transforms
205
+
206
+ // TODO: Check for content-type
207
+
208
+ const source = await response . text ( ) ;
209
+ const createModule = translators . get ( 'module:hack' ) ;
210
+ return createModule ( url , source ) ;
211
+ } ;
212
+
213
+ // TODO: inspectBrk checks for this
214
+ const format = 'module' ;
215
+ const inspectBrk = parentURL === undefined &&
216
+ format === 'module' && getOptionValue ( '--inspect-brk' ) ;
217
+ job = new ModuleJob ( this , url , loaderInstance , parentURL === undefined ,
218
+ inspectBrk ) ;
219
+ this . moduleMap . set ( url , job ) ;
220
+ return job ;
221
+ }
222
+
155
223
async getModuleJob ( specifier , parentURL ) {
224
+ console . log ( 'getModuleJob' , this . _fetchListener ) ;
225
+ if ( this . _fetchListener ) {
226
+ return this . getModuleJobFromFetch ( specifier , parentURL ) ;
227
+ }
228
+
156
229
const { url, format } = await this . resolve ( specifier , parentURL ) ;
157
230
let job = this . moduleMap . get ( url ) ;
158
231
// CommonJS will set functions for lazy job evaluation.
0 commit comments