Skip to content

Commit ccfdae6

Browse files
committed
use import/export in module
1 parent a9af4e0 commit ccfdae6

File tree

1 file changed

+37
-52
lines changed

1 file changed

+37
-52
lines changed

src/module.ts

+37-52
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* 模块化系统的支持类
33
*/
44

5+
import { transpiler } from "./transpiler";
6+
57
class Module {
68
hook: Promise<any> // 加载状态
79
_content: any // 模块内容
@@ -60,52 +62,40 @@ const importCSS = async function(url: string) {
6062
})
6163
}
6264

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 = /\/\/\/ *<amd-dependency +path=["']([\w\.\/-]+)["']( +name=["']([\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) {
8266
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+
});
9786
await Promise.all(deps);
98-
// @ts-ignore
99-
AMEF.__temp__[url]["@exports"] = _export(url);
10087
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);
10897
document.body.appendChild(elm);
98+
cache[url]._content = AMEF.__module__[url];
10999
}
110100

111101
const importJS = async function(url: string) {
@@ -125,17 +115,12 @@ const importSFC = async function(url: string) {
125115
sandbox.innerHTML = sfc;
126116
const script = sandbox.content.querySelector("script").innerHTML;
127117
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-
});
135118
const styles = sandbox.content.querySelectorAll("style");
136-
await processJS(depOrders.join('\n') + script, url);
119+
await processJS(script, url);
137120
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+
}
139124
return cache[url].content;
140125
})
141126
}
@@ -163,7 +148,7 @@ export const registerLoader = function(suffix: string, loader: (path: string) =>
163148
* @param {string} path 要引入模块的路径
164149
* @param {string} now 当前模块的路径
165150
*/
166-
export const _import = async function(path: string, now = "."): Promise<any> {
151+
export const _import = async function(path: string, now: string = "."): Promise<any> {
167152
const suffix = path.split(".").slice(-1)[0];
168153
path = resolvePath(now, path);
169154
console.log(suffix, path, now);

0 commit comments

Comments
 (0)