1
- const CONFIG = {
2
- isAutoFixJs :true
3
- }
4
- const MODULE_NOT_FOUND = 1 ;
5
- // const MODULE_COMPLETA = 2;
6
- const MODULE_LOADING = 3 ;
7
- const MODULE_DONE = 4 ;
8
-
9
-
10
- console . log = function ( ...arg ) {
11
- console . log ( `%c${ arg } ` , 'color:back;font-weight:bold;' )
12
- }
13
- console . error = function ( ...arg ) {
14
- console . error ( `%c${ arg } ` , `color:red;font-weight:bold;` )
15
- }
16
- console . info = function ( ...arg ) {
17
- console . info ( `%c${ arg } ` , `color:green;` )
18
- }
19
-
20
- //load function
21
-
22
- function CreateLoad ( url ) {
23
- let scrContainer = document . createElement ( 'script' ) ;
24
- let head = document . querySelector ( 'head' ) ;
25
- scrContainer . src = url ;
26
- head . appendChild ( scrContainer )
27
-
28
- }
29
- const helpis = ( obj , param ) => {
30
- return Object . prototype . toString . call ( obj ) === `[object ${ param } ]` ;
31
- }
32
-
33
- const regexpList = [ ]
34
- const NowPath = window . location . origin ;
35
- //fix deps path
36
- const pathname = window . location . pathname ;
37
- const pathnameList = pathname . split ( '/' ) . map ( ( a , index , obj ) => {
38
- // return a !== '' &&
39
- if ( index == obj . length - 1 ) {
40
- return ! ( / ' j s | p h p | h t m l | j s p | a s p | c s s ' / . test ( a ) ) && a !== '' ;
1
+ ( function ( root , factory ) {
2
+ if ( typeof define === 'function' && define . amd ) {
3
+ define ( function ( ) {
4
+ return factory ( root ) ;
5
+ } ) ;
6
+ } else if ( typeof exports === 'object' ) {
7
+ module . exports = factory ;
8
+ } else {
9
+ root . KLoader = factory ( root ) ;
41
10
}
42
- return a !== '' ;
43
- } )
44
- const isIndex = ( pathname === '' ) ;
45
-
46
- function resolvePath ( filename , url = NowPath ) {
47
- // url = window.location.origin;
11
+ } ) ( this , function ( root ) {
12
+ 'use strict' ;
13
+ const CONFIG = {
14
+ isAutoFixJs :true
15
+ }
16
+ const MODULE_NOT_FOUND = 1
17
+ const MODULE_READY = 2
18
+ const MODULE_LOADING = 3
19
+ const MODULE_DONE = 4
48
20
49
- // let NowPath = window.location.origin;
21
+ const consoleColor = ( color ) => `color: ${ color } ;font-weight:bold;font-size: 16px;`
50
22
51
- if ( url . test ( / ^ [ h t t p | h t t p s ] / ) ) {
52
- return url ;
23
+ const log = function ( ... arg ) {
24
+ console . log ( `%c ${ arg } ` , consoleColor ( `#ccc` ) )
53
25
}
54
- if ( url . test ( / ^ \. (? = \. ) / ) && isIndex ) {
55
- throw new TypeError ( 'path first must not a ..' ) ;
26
+ const error = function ( ... arg ) {
27
+ console . error ( `%c ${ arg } ` , consoleColor ( `red` ) )
56
28
}
57
- let pathList = pathname . split ( '/' ) ;
58
- let nowpath = pathnameList . slice ( 0 ) ;
59
- for ( let i of pathList ) {
60
- if ( i === '..' ) {
61
- if ( nowpath . length == 0 ) {
62
- throw new TypeError ( 'too much ..' ) ;
63
- }
64
- nowpath . length = nowpath . length - 1 ;
65
- } else if ( i === '.' ) {
66
- } else {
67
- nowpath . push ( i ) ;
68
- }
29
+ const info = function ( ...arg ) {
30
+ console . info ( `%c${ arg } ` , consoleColor ( `green` ) )
69
31
}
70
- return nowpath . reduce ( ( a , b ) => {
71
- return a + '/' + b ;
72
- } , NowPath + '/' )
73
- }
74
-
75
- function pathToHash ( string ) {
76
- return string . split ( '' ) . reduce ( ( a , b ) => {
77
- return a + b . toString ( ) . charCodeAt ( )
78
- } , '' ) ;
79
- }
80
- //
81
32
82
- /**
83
- * 模块存放位置
84
- */
85
- const MODULES = {
86
- NAME :'xx.js' ,
87
- dependeni : [ ] ,
88
- node
89
- }
90
- const CACHE_MODULES = {
91
- '123123123123123' :{ }
92
- }
93
- const getRequireRegExp = / = ( \s + ) r e q u i r e ( \s * ) (? = \( \s * [ ' | " ] ( [ \w | \s | \. ] + ) [ ' | " ] ) / g;
94
-
95
- function fixdepList ( ) {
96
-
97
- }
98
- //fix loop deps
99
- /**
100
- * MODULE = {
101
- * 'xxxx':{
102
- * path:'url',
103
- * export:{}
104
- * }
105
- * }
106
- */
33
+ //load function
34
+ function CreateLoad ( url ) {
35
+ console . log ( url )
36
+ return new Promise ( ( resolve , reject ) => {
37
+ let scrContainer = document . createElement ( 'script' ) ;
38
+ let head = document . querySelector ( 'head' ) ;
39
+ scrContainer . src = url ;
40
+ scrContainer . onload = function ( ) {
41
+ resolve ( url )
42
+ }
43
+ scrContainer . onerror = function ( e ) {
44
+ reject ( e , url )
45
+ }
46
+ head . appendChild ( scrContainer )
47
+ } )
48
+ }
107
49
108
- KLoader . defined = function ( moduleName , depsList , callback ) {
109
- if ( arguments . length == 1 ) {
110
- if ( typeof moduleName == 'String' ) {
111
- return { } ;
50
+ const helpis = ( obj , param ) => {
51
+ return Object . prototype . toString . call ( obj ) === `[object ${ param } ]` ;
52
+ }
53
+ //fix deps path
54
+ const regexpList = [ ]
55
+ const NowPath = window . location . origin ;
56
+ const pathname = window . location . pathname ;
57
+ const pathnameList = pathname . split ( '/' ) . filter ( ( a , index , obj ) => {
58
+ if ( index == obj . length - 1 ) {
59
+ return ! ( / ' j s | p h p | h t m l | j s p | a s p | c s s ' / . test ( a ) ) && a !== ''
112
60
}
113
- if ( typeof moduleName == 'function' ) {
114
- callback = moduleName ;
115
- depsList = [ ] ;
116
- moduleName = '' ;
61
+ return a !== ''
62
+ } )
63
+ const isIndex = ( pathname === '' )
64
+
65
+ function resolvePath ( filename , targetLink = NowPath ) {
66
+ if ( / ^ \. (? = \. ) / . test ( targetLink ) && isIndex ) {
67
+ error ( 'path first must not a ..' )
68
+ throw new TypeError ( 'path first must not a ..' )
117
69
}
118
- if ( typeof moduleName == 'object' ) {
119
-
70
+ let tempPath = NowPath + '/'
71
+ let pathList = filename . split ( '/' )
72
+ let nowpath = pathnameList . slice ( 0 ) ;
73
+ for ( let i of pathList ) {
74
+ if ( i === '..' ) {
75
+ if ( nowpath . length == 0 ) {
76
+ throw new TypeError ( 'too much ..' )
77
+ }
78
+ nowpath . length = nowpath . length - 1
79
+ } else if ( i === '.' ) {
80
+ } else {
81
+ nowpath . push ( i )
82
+ }
120
83
}
84
+ return nowpath . reduce ( ( a , b ) => `${ a } /${ b } ` , tempPath )
121
85
}
122
- if ( arguments . length == 2 ) {
123
86
87
+ function pathToHash ( url ) {
88
+ return resolvePath ( url ) . split ( '' ) . reduce ( ( a , b ) => {
89
+ return a + b . toString ( ) . charCodeAt ( )
90
+ } , '' ) ;
124
91
}
125
-
126
- //fix CommondJS
127
-
128
- }
129
-
130
-
131
- KLoader . require = function ( name ) {
132
- let path = resolvePath ( ) ;
133
- let pathHash = pathToHash ( path ) ;
134
-
135
- if ( CACHE_MODULES [ pathHash ] ) {
136
- console . error ( `the module doesn't load` ) ;
137
- return { } ;
92
+ //
93
+ let KLoader = { }
94
+ /**
95
+ * 模块存放位置
96
+ */
97
+ const MODULES = {
138
98
}
139
-
140
- return CACHE_MODULES [ pathHash ] ;
141
- }
142
-
143
- KLoader . load = function ( url ) {
144
- let link = resolvePath ( url ) ;
145
- let node = document . createElement ( 'script' ) ;
146
- let head = document . createElement ( 'head' ) [ 0 ] ;
147
-
148
-
149
- // let supportOnload
150
- node . charset = 'utf-8' ;
151
- node . type = 'text/javascript' ;
152
- node . onload = function ( ) {
153
-
99
+ const CACHE_MODULES = {
154
100
}
155
- node . onerror = function ( ) {
101
+ // 用于鉴别
102
+ const getRequireRegExp = / = ( \s + ) r e q u i r e ( \s * ) (? = \( \s * [ ' | " ] ( [ \w | \s | \. ] + ) [ ' | " ] ) / g;
156
103
104
+ KLoader . defined = function ( moduleName , depsList , callback ) {
105
+ if ( moduleName && ! depsList && ! callback ) {
106
+ MODULES [ pathToHash ( moduleName ) ] = { }
107
+ return
108
+ }
109
+ if ( moduleName && helpis ( depsList , 'Function' ) ) {
110
+ moduleName = pathToHash ( moduleName ) ;
111
+ callback = depsList
112
+ MODULES [ moduleName ] = callback ( )
113
+ return
114
+ }
115
+ checkDeepIsCanBeUse ( depsList ) . then ( ( ) => {
116
+ const args = depsList . map ( v => MODULES [ pathToHash ( v ) ] )
117
+ MODULES [ pathToHash ( moduleName ) ] = callback . apply ( null , args )
118
+ } )
157
119
}
158
- node . src = url ;
159
- // node.src = 'test.js'
160
-
161
- head . appendChild ( node ) ;
162
- }
163
120
164
- function fixDepDenpend ( func ) {
165
-
166
- }
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
- // function parseToMoney(money){
191
- // if(/^[0-9]*(\.[0-9]*.)/.test(123.123))return throw new TypeError('error')
192
- // var str = money.toString()
193
- // var temp = str.split('.')
194
- // temp[0].reduce(()=>{})
195
- // }
121
+ const checkDeepIsCanBeUse = ( list ) => {
122
+ const listRequire = list . map ( v => KLoader . require ( v ) )
123
+ return Promise . all ( listRequire )
124
+ }
196
125
126
+ KLoader . require = function ( name ) {
127
+ let pathHash = pathToHash ( name ) ;
128
+ if ( ! MODULES [ pathHash ] ) {
129
+ return CreateLoad ( name ) . then ( v => v )
130
+ }
131
+ return MODULES [ pathHash ] ;
132
+ }
197
133
198
- function parseToMoney ( money ) {
199
- if ( Number ( money ) !== money ) throw new TypeError ( 'error type' )
200
- var str = money . toString ( )
201
- var temp = str . split ( '.' )
202
- var _temp = temp [ 0 ] . split ( '' ) . reverse ( ) . join ( '' )
203
- while ( / \d { 4 } ?/ . test ( _temp ) ) {
204
- _temp = _temp . replace ( / ( \d { 3 } ) (? = \d ) / , function ( word ) {
205
- return RegExp . $1 + ','
134
+ KLoader . load = function ( url , callback ) {
135
+ return CreateLoad ( url ) . then ( ( v ) => {
136
+ v = pathToHash ( v )
137
+ if ( MODULES [ v ] === undefined ) {
138
+ MODULES [ v ] = { }
139
+ }
140
+ MODULES [ v ] . loaded = true
141
+ return v
142
+ } ) . then ( v => {
143
+ callback ( MODULES [ v ] )
206
144
} )
207
145
}
208
- // console.log(_temp)
209
- if ( ! ! temp [ 1 ] ) {
210
- temp [ 0 ] = _temp . split ( '' ) . reverse ( ) . join ( '' )
211
- }
212
-
213
- return temp . join ( '.' )
214
- }
215
- console . log ( parseToMoney ( 13112111123 ) )
216
- // function parseToMoney(money){
217
- // if(Number(money)!==money)throw new TypeError('error type')
218
- // var temp = money.toString()
219
- // var str = temp.split('.')[0]
220
- // str = str.split('').reverse().join('')
221
- // console.log(str)
222
- // while(!(/\,\d{3}\./.test(str)||/\,\d{3}$/.test(str))){
223
- // str = str.replace(/(\d{3})(?=\d)(!\.\d+)*/,function(){return RegExp.$1+','})
224
- // }
225
- // return str.split('').reverse().join('')
226
- // }
227
- console . log ( parseToMoney ( 13112111123 ) )
146
+
147
+ return KLoader
148
+ } )
0 commit comments