1
1
import RegisterLoader from 'es-module-loader/core/register-loader.js' ;
2
- import { isBrowser , isNode , global , baseURI } from 'es-module-loader/core/common.js' ;
2
+ import { isBrowser , isNode , global , baseURI , fileUrlToPath } from 'es-module-loader/core/common.js' ;
3
3
import { resolveUrlToParentIfNotPlain } from 'es-module-loader/core/resolve.js' ;
4
- import { scriptLoad , nodeFetch } from 'es-module-loader/core/fetch.js' ;
5
4
6
5
/*
7
6
* Example System Register loader
@@ -36,26 +35,87 @@ SystemRegisterLoader.prototype.normalize = function(key, parent, metadata) {
36
35
return key ;
37
36
} ;
38
37
38
+ var fs ;
39
+
39
40
// instantiate just needs to run System.register
40
41
// so we load the module name as a URL, and expect that to run System.register
41
42
SystemRegisterLoader . prototype . instantiate = function ( key , metadata ) {
42
- var loader = this ;
43
+ var thisLoader = this ;
43
44
44
45
return new Promise ( function ( resolve , reject ) {
45
46
if ( isNode )
46
- nodeFetch ( key , undefined , function ( source ) {
47
- eval ( source ) ;
48
- loader . processRegisterContext ( key ) ;
49
- resolve ( ) ;
50
- } , reject ) ;
47
+ Promise . resolve ( fs || ( fs = typeof require !== 'undefined' ? require ( 'fs' ) : loader . import ( 'fs' ) . then ( m => m . default ) ) )
48
+ . then ( function ( fs ) {
49
+ console . log ( fs ) ;
50
+ fs . readFile ( fileUrlToPath ( key ) , function ( err , source ) {
51
+ if ( err )
52
+ return reject ( err ) ;
53
+
54
+ ( 0 , eval ) ( source ) ;
55
+ thisLoader . processRegisterContext ( key ) ;
56
+ resolve ( ) ;
57
+ } ) ;
58
+ } ) ;
51
59
else if ( isBrowser )
52
60
scriptLoad ( key , function ( ) {
53
- loader . processRegisterContext ( key ) ;
61
+ thisLoader . processRegisterContext ( key ) ;
54
62
resolve ( ) ;
55
63
} , reject ) ;
56
64
else
57
65
throw new Error ( 'No fetch system defined for this environment.' ) ;
58
66
} ) ;
59
67
} ;
60
68
69
+ function nodeFetch ( url , authorization , fulfill , reject ) {
70
+ if ( url . substr ( 0 , 8 ) != 'file:///' )
71
+ throw new Error ( 'Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.' ) ;
72
+ fs = fs || module . require ( 'fs' ) ;
73
+ if ( isWindows )
74
+ url = url . replace ( / \/ / g, '\\' ) . substr ( 8 ) ;
75
+ else
76
+ url = url . substr ( 7 ) ;
77
+ return fs . readFile ( url , function ( err , data ) {
78
+ if ( err ) {
79
+ return reject ( err ) ;
80
+ }
81
+ else {
82
+ // Strip Byte Order Mark out if it's the leading char
83
+ var dataString = data + '' ;
84
+ if ( dataString [ 0 ] === '\ufeff' )
85
+ dataString = dataString . substr ( 1 ) ;
86
+
87
+ fulfill ( dataString ) ;
88
+ }
89
+ } ) ;
90
+ }
91
+
92
+ function scriptLoad ( src , resolve , reject ) {
93
+ var script = document . createElement ( 'script' ) ;
94
+ script . type = 'text/javascript' ;
95
+ script . charset = 'utf-8' ;
96
+ script . async = true ;
97
+
98
+ script . addEventListener ( 'load' , load , false ) ;
99
+ script . addEventListener ( 'error' , error , false ) ;
100
+
101
+ script . src = src ;
102
+ document . head . appendChild ( script ) ;
103
+
104
+ function load ( ) {
105
+ resolve ( ) ;
106
+ cleanup ( ) ;
107
+ }
108
+
109
+ function error ( err ) {
110
+ cleanup ( ) ;
111
+ reject ( new Error ( 'Fetching ' + src ) ) ;
112
+ }
113
+
114
+ function cleanup ( ) {
115
+ script . removeEventListener ( 'load' , load , false ) ;
116
+ script . removeEventListener ( 'error' , error , false ) ;
117
+ document . head . removeChild ( script ) ;
118
+ }
119
+ }
120
+
61
121
export default SystemRegisterLoader ;
0 commit comments