forked from jd-opensource/nutui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformFinalCode.ts
60 lines (60 loc) · 1.95 KB
/
transformFinalCode.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
// Components that need to be converted
export const DEFAULT_Components = new Set(['scroll-view']);
//whether to include the path to the current file
export const judgePath = (paths: string[], targetPath: string) => {
for (let i = 0; i < paths.length; i++) {
let reg = new RegExp(paths[i]);
if (reg.test(targetPath)) {
return true;
}
}
return false;
};
import type { Plugin } from 'vite';
export interface transformOptions {
exclude?: string[];
components?: string[];
envCondition?: string;
include?: string[];
}
export function transformFinalCode(options: transformOptions = {}): Plugin {
let _options: transformOptions = {
envCondition: 'process.env.TARO_ENV',
components: [],
include: [],
exclude: []
};
_options = Object.assign(_options, options);
return {
name: 'transformFinalCode',
enforce: 'post',
async config(config) {
if (!_options.envCondition) {
throw new Error('Environment variable is missing, check the envCondition field');
}
let _define = {};
_define[_options.envCondition] = _options.envCondition;
config.define = Object.assign(config.define, _define);
return config;
},
transform(code: string, id: any) {
let _code = code;
let _components = DEFAULT_Components;
if (_options.components && _options.components.length > 0) {
_components = new Set(_options.components);
}
if (_options.exclude && _options.exclude.length !== 0 && judgePath(_options.exclude, id)) {
return _code;
}
if (_options.include && _options.include.length !== 0 && !judgePath(_options.include, id)) {
return _code;
}
_components.forEach((tagName) => {
let Reg = new RegExp(`"${tagName}"`, 'ig');
const r = `function(){if(${_options.envCondition} === 'h5'){return 'taro-${tagName}'}else{return '${tagName}'}}()`;
_code = _code.replace(Reg, r);
});
return _code;
}
};
}