2
2
* 模块化系统的支持类
3
3
*/
4
4
5
+ import { transpiler } from "./transpiler" ;
6
+
5
7
class Module {
6
8
hook : Promise < any > // 加载状态
7
9
_content : any // 模块内容
@@ -60,52 +62,40 @@ const importCSS = async function(url: string) {
60
62
} )
61
63
}
62
64
63
- /**
64
- * export的工厂函数, 返回一个export函数供模块使用
65
- * @param {string } path
66
- * @returns {(obj: any) => void }
67
- */
68
- const _export = function ( path : string ) : ( obj : any ) => void {
69
- return ( m ) => {
70
- if ( cache [ path ] . content ) {
71
- throw Error ( `在导入${ path } 时出现了错误: 模块进行了多次导出` ) ;
72
- }
73
- cache [ path ] . _content = m ;
74
- }
75
- }
76
-
77
- const depReg = / \/ \/ \/ * < a m d - d e p e n d e n c y + p a t h = [ " ' ] ( [ \w \. \/ - ] + ) [ " ' ] ( + n a m e = [ " ' ] ( [ \w $ ] + ) [ " ' ] ) ? * \/ > / g;
78
-
79
- const processJS = async function ( script : string , url : string ) {
80
- let res : RegExpExecArray ;
81
- const deps = [ ] ;
65
+ const processJS = async function ( rawscript : string , url : string ) {
82
66
const headstrs = [ ] ;
83
- console . log ( script ) ;
84
- // @ts -ignore
85
- AMEF . __temp__ [ url ] = { } ;
86
- while ( res = depReg . exec ( script ) ) {
87
- const path = res [ 1 ] , name = res [ 3 ] ;
88
- deps . push ( _import ( path , url ) . then ( ( m ) => {
89
- console . log ( path , name ) ;
90
- if ( name ) {
91
- headstrs . push ( `var ${ name } = AMEF.__temp__["${ url } "]["${ path } "]\n` ) ;
92
- // @ts -ignore
93
- AMEF . __temp__ [ url ] [ path ] = m ;
94
- }
95
- } ) ) ;
96
- }
67
+ AMEF . __module__ [ url ] = { } ;
68
+ const { imports, exports, script } = transpiler ( rawscript ) ;
69
+ const deps = Object . entries ( imports || { } ) . map ( ( [ path , dep ] ) => {
70
+ if ( Array . isArray ( dep ) ) { // 有导出模块
71
+ const uri = resolvePath ( url , path ) ;
72
+ dep . forEach ( ( e ) => {
73
+ if ( Array . isArray ( e ) ) {
74
+ if ( e [ 0 ] === "*" ) { // 导出全部
75
+ headstrs . push ( `var ${ e [ 1 ] } = AMEF.__module__["${ uri } "];` ) ;
76
+ } else {
77
+ headstrs . push ( `var ${ e [ 1 ] } = AMEF.__module__["${ uri } "]["${ e [ 0 ] } "];` ) ;
78
+ }
79
+ } else {
80
+ headstrs . push ( `var ${ e } = AMEF.__module__["${ uri } "]["${ e } "];` ) ;
81
+ }
82
+ } )
83
+ }
84
+ return _import ( path , url ) ;
85
+ } ) ;
97
86
await Promise . all ( deps ) ;
98
- // @ts -ignore
99
- AMEF . __temp__ [ url ] [ "@exports" ] = _export ( url ) ;
100
87
const elm = document . createElement ( "script" ) ;
101
- console . log ( headstrs ) ;
102
- script = /* js */ `(function(exports) {
103
- /** @file ${ url } */
104
- ${ headstrs . join ( '\n' ) }
105
- ${ script }
106
- })(AMEF.__temp__["${ url } "]["@exports"])` ;
107
- elm . innerHTML = script ;
88
+ elm . innerHTML = /* js */ `"use strict";
89
+ /** @file ${ url } */
90
+ (function(exports) {
91
+ ${ headstrs . join ( '\n' ) }
92
+ ${ script }
93
+ ${ exports ? exports . map ( e => `exports.${ e } = ${ e } ;` ) . join ( '\n' ) : "" }
94
+ })(AMEF.__module__["${ url } "])
95
+ ` ;
96
+ console . log ( elm . innerHTML ) ;
108
97
document . body . appendChild ( elm ) ;
98
+ cache [ url ] . _content = AMEF . __module__ [ url ] ;
109
99
}
110
100
111
101
const importJS = async function ( url : string ) {
@@ -125,17 +115,12 @@ const importSFC = async function(url: string) {
125
115
sandbox . innerHTML = sfc ;
126
116
const script = sandbox . content . querySelector ( "script" ) . innerHTML ;
127
117
const template = sandbox . content . querySelector ( "template" ) . innerHTML ;
128
- const deps = sandbox . content . querySelectorAll ( "amd-dependency" ) ;
129
- const depOrders = [ ] ;
130
- deps . forEach ( ( e ) => {
131
- const path = e . getAttribute ( 'path' ) ;
132
- const name = e . getAttribute ( 'name' ) ;
133
- depOrders . push ( `/// <amd-dependency path="${ path } " ${ name ? 'name="' + path + "'" : "" } />` ) ;
134
- } ) ;
135
118
const styles = sandbox . content . querySelectorAll ( "style" ) ;
136
- await processJS ( depOrders . join ( '\n' ) + script , url ) ;
119
+ await processJS ( script , url ) ;
137
120
styles . forEach ( ( e ) => document . body . appendChild ( e ) ) ;
138
- cache [ url ] . _content . template = template ;
121
+ if ( template && cache [ url ] . content . default ) {
122
+ cache [ url ] . _content . default . template = template ;
123
+ }
139
124
return cache [ url ] . content ;
140
125
} )
141
126
}
@@ -163,7 +148,7 @@ export const registerLoader = function(suffix: string, loader: (path: string) =>
163
148
* @param {string } path 要引入模块的路径
164
149
* @param {string } now 当前模块的路径
165
150
*/
166
- export const _import = async function ( path : string , now = "." ) : Promise < any > {
151
+ export const _import = async function ( path : string , now : string = "." ) : Promise < any > {
167
152
const suffix = path . split ( "." ) . slice ( - 1 ) [ 0 ] ;
168
153
path = resolvePath ( now , path ) ;
169
154
console . log ( suffix , path , now ) ;
0 commit comments