-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.ts
130 lines (103 loc) · 3.67 KB
/
helper.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
import * as path from 'path';
import { normalizePath } from 't-comm';
import { getRelativePath, getUniCliCache } from '../../helper/index';
import type { IDispatchScriptOptions, IChunks, IModule } from './types';
const getPageSet = () => getUniCliCache('getPageSet')();
const INPUT_DIR = process.env.UNI_INPUT_DIR || '';
const SUB_PACKAGE_ROOTS = Object.keys((process as any).UNI_SUBPACKAGES || {});
const subPackageRootsWithSlash = Object.keys((process as any).UNI_SUBPACKAGES || {}).map(root => `${root}/`);
let mainPackagePages: Array<string>;
export const ROOT_NAME = 'MAIN';
export const VENDER_PATH = 'common/vendor';
/**
* 获取主包页面,比如`src/project/user/views/index/index-home.vue`
*/
export function getMainPackagePages() {
if (mainPackagePages) {
return mainPackagePages;
}
const subPackages = SUB_PACKAGE_ROOTS
.map(item => path.resolve(INPUT_DIR, item));
const pages = (Array.from(getPageSet()) as Array<string>)
.map(item => path.resolve(INPUT_DIR, item));
mainPackagePages = pages
.filter(page => !subPackages.find(subPackage => page.startsWith(subPackage)))
.map(item => `${getRelativePath(item)}.vue`);
return mainPackagePages;
}
/**
* 获取main.js路径,比如`src/project/user/main.js`
*/
export function getMainJSPath() {
return getRelativePath(path.resolve(INPUT_DIR, 'main.js'));
}
/**
* 获取页面列表
*/
export function getPagesList() {
const pages = Array.from(getPageSet()) as Array<string>;
return pages.map(item => getRelativePath(path.resolve(INPUT_DIR, item)));
}
/**
* path是否为页面路径
* @example
* src/project/user/views/match-data/rank.vue // true
* src/local-logic/tip-match/homepage/homepage.js // false
*/
export function isPage(path: string) {
const pages = getPagesList();
const isPage = pages.find(item => path.startsWith(item));
return !!isPage;
}
/**
* 获取页面对应的分包
*/
export function getPageSubPackages(page: string) {
const subPackageMap = SUB_PACKAGE_ROOTS
.reduce((acc: Record<string, string>, item) => {
acc[getRelativePath(path.resolve(INPUT_DIR, item))] = item;
return acc;
}, {});
const subPackage = Object.keys(subPackageMap).find(item => page.startsWith(`${item}/`));
if (subPackage) {
return subPackageMap[subPackage];
}
}
// 基础检测,vue和css类型不处理,只处理js类型
export function baseTest(module: IModule, options: IDispatchScriptOptions) {
const mainPath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR || '', 'main.'));
if (module.type === 'css/mini-extract') {
return false;
}
if (module.resource) {
const resource = normalizePath(module.resource);
if (
resource.indexOf('.vue') !== -1
|| resource.indexOf('.nvue') !== -1
|| resource.indexOf(mainPath) === 0 // main.js
) {
return false;
}
if (options?.whiteList?.length) {
const isExist = options.whiteList.find(url => resource.includes(url));
return !isExist;
}
}
return true;
}
export const findSubPackages = function (chunks: IChunks) {
return chunks.reduce((pkgs, item) => {
const name = normalizePath(item.name);
// 用chunk的名称匹配分包
const pkgRoot = subPackageRootsWithSlash.find(root => name.indexOf(root) === 0);
pkgRoot && pkgs.add(pkgRoot);
return pkgs;
}, new Set());
};
export const hasMainPackage = function (chunks: IChunks) {
// 有一个chunk的名称,在所有分包中都匹配不到
return chunks.find(item => !subPackageRootsWithSlash.find(root => item.name.indexOf(root) === 0));
};
export const findNameChunk = function (chunks: IChunks, name: string) {
return chunks.find(chunk => chunk.name === name);
};