@ainc/fs 0.1.22 → 0.1.23
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/dist/cache.d.ts +16 -0
- package/dist/cache.js +24 -0
- package/dist/dict.d.ts +21 -0
- package/dist/dict.js +66 -0
- package/dist/downloadFile.js +19 -13
- package/dist/index.d.ts +37 -1
- package/dist/index.exports.json +34 -0
- package/dist/index.js +59 -3
- package/dist/isEsmModule.d.ts +18 -0
- package/dist/isEsmModule.js +63 -0
- package/dist/loadPackageDescription.d.ts +61 -0
- package/dist/loadPackageDescription.js +71 -0
- package/dist/loadTsConfig.d.ts +87 -0
- package/dist/loadTsConfig.js +219 -0
- package/dist/match.d.ts +30 -0
- package/dist/match.js +59 -0
- package/dist/resolveAlias.d.ts +25 -0
- package/dist/resolveAlias.js +82 -0
- package/dist/resolveDirect.d.ts +15 -0
- package/dist/resolveDirect.js +144 -0
- package/dist/resolveExports.d.ts +19 -0
- package/dist/resolveExports.js +54 -0
- package/dist/resolveImports.d.ts +21 -0
- package/dist/resolveImports.js +114 -0
- package/dist/resolveModuleDir.d.ts +12 -0
- package/dist/resolveModuleDir.js +67 -0
- package/dist/resolveModuleId.d.ts +6 -0
- package/dist/resolveModuleId.js +32 -0
- package/dist/resolvePath.d.ts +23 -0
- package/dist/resolvePath.js +86 -0
- package/dist/resolvePaths.d.ts +13 -0
- package/dist/resolvePaths.js +42 -0
- package/dist/split.d.ts +6 -0
- package/dist/split.js +24 -0
- package/dist/sys.d.ts +60 -0
- package/dist/sys.js +260 -0
- package/esm/cache.mjs +22 -0
- package/esm/dict.mjs +63 -0
- package/esm/downloadFile.mjs +19 -13
- package/esm/index.exports.json +34 -0
- package/esm/index.mjs +29 -1
- package/esm/isEsmModule.mjs +59 -0
- package/esm/loadPackageDescription.mjs +67 -0
- package/esm/loadTsConfig.mjs +215 -0
- package/esm/match.mjs +53 -0
- package/esm/resolveAlias.mjs +79 -0
- package/esm/resolveDirect.mjs +142 -0
- package/esm/resolveExports.mjs +51 -0
- package/esm/resolveImports.mjs +112 -0
- package/esm/resolveModuleDir.mjs +64 -0
- package/esm/resolveModuleId.mjs +30 -0
- package/esm/resolvePath.mjs +84 -0
- package/esm/resolvePaths.mjs +40 -0
- package/esm/split.mjs +22 -0
- package/esm/sys.mjs +258 -0
- package/package.json +3 -2
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2023-12-30 13:48:09
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.loadTsConfig = loadTsConfig;
|
|
10
|
+
exports.loadCompilerOptions = loadCompilerOptions;
|
|
11
|
+
exports.resolveTsConfig = resolveTsConfig;
|
|
12
|
+
/**
|
|
13
|
+
*****************************************
|
|
14
|
+
* 加载依赖
|
|
15
|
+
*****************************************
|
|
16
|
+
*/
|
|
17
|
+
const node_fs_1 = require("node:fs");
|
|
18
|
+
const node_path_1 = require("node:path");
|
|
19
|
+
const relative_1 = require("./relative");
|
|
20
|
+
const resolveFile_1 = require("./resolveFile");
|
|
21
|
+
const stat_1 = require("./stat");
|
|
22
|
+
const lookup_1 = require("./lookup");
|
|
23
|
+
const jsonc_1 = require("./jsonc");
|
|
24
|
+
const match_1 = require("./match");
|
|
25
|
+
const resolveModuleDir_1 = require("./resolveModuleDir");
|
|
26
|
+
/**
|
|
27
|
+
*****************************************
|
|
28
|
+
* 加载 TS 配置
|
|
29
|
+
*****************************************
|
|
30
|
+
*/
|
|
31
|
+
function loadTsConfig(from) {
|
|
32
|
+
const data = resolveTsConfig(from);
|
|
33
|
+
// 返回配置
|
|
34
|
+
if (data) {
|
|
35
|
+
return data.config;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
*****************************************
|
|
40
|
+
* 加载编译配置
|
|
41
|
+
*****************************************
|
|
42
|
+
*/
|
|
43
|
+
function loadCompilerOptions(from) {
|
|
44
|
+
const data = resolveTsConfig(from);
|
|
45
|
+
// 返回配置
|
|
46
|
+
if (data && data.config) {
|
|
47
|
+
return data.config.compilerOptions;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
*****************************************
|
|
52
|
+
* 缓存内容
|
|
53
|
+
*****************************************
|
|
54
|
+
*/
|
|
55
|
+
const cache = new Map();
|
|
56
|
+
/**
|
|
57
|
+
*****************************************
|
|
58
|
+
* 解析 TS 配置
|
|
59
|
+
*****************************************
|
|
60
|
+
*/
|
|
61
|
+
function resolveTsConfig(from) {
|
|
62
|
+
return (0, lookup_1.lookup)('tsconfig.json', {
|
|
63
|
+
from,
|
|
64
|
+
cache,
|
|
65
|
+
resolve(path) {
|
|
66
|
+
return (0, stat_1.isFile)(path) ? loadTsConfigFile(path) : undefined;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
*****************************************
|
|
72
|
+
* 解析 TS 配置文件
|
|
73
|
+
*****************************************
|
|
74
|
+
*/
|
|
75
|
+
function loadTsConfigFile(path) {
|
|
76
|
+
const config = parseTsConfig(path, new Set([path]), ['.json', node_path_1.sep + 'tsconfig.json']);
|
|
77
|
+
const dir = (0, node_path_1.dirname)(path);
|
|
78
|
+
// 移除继承属性
|
|
79
|
+
config.extends = undefined;
|
|
80
|
+
// 格式化 include 配置
|
|
81
|
+
if (config.include) {
|
|
82
|
+
config.include = config.include.map(path => (0, relative_1.relative)(dir, path));
|
|
83
|
+
}
|
|
84
|
+
// 格式化 exclude 配置
|
|
85
|
+
if (config.exclude) {
|
|
86
|
+
config.exclude = config.exclude.map(path => (0, relative_1.relative)(dir, path));
|
|
87
|
+
}
|
|
88
|
+
// 格式化 files 配置
|
|
89
|
+
if (config.files) {
|
|
90
|
+
config.files = config.files.map(path => (0, relative_1.relative)(dir, path));
|
|
91
|
+
}
|
|
92
|
+
// 返回结果
|
|
93
|
+
return { path, config };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
*****************************************
|
|
97
|
+
* 解析配置
|
|
98
|
+
*****************************************
|
|
99
|
+
*/
|
|
100
|
+
function parseTsConfig(path, tracker, extensions) {
|
|
101
|
+
const data = (0, jsonc_1.jsonc)((0, node_fs_1.readFileSync)(path, 'utf8'));
|
|
102
|
+
const dir = (0, node_path_1.dirname)(path);
|
|
103
|
+
const config = normalizeTsConfig(data, dir);
|
|
104
|
+
// 不存在继承,直接返回
|
|
105
|
+
if (!config.extends) {
|
|
106
|
+
return config;
|
|
107
|
+
}
|
|
108
|
+
// 获取继承列表
|
|
109
|
+
const extendsList = Array.isArray(config.extends) ? config.extends : [config.extends];
|
|
110
|
+
const configs = extendsList.map(path => parseExtendsTsConfig(path, dir, tracker, extensions));
|
|
111
|
+
// 追加当前配置
|
|
112
|
+
configs.push(config);
|
|
113
|
+
// 合并配置
|
|
114
|
+
return configs.reduce(mergeTsConfig);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
*****************************************
|
|
118
|
+
* 合并配置
|
|
119
|
+
*****************************************
|
|
120
|
+
*/
|
|
121
|
+
function mergeTsConfig(target, source) {
|
|
122
|
+
const { baseUrl, paths } = source.compilerOptions || {};
|
|
123
|
+
const config = { ...target, ...source };
|
|
124
|
+
// 合并配置项
|
|
125
|
+
config.compilerOptions = { ...target.compilerOptions, ...source.compilerOptions };
|
|
126
|
+
config.watchOptions = { ...target.watchOptions, ...source.watchOptions };
|
|
127
|
+
// 更新 paths 配置
|
|
128
|
+
if (baseUrl || paths) {
|
|
129
|
+
config.compilerOptions.baseUrl = baseUrl;
|
|
130
|
+
config.compilerOptions.paths = paths;
|
|
131
|
+
}
|
|
132
|
+
// 返回配置
|
|
133
|
+
return config;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
*****************************************
|
|
137
|
+
* 解析继承配置
|
|
138
|
+
*****************************************
|
|
139
|
+
*/
|
|
140
|
+
function parseExtendsTsConfig(path, from, tracker, extensions) {
|
|
141
|
+
const extendsFile = resolveExtendsFile(path, from, extensions);
|
|
142
|
+
// 未找到文件
|
|
143
|
+
if (!extendsFile) {
|
|
144
|
+
throw new Error(`file '${path}' not found.`);
|
|
145
|
+
}
|
|
146
|
+
// 循环引用
|
|
147
|
+
if (tracker.has(extendsFile)) {
|
|
148
|
+
throw new Error(`circularity detected while resolving configuration: ${extendsFile}`);
|
|
149
|
+
}
|
|
150
|
+
// 添加引用文件
|
|
151
|
+
tracker.add(extendsFile);
|
|
152
|
+
// 解析文件
|
|
153
|
+
return parseTsConfig(extendsFile, tracker, extensions);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
*****************************************
|
|
157
|
+
* 解析继承文件地址
|
|
158
|
+
*****************************************
|
|
159
|
+
*/
|
|
160
|
+
function resolveExtendsFile(path, from, extensions) {
|
|
161
|
+
// 解析相对路径
|
|
162
|
+
if ((0, relative_1.isRelative)(path)) {
|
|
163
|
+
return (0, resolveFile_1.resolveFile)((0, node_path_1.resolve)(from, path), extensions);
|
|
164
|
+
}
|
|
165
|
+
// 解析绝对路径
|
|
166
|
+
if ((0, node_path_1.isAbsolute)(path)) {
|
|
167
|
+
return (0, resolveFile_1.resolveFile)(path, extensions);
|
|
168
|
+
}
|
|
169
|
+
// 解析模块目录
|
|
170
|
+
const moduleDirs = (0, resolveModuleDir_1.resolveModuleDirs)(from);
|
|
171
|
+
// 模块目录
|
|
172
|
+
return (0, match_1.match)(moduleDirs, dir => (0, resolveFile_1.resolveFile)((0, node_path_1.resolve)(dir, path), extensions));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
*****************************************
|
|
176
|
+
* 格式化配置
|
|
177
|
+
*****************************************
|
|
178
|
+
*/
|
|
179
|
+
function normalizeTsConfig(config, dir) {
|
|
180
|
+
const compilerOptions = config.compilerOptions;
|
|
181
|
+
const watchOptions = config.watchOptions;
|
|
182
|
+
// 格式化 include 配置
|
|
183
|
+
if (config.include) {
|
|
184
|
+
config.include = config.include.map(path => (0, node_path_1.resolve)(dir, path));
|
|
185
|
+
}
|
|
186
|
+
// 格式化 exclude 配置
|
|
187
|
+
if (config.exclude) {
|
|
188
|
+
config.exclude = config.exclude.map(path => (0, node_path_1.resolve)(dir, path));
|
|
189
|
+
}
|
|
190
|
+
// 格式化 files 配置
|
|
191
|
+
if (config.files) {
|
|
192
|
+
config.files = config.files.map(path => (0, node_path_1.resolve)(dir, path));
|
|
193
|
+
}
|
|
194
|
+
// 格式化 compilerOptions 配置
|
|
195
|
+
if (compilerOptions) {
|
|
196
|
+
// 格式化 baseUrl 配置
|
|
197
|
+
if (compilerOptions.baseUrl) {
|
|
198
|
+
compilerOptions.baseUrl = (0, node_path_1.resolve)(dir, compilerOptions.baseUrl);
|
|
199
|
+
}
|
|
200
|
+
// 格式化 outDir 配置
|
|
201
|
+
if (compilerOptions.outDir) {
|
|
202
|
+
compilerOptions.outDir = (0, node_path_1.resolve)(dir, compilerOptions.outDir);
|
|
203
|
+
}
|
|
204
|
+
// 格式化 rootDir 配置
|
|
205
|
+
if (compilerOptions.rootDir) {
|
|
206
|
+
compilerOptions.rootDir = (0, node_path_1.resolve)(dir, compilerOptions.rootDir);
|
|
207
|
+
}
|
|
208
|
+
// 格式化 rootDirs 配置
|
|
209
|
+
if (compilerOptions.rootDirs) {
|
|
210
|
+
compilerOptions.rootDirs = compilerOptions.rootDirs.map(path => (0, node_path_1.resolve)(dir, path));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// 格式化监听路径
|
|
214
|
+
if (watchOptions && watchOptions.excludeDirectories) {
|
|
215
|
+
watchOptions.excludeDirectories = watchOptions.excludeDirectories.map(path => (0, node_path_1.resolve)(dir, path));
|
|
216
|
+
}
|
|
217
|
+
// 返回配置
|
|
218
|
+
return config;
|
|
219
|
+
}
|
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* 空函数
|
|
4
|
+
*****************************************
|
|
5
|
+
*/
|
|
6
|
+
export declare function noop(): void;
|
|
7
|
+
/**
|
|
8
|
+
*****************************************
|
|
9
|
+
* nil 函数
|
|
10
|
+
*****************************************
|
|
11
|
+
*/
|
|
12
|
+
export declare function nil(): null;
|
|
13
|
+
/**
|
|
14
|
+
*****************************************
|
|
15
|
+
* false
|
|
16
|
+
*****************************************
|
|
17
|
+
*/
|
|
18
|
+
export declare function falsy(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
*****************************************
|
|
21
|
+
* 一元函数
|
|
22
|
+
*****************************************
|
|
23
|
+
*/
|
|
24
|
+
export declare function unary<T>(value: T): T;
|
|
25
|
+
/**
|
|
26
|
+
*****************************************
|
|
27
|
+
* 执行匹配
|
|
28
|
+
*****************************************
|
|
29
|
+
*/
|
|
30
|
+
export declare function match<T, P>(list: T[], handler: (value: T) => P): P | undefined;
|
package/dist/match.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2026-03-18 15:12:42
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.noop = noop;
|
|
10
|
+
exports.nil = nil;
|
|
11
|
+
exports.falsy = falsy;
|
|
12
|
+
exports.unary = unary;
|
|
13
|
+
exports.match = match;
|
|
14
|
+
/**
|
|
15
|
+
*****************************************
|
|
16
|
+
* 空函数
|
|
17
|
+
*****************************************
|
|
18
|
+
*/
|
|
19
|
+
function noop() {
|
|
20
|
+
// do nothing
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
*****************************************
|
|
24
|
+
* nil 函数
|
|
25
|
+
*****************************************
|
|
26
|
+
*/
|
|
27
|
+
function nil() {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
*****************************************
|
|
32
|
+
* false
|
|
33
|
+
*****************************************
|
|
34
|
+
*/
|
|
35
|
+
function falsy() {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
*****************************************
|
|
40
|
+
* 一元函数
|
|
41
|
+
*****************************************
|
|
42
|
+
*/
|
|
43
|
+
function unary(value) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
*****************************************
|
|
48
|
+
* 执行匹配
|
|
49
|
+
*****************************************
|
|
50
|
+
*/
|
|
51
|
+
function match(list, handler) {
|
|
52
|
+
for (let i = 0; i < list.length; i++) {
|
|
53
|
+
const result = handler(list[i]);
|
|
54
|
+
// 查找到结果
|
|
55
|
+
if (result !== undefined) {
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* 加载依赖
|
|
4
|
+
*****************************************
|
|
5
|
+
*/
|
|
6
|
+
import { Dict } from './dict';
|
|
7
|
+
/**
|
|
8
|
+
*****************************************
|
|
9
|
+
* 处理函数
|
|
10
|
+
*****************************************
|
|
11
|
+
*/
|
|
12
|
+
export type Handler = (path: string) => string | null | undefined;
|
|
13
|
+
export type Match = (id: string, handler: Handler) => string | null | undefined;
|
|
14
|
+
/**
|
|
15
|
+
*****************************************
|
|
16
|
+
* 解析别名
|
|
17
|
+
*****************************************
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveAlias(alias: Record<string, string>): Dict<Match>;
|
|
20
|
+
/**
|
|
21
|
+
*****************************************
|
|
22
|
+
* 解析字典
|
|
23
|
+
*****************************************
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveDict<T, P>(data: Record<string, T>, handler: (key: string, value: T) => P): Dict<P>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2025-05-26 22:30:23
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.resolveAlias = resolveAlias;
|
|
10
|
+
exports.resolveDict = resolveDict;
|
|
11
|
+
/**
|
|
12
|
+
*****************************************
|
|
13
|
+
* 加载依赖
|
|
14
|
+
*****************************************
|
|
15
|
+
*/
|
|
16
|
+
const dict_1 = require("./dict");
|
|
17
|
+
const cache_1 = require("./cache");
|
|
18
|
+
const resolveDirect_1 = require("./resolveDirect");
|
|
19
|
+
/**
|
|
20
|
+
*****************************************
|
|
21
|
+
* 缓存
|
|
22
|
+
*****************************************
|
|
23
|
+
*/
|
|
24
|
+
const cached = (0, cache_1.cache)(new WeakMap());
|
|
25
|
+
/**
|
|
26
|
+
*****************************************
|
|
27
|
+
* 解析别名
|
|
28
|
+
*****************************************
|
|
29
|
+
*/
|
|
30
|
+
function resolveAlias(alias) {
|
|
31
|
+
return cached.get(alias, resolveTarget);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
*****************************************
|
|
35
|
+
* 解析别名
|
|
36
|
+
*****************************************
|
|
37
|
+
*/
|
|
38
|
+
function resolveTarget(alias) {
|
|
39
|
+
return resolveDict(alias, resolvePath);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
*****************************************
|
|
43
|
+
* 解析匹配路径
|
|
44
|
+
*****************************************
|
|
45
|
+
*/
|
|
46
|
+
function resolvePath(key, value) {
|
|
47
|
+
const { test, resolve } = (0, resolveDirect_1.resolveDirect)(key);
|
|
48
|
+
const rule = resolve(value);
|
|
49
|
+
// 返回匹配函数
|
|
50
|
+
return function match(id, handler) {
|
|
51
|
+
if (test(id)) {
|
|
52
|
+
return handler(rule(id));
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
*****************************************
|
|
58
|
+
* 解析字典
|
|
59
|
+
*****************************************
|
|
60
|
+
*/
|
|
61
|
+
function resolveDict(data, handler) {
|
|
62
|
+
const result = new dict_1.Dict();
|
|
63
|
+
// 生成元素
|
|
64
|
+
Object.keys(data)
|
|
65
|
+
.forEach(key => result.set(key, handler(key, data[key])));
|
|
66
|
+
// 排序列表
|
|
67
|
+
result.sort((a, b) => {
|
|
68
|
+
const idxA = a.indexOf('*');
|
|
69
|
+
const idxB = b.indexOf('*');
|
|
70
|
+
const baseA = idxA === -1 ? a.length : idxA;
|
|
71
|
+
const baseB = idxB === -1 ? b.length : idxB;
|
|
72
|
+
// 比较基准长度
|
|
73
|
+
if (baseA !== baseB) {
|
|
74
|
+
return baseB - baseA;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return idxA === -1 ? -1 : idxB === -1 ? 1 : b.length - a.length;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
// 返回结果
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* 解析指令
|
|
4
|
+
*****************************************
|
|
5
|
+
*/
|
|
6
|
+
export interface Direct {
|
|
7
|
+
test(id: string): boolean;
|
|
8
|
+
resolve(path: string): (id: string) => string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
*****************************************
|
|
12
|
+
* 解析指令
|
|
13
|
+
*****************************************
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveDirect(direct: string): Direct;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2026-03-18 13:32:09
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.resolveDirect = resolveDirect;
|
|
10
|
+
/**
|
|
11
|
+
*****************************************
|
|
12
|
+
* 加载依赖
|
|
13
|
+
*****************************************
|
|
14
|
+
*/
|
|
15
|
+
const node_path_1 = require("node:path");
|
|
16
|
+
const match_1 = require("./match");
|
|
17
|
+
const split_1 = require("./split");
|
|
18
|
+
/**
|
|
19
|
+
*****************************************
|
|
20
|
+
* 格式化路径
|
|
21
|
+
*****************************************
|
|
22
|
+
*/
|
|
23
|
+
const sepReg = /\//g;
|
|
24
|
+
const normalizePath = (path) => path.replace(sepReg, node_path_1.sep);
|
|
25
|
+
/**
|
|
26
|
+
*****************************************
|
|
27
|
+
* 格式化路径
|
|
28
|
+
*****************************************
|
|
29
|
+
*/
|
|
30
|
+
function normalizeFn(path) {
|
|
31
|
+
return node_path_1.sep === '\\' && (0, node_path_1.isAbsolute)(path) ? normalizePath : match_1.unary;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
*****************************************
|
|
35
|
+
* 解析指令
|
|
36
|
+
*****************************************
|
|
37
|
+
*/
|
|
38
|
+
function resolveDirect(direct) {
|
|
39
|
+
const idx = direct.indexOf('*');
|
|
40
|
+
if (idx === -1) {
|
|
41
|
+
return {
|
|
42
|
+
test: id => id === direct,
|
|
43
|
+
resolve: path => () => path,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// 分割路径
|
|
47
|
+
const [prefix, suffix] = [direct.slice(0, idx), direct.slice(idx + 1)];
|
|
48
|
+
// 生成匹配规则
|
|
49
|
+
if (prefix) {
|
|
50
|
+
return suffix ? resolvePrefixSuffix(prefix, suffix) : resolvePrefix(prefix);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return suffix ? resolveSuffix(suffix) : resolveWildcard();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
*****************************************
|
|
58
|
+
* 解析前/后缀
|
|
59
|
+
*****************************************
|
|
60
|
+
*/
|
|
61
|
+
function resolvePrefixSuffix(prefix, suffix) {
|
|
62
|
+
const size = prefix.length + suffix.length;
|
|
63
|
+
return {
|
|
64
|
+
test(id) {
|
|
65
|
+
return id.length >= size && id.startsWith(prefix) && id.endsWith(suffix);
|
|
66
|
+
},
|
|
67
|
+
resolve(path) {
|
|
68
|
+
const [start, end] = (0, split_1.split)(path, '*');
|
|
69
|
+
const normalize = normalizeFn(start);
|
|
70
|
+
if (end === undefined) {
|
|
71
|
+
return () => start;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return (id) => start + normalize(id.slice(prefix.length, -suffix.length)) + end;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
*****************************************
|
|
81
|
+
* 解析前缀
|
|
82
|
+
*****************************************
|
|
83
|
+
*/
|
|
84
|
+
function resolvePrefix(prefix) {
|
|
85
|
+
return {
|
|
86
|
+
test(id) {
|
|
87
|
+
return id.startsWith(prefix);
|
|
88
|
+
},
|
|
89
|
+
resolve(path) {
|
|
90
|
+
const [start, end] = (0, split_1.split)(path, '*');
|
|
91
|
+
const normalize = normalizeFn(start);
|
|
92
|
+
if (end === undefined) {
|
|
93
|
+
return () => start;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return (id) => start + normalize(id.slice(prefix.length)) + end;
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
*****************************************
|
|
103
|
+
* 解析后缀
|
|
104
|
+
*****************************************
|
|
105
|
+
*/
|
|
106
|
+
function resolveSuffix(suffix) {
|
|
107
|
+
return {
|
|
108
|
+
test(id) {
|
|
109
|
+
return id.endsWith(suffix);
|
|
110
|
+
},
|
|
111
|
+
resolve(val) {
|
|
112
|
+
const [start, end] = (0, split_1.split)(val, '*');
|
|
113
|
+
const normalize = normalizeFn(start);
|
|
114
|
+
if (end === undefined) {
|
|
115
|
+
return () => start;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return (id) => start + normalize(id.slice(0, -suffix.length)) + end;
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
*****************************************
|
|
125
|
+
* 解析通配符
|
|
126
|
+
*****************************************
|
|
127
|
+
*/
|
|
128
|
+
function resolveWildcard() {
|
|
129
|
+
return {
|
|
130
|
+
test() {
|
|
131
|
+
return true;
|
|
132
|
+
},
|
|
133
|
+
resolve(val) {
|
|
134
|
+
const [start, end] = (0, split_1.split)(val, '*');
|
|
135
|
+
const normalize = normalizeFn(start);
|
|
136
|
+
if (end === undefined) {
|
|
137
|
+
return () => start;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
return (id) => start + normalize(id) + end;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* 加载依赖
|
|
4
|
+
*****************************************
|
|
5
|
+
*/
|
|
6
|
+
import { TargetDescription } from './loadPackageDescription';
|
|
7
|
+
import { Handler } from './resolveAlias';
|
|
8
|
+
/**
|
|
9
|
+
*****************************************
|
|
10
|
+
* 解析导出描述
|
|
11
|
+
*****************************************
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveTargetDescription(exports: (null | string | TargetDescription)[] | TargetDescription): TargetDescription;
|
|
14
|
+
/**
|
|
15
|
+
*****************************************
|
|
16
|
+
* 解析导出配置
|
|
17
|
+
*****************************************
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveExports(id: string, conditions: string[], exports: TargetDescription[string], handler?: Handler): string | null | undefined;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2025-05-27 21:02:36
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.resolveTargetDescription = resolveTargetDescription;
|
|
10
|
+
exports.resolveExports = resolveExports;
|
|
11
|
+
const resolveImports_1 = require("./resolveImports");
|
|
12
|
+
/**
|
|
13
|
+
*****************************************
|
|
14
|
+
* 缓存
|
|
15
|
+
*****************************************
|
|
16
|
+
*/
|
|
17
|
+
const cache = new WeakMap();
|
|
18
|
+
/**
|
|
19
|
+
*****************************************
|
|
20
|
+
* 解析导出描述
|
|
21
|
+
*****************************************
|
|
22
|
+
*/
|
|
23
|
+
function resolveTargetDescription(exports) {
|
|
24
|
+
const cached = cache.get(exports);
|
|
25
|
+
if (cached) {
|
|
26
|
+
return cached;
|
|
27
|
+
}
|
|
28
|
+
// 生成目标配置
|
|
29
|
+
const target = Array.isArray(exports) || !Object.keys(exports).some(key => key.charAt(0) === '.')
|
|
30
|
+
? { '.': exports }
|
|
31
|
+
: exports;
|
|
32
|
+
// 添加缓存
|
|
33
|
+
cache.set(exports, target);
|
|
34
|
+
// 返回目标
|
|
35
|
+
return target;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
*****************************************
|
|
39
|
+
* 解析导出配置
|
|
40
|
+
*****************************************
|
|
41
|
+
*/
|
|
42
|
+
function resolveExports(id, conditions, exports, handler) {
|
|
43
|
+
if (!exports) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// 处理导出路径
|
|
47
|
+
if (typeof exports === 'string') {
|
|
48
|
+
return id === '.' ? exports : undefined;
|
|
49
|
+
}
|
|
50
|
+
// 解析导出描述
|
|
51
|
+
const target = resolveTargetDescription(exports);
|
|
52
|
+
// 获取匹配函数
|
|
53
|
+
return (0, resolveImports_1.resolveImports)(id, conditions, target, handler);
|
|
54
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* 加载依赖
|
|
4
|
+
*****************************************
|
|
5
|
+
*/
|
|
6
|
+
import { TargetDescription } from './loadPackageDescription';
|
|
7
|
+
import { Handler } from './resolveAlias';
|
|
8
|
+
/**
|
|
9
|
+
*****************************************
|
|
10
|
+
* 映射函数
|
|
11
|
+
*****************************************
|
|
12
|
+
*/
|
|
13
|
+
export interface Direct {
|
|
14
|
+
[key: string]: Handler | Direct | (Handler | Direct)[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
*****************************************
|
|
18
|
+
* 解析目标
|
|
19
|
+
*****************************************
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveImports(id: string, conditions: string[], target: TargetDescription, handler?: Handler): string | null | undefined;
|