ranuts 0.1.0-alpha.1 → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/assets/img/sort/bubble.gif +0 -0
- package/assets/img/sort/complexity.png +0 -0
- package/assets/img/sort/select.gif +0 -0
- package/assets/img/sort/sort.png +0 -0
- package/assets/img/tree/balanceTree.png +0 -0
- package/dist/index.js.map +0 -1
- package/dist/index.umd.cjs.map +0 -1
- package/dist/src/file/fileInfo.d.ts +0 -7
- package/dist/src/file/fs.d.ts +0 -2
- package/dist/src/file/readDir.d.ts +0 -6
- package/dist/src/file/readFile.d.ts +0 -8
- package/dist/src/file/watchFile.d.ts +0 -8
- package/dist/src/file/writeFile.d.ts +0 -8
- package/dist/src/mode/subscribe.d.ts +0 -16
- package/dist/src/ranlog/behavior.d.ts +0 -1
- package/dist/src/ranlog/env.d.ts +0 -5
- package/dist/src/ranlog/error.d.ts +0 -2
- package/dist/src/ranlog/index.d.ts +0 -26
- package/dist/src/ranlog/performance.d.ts +0 -21
- package/dist/src/ranlog/report.d.ts +0 -7
- package/dist/src/ranlog/request.d.ts +0 -18
- package/dist/src/ranlog/utils.d.ts +0 -19
- package/dist/src/utils/filterObj.d.ts +0 -8
- package/dist/src/utils/str2Xml.d.ts +0 -8
- package/dist/src/vnode/h.d.ts +0 -6
- package/dist/src/vnode/hooks.d.ts +0 -23
- package/dist/src/vnode/htmlDomApi.d.ts +0 -33
- package/dist/src/vnode/init.d.ts +0 -2
- package/dist/src/vnode/is.d.ts +0 -5
- package/dist/src/vnode/modules/attributes.d.ts +0 -8
- package/dist/src/vnode/modules/class.d.ts +0 -8
- package/dist/src/vnode/modules/index.d.ts +0 -7
- package/dist/src/vnode/modules/listeners.d.ts +0 -14
- package/dist/src/vnode/modules/props.d.ts +0 -8
- package/dist/src/vnode/modules/style.d.ts +0 -14
- package/dist/src/vnode/vnode.d.ts +0 -31
- package/src/astParser/Parser.ts +0 -654
- package/src/astParser/Tokenizer.ts +0 -447
- package/src/astParser/nodeTypes.ts +0 -194
- package/src/astParser/utils.ts +0 -27
- package/src/babel/parser.ts +0 -10
- package/src/colors/fmt.ts +0 -29
- package/src/colors/isColorSupported.ts +0 -13
- package/src/file/appendFile.ts +0 -29
- package/src/file/fileInfo.ts +0 -22
- package/src/file/fs.ts +0 -9
- package/src/file/readDir.ts +0 -18
- package/src/file/readFile.ts +0 -26
- package/src/file/watchFile.ts +0 -31
- package/src/file/writeFile.ts +0 -38
- package/src/mode/subscribe.ts +0 -89
- package/src/ran/commit.ts +0 -0
- package/src/ran/dom.ts +0 -0
- package/src/ran/hooks.ts +0 -0
- package/src/ran/reconcile.ts +0 -0
- package/src/ran/schedule.ts +0 -0
- package/src/ranlog/behavior.ts +0 -5
- package/src/ranlog/console.ts +0 -16
- package/src/ranlog/env.ts +0 -29
- package/src/ranlog/error.ts +0 -11
- package/src/ranlog/performance.ts +0 -65
- package/src/ranlog/report.ts +0 -33
- package/src/ranlog/request.ts +0 -60
- package/src/ranlog/utils.ts +0 -92
- package/src/ranpack/ast/Declaration.ts +0 -120
- package/src/ranpack/ast/Node.ts +0 -7
- package/src/ranpack/ast/Reference.ts +0 -32
- package/src/ranpack/ast/Scope.ts +0 -80
- package/src/ranpack/bundle.ts +0 -50
- package/src/ranpack/graph.ts +0 -143
- package/src/ranpack/module.ts +0 -431
- package/src/ranpack/moduleLoader.ts +0 -73
- package/src/ranpack/plugins.ts +0 -70
- package/src/ranpack/statement.ts +0 -66
- package/src/ranpack/utils/buildScope.ts +0 -77
- package/src/ranpack/utils/findReference.ts +0 -36
- package/src/ranpack/utils/isFunctionDeclaration.ts +0 -41
- package/src/ranpack/utils/makeLegalIdentifier.ts +0 -18
- package/src/ranpack/utils/object.ts +0 -8
- package/src/ranpack/utils/resolve.ts +0 -32
- package/src/ranpack/utils/walk.ts +0 -66
- package/src/ranpack/ws.ts +0 -269
- package/src/server/connectType.ts +0 -9
- package/src/server/encodeUrl.ts +0 -46
- package/src/server/escapeHtml.ts +0 -46
- package/src/server/get.ts +0 -36
- package/src/server/jitter.ts +0 -77
- package/src/server/mimeType.ts +0 -858
- package/src/server/paresUrl.ts +0 -40
- package/src/server/send.ts +0 -89
- package/src/server/server.ts +0 -67
- package/src/server/status.ts +0 -191
- package/src/server/traverse.ts +0 -54
- package/src/server/websocket.ts +0 -200
- package/src/sort/bubble.ts +0 -21
- package/src/sort/bucket.ts +0 -11
- package/src/sort/count.ts +0 -10
- package/src/sort/heap.ts +0 -10
- package/src/sort/insert.ts +0 -20
- package/src/sort/merge.ts +0 -35
- package/src/sort/quick.ts +0 -50
- package/src/sort/radix.ts +0 -11
- package/src/sort/randomArray.ts +0 -21
- package/src/sort/select.ts +0 -23
- package/src/sort/shell.ts +0 -22
- package/src/utils/compose.ts +0 -37
- package/src/utils/filterObj.ts +0 -21
- package/src/utils/mergeObj.ts +0 -12
- package/src/utils/startTask.ts +0 -15
- package/src/utils/str2Xml.ts +0 -26
- package/src/utils/taskEnd.ts +0 -14
- package/src/vnode/h.ts +0 -108
- package/src/vnode/hooks.ts +0 -25
- package/src/vnode/htmlDomApi.ts +0 -199
- package/src/vnode/init.ts +0 -429
- package/src/vnode/is.ts +0 -17
- package/src/vnode/modules/attributes.ts +0 -70
- package/src/vnode/modules/class.ts +0 -46
- package/src/vnode/modules/listeners.ts +0 -123
- package/src/vnode/modules/props.ts +0 -41
- package/src/vnode/modules/style.ts +0 -118
- package/src/vnode/vnode.ts +0 -65
package/src/ranpack/module.ts
DELETED
|
@@ -1,431 +0,0 @@
|
|
|
1
|
-
import MagicString from 'magic-string';
|
|
2
|
-
import { parse } from '../astParser';
|
|
3
|
-
import type { Bundle } from './bundle';
|
|
4
|
-
import { Statement } from './statement';
|
|
5
|
-
import type { ModuleLoader } from './moduleLoader';
|
|
6
|
-
import type {
|
|
7
|
-
Declaration
|
|
8
|
-
} from './ast/Declaration';
|
|
9
|
-
import {
|
|
10
|
-
SyntheticDefaultDeclaration,
|
|
11
|
-
SyntheticNamespaceDeclaration
|
|
12
|
-
} from './ast/Declaration';
|
|
13
|
-
import { keys } from './utils/object';
|
|
14
|
-
|
|
15
|
-
export interface ModuleOptions {
|
|
16
|
-
path: string;
|
|
17
|
-
bundle: Bundle;
|
|
18
|
-
loader: ModuleLoader;
|
|
19
|
-
code: string;
|
|
20
|
-
isEntry: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface ImportOrExportInfo {
|
|
24
|
-
source?: string;
|
|
25
|
-
localName: string;
|
|
26
|
-
name: string;
|
|
27
|
-
statement?: Statement;
|
|
28
|
-
isDeclaration?: boolean;
|
|
29
|
-
module?: Module;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface Specifier {
|
|
33
|
-
type: string;
|
|
34
|
-
local: {
|
|
35
|
-
name: string;
|
|
36
|
-
};
|
|
37
|
-
imported: {
|
|
38
|
-
name: string;
|
|
39
|
-
};
|
|
40
|
-
exported: {
|
|
41
|
-
name: string;
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type Imports = Record<string, ImportOrExportInfo>;
|
|
46
|
-
type Exports = Record<string, ImportOrExportInfo>;
|
|
47
|
-
|
|
48
|
-
export class Module {
|
|
49
|
-
isEntry: boolean = false;
|
|
50
|
-
id: string;
|
|
51
|
-
path: string;
|
|
52
|
-
bundle: Bundle;
|
|
53
|
-
moduleLoader: ModuleLoader;
|
|
54
|
-
code: string;
|
|
55
|
-
magicString: MagicString;
|
|
56
|
-
statements: Statement[];
|
|
57
|
-
imports: Imports;
|
|
58
|
-
exports: Exports;
|
|
59
|
-
reexports: Exports;
|
|
60
|
-
exportAllSources: string[] = [];
|
|
61
|
-
exportAllModules: Module[] = [];
|
|
62
|
-
declarations: Record<string, Declaration>;
|
|
63
|
-
dependencies: string[] = [];
|
|
64
|
-
dependencyModules: Module[] = [];
|
|
65
|
-
referencedModules: Module[] = [];
|
|
66
|
-
constructor({ path, bundle, code, loader, isEntry = false }: ModuleOptions) {
|
|
67
|
-
this.id = path;
|
|
68
|
-
this.bundle = bundle;
|
|
69
|
-
this.moduleLoader = loader;
|
|
70
|
-
this.isEntry = isEntry;
|
|
71
|
-
this.path = path;
|
|
72
|
-
this.code = code;
|
|
73
|
-
this.magicString = new MagicString(code);
|
|
74
|
-
this.imports = {};
|
|
75
|
-
this.exports = {};
|
|
76
|
-
this.reexports = {};
|
|
77
|
-
this.declarations = {};
|
|
78
|
-
try {
|
|
79
|
-
// 将整个模块解析成 ast 结构,用于分析模块
|
|
80
|
-
const ast = parse(code);
|
|
81
|
-
// 获取 ast 的 内容体
|
|
82
|
-
const nodes = ast.body;
|
|
83
|
-
// 以语句(Statement)的维度来拆分 Module,保存 statement 的集合,供之后分析
|
|
84
|
-
this.statements = nodes.map((node) => {
|
|
85
|
-
const magicString = this.magicString.snip(node.start, node.end);
|
|
86
|
-
return new Statement(node, magicString, this);
|
|
87
|
-
});
|
|
88
|
-
} catch (e) {
|
|
89
|
-
console.log('generate module fail: ', e);
|
|
90
|
-
throw e;
|
|
91
|
-
}
|
|
92
|
-
// 分析 AST 节点
|
|
93
|
-
this.analyzeAST();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
analyzeAST(): void {
|
|
97
|
-
// 以语句为最小单元来分析
|
|
98
|
-
this.statements.forEach((statement) => {
|
|
99
|
-
// 对 statement 进行分析
|
|
100
|
-
statement.analyze();
|
|
101
|
-
// 如果有 import 声明
|
|
102
|
-
if (statement.isImportDeclaration) {
|
|
103
|
-
this.addImports(statement);
|
|
104
|
-
} else if (statement.isExportDeclaration) {
|
|
105
|
-
// 如果有 export 声明
|
|
106
|
-
this.addExports(statement);
|
|
107
|
-
}
|
|
108
|
-
// 注册顶层声明
|
|
109
|
-
if (!statement.scope.parent) {
|
|
110
|
-
statement.scope.eachDeclaration((name, declaration) => {
|
|
111
|
-
this.declarations[name] = declaration;
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
// 注册 statement 的 next 属性,用于生成代码使用,next 即下一个 statement 的起始位置
|
|
116
|
-
const statements = this.statements;
|
|
117
|
-
let next = this.code.length;
|
|
118
|
-
for (let i = statements.length - 1; i >= 0; i--) {
|
|
119
|
-
statements[i].next = next;
|
|
120
|
-
next = statements[i].start;
|
|
121
|
-
}
|
|
122
|
-
// console.log('module:', this.path, this.declarations);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
addDependencies(source: string): void {
|
|
126
|
-
if (!this.dependencies.includes(source)) {
|
|
127
|
-
this.dependencies.push(source);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
addImports(statement: Statement): void {
|
|
132
|
-
const node = statement.node as any;
|
|
133
|
-
const source = node.source.value;
|
|
134
|
-
// import
|
|
135
|
-
node.specifiers.forEach((specifier: Specifier) => {
|
|
136
|
-
const isDefault = specifier.type === 'ImportDefaultSpecifier';
|
|
137
|
-
const isNamespace = specifier.type === 'ImportNamespaceSpecifier';
|
|
138
|
-
const localName = specifier.local.name;
|
|
139
|
-
const name = isDefault
|
|
140
|
-
? 'default'
|
|
141
|
-
: isNamespace
|
|
142
|
-
? '*'
|
|
143
|
-
: specifier.imported.name;
|
|
144
|
-
this.imports[localName] = { source, name, localName };
|
|
145
|
-
});
|
|
146
|
-
this.addDependencies(source);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
addExports(statement: Statement): void {
|
|
150
|
-
const node = statement.node as any;
|
|
151
|
-
const source = node.source && node.source.value;
|
|
152
|
-
if (node.type === 'ExportNamedDeclaration') {
|
|
153
|
-
// export { a, b } from 'mod'
|
|
154
|
-
if (node.specifiers.length) {
|
|
155
|
-
node.specifiers.forEach((specifier: Specifier) => {
|
|
156
|
-
const localName = specifier.local.name;
|
|
157
|
-
const exportedName = specifier.exported.name;
|
|
158
|
-
this.exports[exportedName] = {
|
|
159
|
-
localName,
|
|
160
|
-
name: exportedName
|
|
161
|
-
};
|
|
162
|
-
if (source) {
|
|
163
|
-
this.reexports[localName] = {
|
|
164
|
-
statement,
|
|
165
|
-
source,
|
|
166
|
-
localName,
|
|
167
|
-
name: localName,
|
|
168
|
-
module: undefined
|
|
169
|
-
};
|
|
170
|
-
this.imports[localName] = {
|
|
171
|
-
source,
|
|
172
|
-
localName,
|
|
173
|
-
name: localName
|
|
174
|
-
};
|
|
175
|
-
this.addDependencies(source);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
} else {
|
|
179
|
-
const declaration = node.declaration;
|
|
180
|
-
let name;
|
|
181
|
-
if (declaration.type === 'VariableDeclaration') {
|
|
182
|
-
// export const foo = 2;
|
|
183
|
-
name = declaration.declarations[0].id.name;
|
|
184
|
-
} else {
|
|
185
|
-
// export function foo() {}
|
|
186
|
-
name = declaration.id.name;
|
|
187
|
-
}
|
|
188
|
-
this.exports[name] = {
|
|
189
|
-
statement,
|
|
190
|
-
localName: name,
|
|
191
|
-
name
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
} else if (node.type === 'ExportDefaultDeclaration') {
|
|
195
|
-
const identifier =
|
|
196
|
-
// export default foo;
|
|
197
|
-
(node.declaration.id && node.declaration.id.name) ||
|
|
198
|
-
// export default function foo(){}
|
|
199
|
-
node.declaration.name;
|
|
200
|
-
|
|
201
|
-
this.exports['default'] = {
|
|
202
|
-
statement,
|
|
203
|
-
localName: identifier,
|
|
204
|
-
name: 'default'
|
|
205
|
-
};
|
|
206
|
-
this.declarations['default'] = new SyntheticDefaultDeclaration(
|
|
207
|
-
node,
|
|
208
|
-
identifier,
|
|
209
|
-
statement
|
|
210
|
-
);
|
|
211
|
-
} else if (node.type === 'ExportAllDeclaration') {
|
|
212
|
-
// export * from 'mod'
|
|
213
|
-
if (source) {
|
|
214
|
-
this.exportAllSources.push(source);
|
|
215
|
-
this.addDependencies(source);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
bind(): void {
|
|
221
|
-
this.bindImportSpecifiers();
|
|
222
|
-
this.bindReferences();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
bindImportSpecifiers(): void {
|
|
226
|
-
[...Object.values(this.imports), ...Object.values(this.reexports)].forEach(
|
|
227
|
-
(specifier) => {
|
|
228
|
-
specifier.module = this._getModuleBySource(specifier.source!);
|
|
229
|
-
}
|
|
230
|
-
);
|
|
231
|
-
this.exportAllModules = this.exportAllSources.map(
|
|
232
|
-
this._getModuleBySource.bind(this)
|
|
233
|
-
);
|
|
234
|
-
// 建立模块依赖图
|
|
235
|
-
this.dependencyModules = this.dependencies.map(
|
|
236
|
-
this._getModuleBySource.bind(this)
|
|
237
|
-
);
|
|
238
|
-
this.dependencyModules.forEach((module) => {
|
|
239
|
-
module.referencedModules.push(this);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
bindReferences(): void {
|
|
244
|
-
// 处理 default 导出
|
|
245
|
-
if (this.declarations['default'] && this.exports['default'].localName) {
|
|
246
|
-
const declaration = this.trace(this.exports['default'].localName);
|
|
247
|
-
if (declaration) {
|
|
248
|
-
(this.declarations['default'] as SyntheticDefaultDeclaration).bind(
|
|
249
|
-
declaration
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
this.statements.forEach((statement) => {
|
|
254
|
-
statement.references.forEach((reference) => {
|
|
255
|
-
// 根据引用寻找声明的位置
|
|
256
|
-
// 寻找顺序: 1. statement 2. 当前模块 3. 依赖模块
|
|
257
|
-
const declaration =
|
|
258
|
-
reference.scope.findDeclaration(reference.name) ||
|
|
259
|
-
this.trace(reference.name);
|
|
260
|
-
if (declaration) {
|
|
261
|
-
declaration.addReference(reference);
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
getOrCreateNamespace(): Declaration {
|
|
267
|
-
if (!this.declarations['*']) {
|
|
268
|
-
this.declarations['*'] = new SyntheticNamespaceDeclaration(this);
|
|
269
|
-
}
|
|
270
|
-
return this.declarations['*'];
|
|
271
|
-
}
|
|
272
|
-
trace(name: string): Declaration | null {
|
|
273
|
-
if (this.declarations[name]) {
|
|
274
|
-
// 从当前模块找
|
|
275
|
-
return this.declarations[name];
|
|
276
|
-
}
|
|
277
|
-
if (this.imports[name]) {
|
|
278
|
-
const importSpecifier = this.imports[name];
|
|
279
|
-
const importModule = importSpecifier.module!;
|
|
280
|
-
if (importSpecifier.name === '*') {
|
|
281
|
-
return importModule.getOrCreateNamespace();
|
|
282
|
-
}
|
|
283
|
-
// 从依赖模块找
|
|
284
|
-
const declaration = importModule.traceExport(importSpecifier.name);
|
|
285
|
-
if (declaration) {
|
|
286
|
-
return declaration;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return null;
|
|
290
|
-
}
|
|
291
|
-
// 从导出名追溯到 Declaration 声明节点
|
|
292
|
-
traceExport(name: string): Declaration | null {
|
|
293
|
-
// 1. reexport
|
|
294
|
-
// export { foo as bar } from './mod'
|
|
295
|
-
const reexportDeclaration = this.reexports[name];
|
|
296
|
-
if (reexportDeclaration) {
|
|
297
|
-
// 说明是从其它模块 reexport 出来的
|
|
298
|
-
// 经过 bindImportSpecifier 方法处理,现已绑定 module
|
|
299
|
-
const declaration = reexportDeclaration.module!.traceExport(
|
|
300
|
-
reexportDeclaration.localName
|
|
301
|
-
);
|
|
302
|
-
if (!declaration) {
|
|
303
|
-
throw new Error(
|
|
304
|
-
`${reexportDeclaration.localName} is not exported by module ${reexportDeclaration.module!.path
|
|
305
|
-
}(imported by ${this.path})`
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
return declaration;
|
|
309
|
-
}
|
|
310
|
-
// 2. export
|
|
311
|
-
// export { foo }
|
|
312
|
-
const exportDeclaration = this.exports[name];
|
|
313
|
-
if (exportDeclaration) {
|
|
314
|
-
const declaration = this.trace(name);
|
|
315
|
-
if (declaration) {
|
|
316
|
-
return declaration;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
// 3. export all
|
|
320
|
-
for (const exportAllModule of this.exportAllModules) {
|
|
321
|
-
const declaration = exportAllModule.trace(name);
|
|
322
|
-
if (declaration) {
|
|
323
|
-
return declaration;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return null;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
render(): MagicString {
|
|
330
|
-
const source = this.magicString.clone().trim();
|
|
331
|
-
this.statements.forEach((statement) => {
|
|
332
|
-
// 1. Tree Shaking
|
|
333
|
-
if (!statement.isIncluded) {
|
|
334
|
-
source.remove(statement.start, statement.next);
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
// 2. 重写引用位置的变量名 -> 对应的声明位置的变量名
|
|
338
|
-
statement.references.forEach((reference) => {
|
|
339
|
-
const { start, end } = reference;
|
|
340
|
-
const declaration = reference.declaration;
|
|
341
|
-
if (declaration) {
|
|
342
|
-
const name = declaration.render();
|
|
343
|
-
source.overwrite(start, end, name!);
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
// 3. 擦除/重写 export 相关的代码
|
|
347
|
-
if (statement.isExportDeclaration && !this.isEntry) {
|
|
348
|
-
// export { foo, bar }
|
|
349
|
-
if (
|
|
350
|
-
statement.node.type === 'ExportNamedDeclaration' &&
|
|
351
|
-
statement.node.specifiers.length
|
|
352
|
-
) {
|
|
353
|
-
source.remove(statement.start, statement.next);
|
|
354
|
-
}
|
|
355
|
-
// remove `export` from `export const foo = 42`
|
|
356
|
-
else if (
|
|
357
|
-
statement.node.type === 'ExportNamedDeclaration' &&
|
|
358
|
-
(statement.node.declaration!.type === 'VariableDeclaration' ||
|
|
359
|
-
statement.node.declaration!.type === 'FunctionDeclaration')
|
|
360
|
-
) {
|
|
361
|
-
source.remove(
|
|
362
|
-
statement.node.start,
|
|
363
|
-
statement.node.declaration!.start
|
|
364
|
-
);
|
|
365
|
-
}
|
|
366
|
-
// remove `export * from './mod'`
|
|
367
|
-
else if (statement.node.type === 'ExportAllDeclaration') {
|
|
368
|
-
source.remove(statement.start, statement.next);
|
|
369
|
-
}
|
|
370
|
-
// export default
|
|
371
|
-
else if (statement.node.type === 'ExportDefaultDeclaration') {
|
|
372
|
-
const defaultDeclaration = this.declarations['default'];
|
|
373
|
-
const defaultName = defaultDeclaration.render();
|
|
374
|
-
|
|
375
|
-
// export default function() {} -> function a() {}
|
|
376
|
-
if (statement.node.declaration.type === 'FunctionDeclaration') {
|
|
377
|
-
if (statement.node.declaration.id) {
|
|
378
|
-
// export default function foo() {} -> const a = function foo() {}
|
|
379
|
-
source.overwrite(
|
|
380
|
-
statement.node.start,
|
|
381
|
-
statement.node.declaration.start,
|
|
382
|
-
`const ${defaultName} = `
|
|
383
|
-
);
|
|
384
|
-
} else {
|
|
385
|
-
source.overwrite(
|
|
386
|
-
statement.node.start,
|
|
387
|
-
statement.node.declaration.start + 8,
|
|
388
|
-
`function ${defaultName}`
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
} else {
|
|
392
|
-
// export default () => {}
|
|
393
|
-
// export default Foo;
|
|
394
|
-
source.overwrite(
|
|
395
|
-
statement.node.start,
|
|
396
|
-
statement.node.declaration.start,
|
|
397
|
-
`const ${defaultName} = `
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
// 4. 单独处理 namespace 导出
|
|
404
|
-
if (this.declarations['*']) {
|
|
405
|
-
const namespaceDeclaration = this.declarations[
|
|
406
|
-
'*'
|
|
407
|
-
] as SyntheticNamespaceDeclaration;
|
|
408
|
-
if (namespaceDeclaration.needsNamespaceBlock) {
|
|
409
|
-
source.append(`\n\n${namespaceDeclaration.renderBlock()}\n`);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return source.trim();
|
|
413
|
-
}
|
|
414
|
-
// 拿到模块所有导出
|
|
415
|
-
getExports(): string[] {
|
|
416
|
-
return [
|
|
417
|
-
...keys(this.exports),
|
|
418
|
-
...keys(this.reexports),
|
|
419
|
-
...this.exportAllModules
|
|
420
|
-
.map((module) =>
|
|
421
|
-
module.getExports().filter((name: string) => name !== 'default')
|
|
422
|
-
)
|
|
423
|
-
.flat()
|
|
424
|
-
];
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
private _getModuleBySource(source: string) {
|
|
428
|
-
const id = this.moduleLoader.resolveId(source!, this.path) as string;
|
|
429
|
-
return this.bundle.getModuleById(id);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
import { Module } from './module'
|
|
3
|
-
import { defaultResolver } from './utils/resolve'
|
|
4
|
-
import type { Bundle } from './bundle'
|
|
5
|
-
/**
|
|
6
|
-
* @description: 1.调用 resolveId 方法解析模块路径 2.初始化模块实例即 Module 对象,解析模块 AST 3.递归初始化模块的所有依赖模块
|
|
7
|
-
* @return {*}
|
|
8
|
-
*/
|
|
9
|
-
export class ModuleLoader {
|
|
10
|
-
bundle: Bundle
|
|
11
|
-
resolveIdsMap: Map<string, string | false> = new Map()
|
|
12
|
-
constructor(bundle: Bundle) {
|
|
13
|
-
this.bundle = bundle
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* @description: 解析模块逻辑
|
|
17
|
-
* @param {string} id
|
|
18
|
-
* @param {string} importer
|
|
19
|
-
* @return {*}
|
|
20
|
-
*/
|
|
21
|
-
resolveId(id: string, importer: string = ''): string | false {
|
|
22
|
-
const cacheKey = id + importer
|
|
23
|
-
if (this.resolveIdsMap.has(cacheKey)) {
|
|
24
|
-
return this.resolveIdsMap.get(cacheKey)!
|
|
25
|
-
}
|
|
26
|
-
const resolved = defaultResolver(id, importer)
|
|
27
|
-
this.resolveIdsMap.set(cacheKey, resolved)
|
|
28
|
-
return resolved
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* @description: 加载模块内容并解析
|
|
32
|
-
* @return {*}
|
|
33
|
-
*/
|
|
34
|
-
async fetchModule(
|
|
35
|
-
id: string,
|
|
36
|
-
importer: string,
|
|
37
|
-
isEntry = false,
|
|
38
|
-
bundle: Bundle = this.bundle,
|
|
39
|
-
loader: ModuleLoader = this,
|
|
40
|
-
): Promise<Module | null> {
|
|
41
|
-
const path = this.resolveId(id, importer)
|
|
42
|
-
|
|
43
|
-
if (path === false) {
|
|
44
|
-
return null
|
|
45
|
-
}
|
|
46
|
-
// 查找缓存
|
|
47
|
-
const existModule = this.bundle.getModuleById(path)
|
|
48
|
-
if (existModule) {
|
|
49
|
-
return existModule
|
|
50
|
-
}
|
|
51
|
-
const code = await readFile(path, { encoding: 'utf-8' })
|
|
52
|
-
// 初始化模块,解析 AST
|
|
53
|
-
const module = new Module({
|
|
54
|
-
path,
|
|
55
|
-
code,
|
|
56
|
-
bundle,
|
|
57
|
-
loader,
|
|
58
|
-
isEntry,
|
|
59
|
-
})
|
|
60
|
-
this.bundle.addModule(module)
|
|
61
|
-
// 拉取所有的依赖模块
|
|
62
|
-
await this.fetchAllDependencies(module)
|
|
63
|
-
return module
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async fetchAllDependencies(module: Module): Promise<void> {
|
|
67
|
-
await Promise.all(
|
|
68
|
-
module.dependencies.map((dep) => {
|
|
69
|
-
return this.fetchModule(dep, module.path)
|
|
70
|
-
}),
|
|
71
|
-
)
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/ranpack/plugins.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
interface PluginItem {
|
|
2
|
-
name: string
|
|
3
|
-
exec: Function
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// pre-alias
|
|
7
|
-
// alias
|
|
8
|
-
// modulepreload-polyfill
|
|
9
|
-
// optimized-deps
|
|
10
|
-
// resolve
|
|
11
|
-
|
|
12
|
-
interface Plugin {
|
|
13
|
-
[x: string]: (() => void) | undefined | string
|
|
14
|
-
name: string
|
|
15
|
-
// input
|
|
16
|
-
options?: () => void
|
|
17
|
-
buildStart?: () => void
|
|
18
|
-
resolvedId?: () => void
|
|
19
|
-
load?: () => void
|
|
20
|
-
transform?: () => void
|
|
21
|
-
moduleParsed?: () => void
|
|
22
|
-
resolveDynamicImport?: () => void
|
|
23
|
-
watchChange?: () => void
|
|
24
|
-
closeWatcher?: () => void
|
|
25
|
-
buildEnd?: () => void
|
|
26
|
-
// output
|
|
27
|
-
renderStart?: () => void
|
|
28
|
-
generateBundle?: () => void
|
|
29
|
-
closeBundle?: () => void
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class PluginHooks {
|
|
33
|
-
[x: string]: Array<PluginItem>
|
|
34
|
-
constructor() {
|
|
35
|
-
// input
|
|
36
|
-
this.option = []
|
|
37
|
-
this.buildStart = []
|
|
38
|
-
this.resolvedId = []
|
|
39
|
-
this.load = []
|
|
40
|
-
this.transform = []
|
|
41
|
-
this.moduleParsed = []
|
|
42
|
-
this.resolveDynamicImport = []
|
|
43
|
-
this.watchChange = []
|
|
44
|
-
this.closeWatcher = []
|
|
45
|
-
this.buildEnd = []
|
|
46
|
-
// output
|
|
47
|
-
this.renderStart = []
|
|
48
|
-
this.generateBundle = []
|
|
49
|
-
this.closeBundle = []
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
class Plugins {
|
|
54
|
-
pluginHooks: PluginHooks
|
|
55
|
-
constructor(plugins = []) {
|
|
56
|
-
this.pluginHooks = new PluginHooks()
|
|
57
|
-
plugins.forEach((plugin) => this.register(plugin))
|
|
58
|
-
}
|
|
59
|
-
register = (plugin: Plugin): void => {
|
|
60
|
-
const { name, ...hooks } = plugin
|
|
61
|
-
Object.keys(hooks).forEach((hook) => {
|
|
62
|
-
const exec = plugin[hook]
|
|
63
|
-
if (exec instanceof Function) {
|
|
64
|
-
this.pluginHooks[hook]?.push({ name, exec })
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export default Plugins
|
package/src/ranpack/statement.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type MagicString from 'magic-string';
|
|
2
|
-
import type { ExportAllDeclaration, ExportDeclaration, ExportNamedDeclaration, FunctionDeclaration, Statement as StatementNode } from '../astParser';
|
|
3
|
-
import type { Reference } from './ast/Reference';
|
|
4
|
-
import { findReference } from './utils/findReference';
|
|
5
|
-
import { buildScope } from './utils/buildScope';
|
|
6
|
-
import {
|
|
7
|
-
isExportDeclaration,
|
|
8
|
-
isFunctionDeclaration,
|
|
9
|
-
isImportDeclaration
|
|
10
|
-
} from './utils/isFunctionDeclaration';
|
|
11
|
-
import { Scope } from './ast/Scope';
|
|
12
|
-
import type { Module } from './module';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @description: 生成用于分析的 Statement 语句
|
|
16
|
-
*/
|
|
17
|
-
export class Statement {
|
|
18
|
-
// acorn type problem
|
|
19
|
-
node: StatementNode;
|
|
20
|
-
magicString: MagicString;
|
|
21
|
-
module: Module;
|
|
22
|
-
scope: Scope;
|
|
23
|
-
start: number;
|
|
24
|
-
next: number;
|
|
25
|
-
isImportDeclaration: boolean;
|
|
26
|
-
isExportDeclaration: boolean;
|
|
27
|
-
isReexportDeclaration: boolean;
|
|
28
|
-
isFunctionDeclaration: boolean;
|
|
29
|
-
isIncluded: boolean = false;
|
|
30
|
-
defines: Set<string> = new Set();
|
|
31
|
-
modifies: Set<string> = new Set();
|
|
32
|
-
dependsOn: Set<string> = new Set();
|
|
33
|
-
references: Reference[] = [];
|
|
34
|
-
constructor(node: StatementNode, magicString: MagicString, module: Module) {
|
|
35
|
-
this.magicString = magicString;
|
|
36
|
-
this.node = node;
|
|
37
|
-
this.module = module;
|
|
38
|
-
this.scope = new Scope({
|
|
39
|
-
statement: this
|
|
40
|
-
});
|
|
41
|
-
this.start = node.start;
|
|
42
|
-
this.next = 0;
|
|
43
|
-
this.isImportDeclaration = isImportDeclaration(node);
|
|
44
|
-
this.isExportDeclaration = isExportDeclaration(node as ExportDeclaration);
|
|
45
|
-
this.isReexportDeclaration = this.isExportDeclaration && !!(node as (ExportAllDeclaration | ExportNamedDeclaration)).source;
|
|
46
|
-
this.isFunctionDeclaration = isFunctionDeclaration(node as FunctionDeclaration);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
analyze():void {
|
|
50
|
-
if (this.isImportDeclaration) return;
|
|
51
|
-
// 1、构建作用域链,记录 Declaration 节点表
|
|
52
|
-
buildScope(this);
|
|
53
|
-
// 2. 寻找引用依赖,记录 Reference 节点表
|
|
54
|
-
findReference(this);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
mark():void {
|
|
58
|
-
if (this.isIncluded) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this.isIncluded = true;
|
|
62
|
-
this.references.forEach(
|
|
63
|
-
(ref: Reference) => ref.declaration && ref.declaration.use()
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
}
|