-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpa.utils.js
95 lines (77 loc) · 2.3 KB
/
mpa.utils.js
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
// 配置
const glob = require('glob');
const path = require('path');
const fs = require('fs');
// console.log(CONFIG)
const browserPages = [];
const port = process.env.PORT || 8000;
// console.log('-------端口--------')
// console.log(port)
// console.log('-------环境--------')
// console.log(process.env.NODE_ENV)
function isProd() {
return process.env.NODE_ENV === 'production';
}
function shouldReadAsEntry(moduleName) {
// 是否小写字母开头 并且不以use开头
return (
moduleName.charAt(0).match(/^.*[a-z]+.*$/) &&
moduleName.indexOf('use') !== 0
);
}
exports.getEntry = function getEntry(globPath) {
const entries = [];
let indexInfo = {
hasIndex: false,
indexComponent: null,
};
glob.sync(globPath).forEach((entry) => {
// 切割路径 --> [ '.', '_project', 'module', 'foo.js' ]
// --> ['_project', 'module', 'foo.js' ]
// --> ['_project', 'module' ]
// --> ['module' ]
const sections = entry.split('/').splice(1);
// console.log(sections)
// 模块名称 --> 'foo'
const moduleName = path.basename(entry, path.extname(entry));
// console.log(moduleName)
// 跳过不符合入口规则的文件
if (!shouldReadAsEntry(moduleName)) {
return;
}
// 已获取模块名,sections移除最后一个
sections.pop();
// 已获取路径参数, 去掉section的工程名
sections.shift();
// 生成唯一id, 防止多个目录下路径重复
let prefix = '';
// 除了moduleName与当前文件名前缀是否一致, 且层级为1
// 其他情况将section串联,作为uuid的一部分
if (
sections.length > 1 ||
(sections.length === 1 && moduleName.indexOf(sections[0]) !== 0)
) {
prefix = `${sections.join('-')}-`;
}
const uuid = `${prefix}${moduleName}`;
// console.log(uuid)
browserPages.push(`http://localhost:${port}/${uuid}.html`);
// entries[uuid] = entry;
// console.log(uuid)
if (uuid == 'index') {
console.log('原目录存在首页');
indexInfo.hasIndex = true;
indexInfo.indexComponent = '../.' + entry;
}
entries.push({
path: `/${uuid}`,
component: '../.' + entry,
});
});
// console.log('-------页面--------');
// console.log(browserPages);
return {
entries,
indexInfo,
};
};