-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
168 lines (144 loc) · 4.62 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import path from 'path';
import through from 'through2';
import replace from 'replacestream';
import slash from 'slash';
type AliasMapType = Record<string, string>;
type Options = {
cwd?: string;
paths?: AliasMapType;
};
type AliasListType = Array<{
aliasKey: string;
aliasValue: string;
}>;
const prefixPatternMap = {
js: 'import\\s*[^\'"]*\\(?|from|require\\s*\\(',
// poster: wxml
xml: 'src=|url=|poster=|href=',
css: '@import\\s*|url\\s*\\(',
};
/* 全匹配的正则规则 */
const exactMatchPattern = /\$$/;
type PrefixPatternMap = typeof prefixPatternMap;
/**
* 编码别名替换项
* eg. '/' -> '\\/'
*/
function encodeAliasString(alias: string): string {
return alias.replace('/', '\\/');
}
/* 获取匹配前缀正则字符串 */
function getPrefixPatternString(prefixPatten: string): string {
return `(?:(${prefixPatten})\\s*['"]?\\s*)`;
}
/* 获取匹配后缀正则字符串 */
function getSuffixPatternString(exactMatch = false): string {
const suffixPattern = '[\'"]|\\s*\\)';
// 全匹配时,后面没有内容,拿掉斜线
return exactMatch ? suffixPattern : `\\/|${suffixPattern}`;
}
/* 获取匹配主体和后缀的正则字符串 */
function getRemainPatternString(aliasKey: string, exactMatch?: boolean): string {
const _aliasKey = exactMatch ? aliasKey.replace(exactMatchPattern, '') : aliasKey;
return `(?<alias>${_aliasKey})(${getSuffixPatternString(exactMatch)})`;
}
/* 获取相对路径 */
function relative(from: string, to: string) {
const relativePath = slash(path.relative(from, to));
if (!relativePath) {
return '.';
}
return !/^\./.test(relativePath) ? `./${relativePath}` : relativePath;
}
// 100000 rows * 100 columns -> 248ms
function replaceAll(file: any, dirname: string, aliasList: AliasListType) {
const ext = path.extname(file.relative);
const isStream = file.isStream();
/* 根据后缀名获得前缀正则字符串,降低复杂度 */
let prefixPatternString: string;
switch (ext) {
// js
case '.js':
case '.ts':
case '.wxs':
prefixPatternString = getPrefixPatternString(prefixPatternMap.js);
break;
// css
case '.css':
case '.less':
case '.scss':
case '.sass':
case '.styl':
case '.stylus':
case '.wxss':
prefixPatternString = getPrefixPatternString(prefixPatternMap.css);
break;
// xml
case '.html':
case '.wxml':
prefixPatternString = getPrefixPatternString(prefixPatternMap.xml);
break;
case '.jsx':
case '.tsx':
default:
prefixPatternString = getPrefixPatternString(
Object.keys(prefixPatternMap)
.map((k) => prefixPatternMap[k as keyof PrefixPatternMap])
.join('|')
);
break;
}
aliasList.forEach(({ aliasKey, aliasValue }) => {
const isExactMatch = exactMatchPattern.test(aliasKey);
const remainPatternString = getRemainPatternString(aliasKey, isExactMatch);
const sentenceReg = new RegExp(`${prefixPatternString}${remainPatternString}`, 'gm');
const replacer = (match: string, ...args: any[]) => {
const group: Record<string, string> = args.pop();
const { alias } = group;
/* 如果替换路径是相对路径不使用 relative 路径替换,而是直接替换 */
const replaceStr = path.isAbsolute(aliasValue) ? relative(dirname, aliasValue) : aliasValue;
return match.replace(alias, replaceStr);
};
/* 先确定文件中匹配的语句,再替换其中所有匹配的 alias */
if (isStream) {
file.contents = file.contents.pipe(replace(sentenceReg, replacer));
} else {
file.contents = Buffer.from(String(file.contents).replace(sentenceReg, replacer));
}
});
return file;
}
/**
*
* @param options
*/
function alias(options: Options = {}) {
const _options: Required<Options> = {
cwd: process.cwd(),
paths: {},
...options,
};
const { paths } = _options;
const isEmptyAlias = !Object.keys(paths).length;
/* 初始化 aliasList,全局使用,避免重复计算 */
const aliasList: AliasListType = Object.keys(paths)
/* 全匹配的需要优先替换,放在前面 */
.sort((a) => (exactMatchPattern.test(a) ? -1 : 1))
.map((aliasKey) => {
/* 替换斜线,否则生成 pattern 时会出错 */
const encodeKey = encodeAliasString(aliasKey);
return {
aliasKey: encodeKey,
aliasValue: paths[aliasKey],
};
});
return through.obj(function (file, _, cb) {
const dirname = path.dirname(file.path);
if (file.isNull() || isEmptyAlias) {
return cb(null, file);
}
file = replaceAll(file, dirname, aliasList);
cb(null, file);
});
}
export = alias;