forked from easy-team/easywebpack
/
utils.js
161 lines (137 loc) · 4.29 KB
/
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
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
'use strict';
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const utils = {};
utils.isFunction = value => typeof value === 'function';
utils.isObject = value => typeof value === 'object';
utils.isString = value => typeof value === 'string';
utils.isBoolean = value => typeof value === 'boolean';
utils.normalizePath = (filepath, baseDir) => path.isAbsolute(filepath) ? filepath : path.join(baseDir, filepath);
utils.joinPath = function() {
return [].slice.call(arguments, 0).map((arg, index) => {
let tempArg = arg.replace(/\/$/, '');
if (index > 0) {
tempArg = arg.replace(/^\//, '');
}
return tempArg;
}).join('/');
};
utils.createEntry = (config, type) => {
const configEntry = config.entry;
if (configEntry && configEntry.include) {
const entryDirs = Array.isArray(configEntry.include) ? configEntry.include : [configEntry.include];
const normalizeEntryDirs = entryDirs.map(entryDir => utils.normalizePath(entryDir, config.baseDir));
let entryLoader = configEntry.loader && configEntry.loader[type];
if(entryLoader){
entryLoader = utils.normalizePath(entryLoader, config.baseDir);
}
return utils.getEntry(normalizeEntryDirs, configEntry.exclude, configEntry.extMatch, entryLoader);
}
return {};
};
utils.getEntry = (dirs, excludeRegex, extMatch = '.js', entryLoader) => {
const entries = {};
let entryDir = '';
const walk = (dir, exclude) => {
const dirList = fs.readdirSync(dir);
dirList.forEach(item => {
const filePath = path.join(dir, item);
if (fs.statSync(filePath).isDirectory()) {
walk(filePath, exclude);
} else {
if (!utils.isMatch(exclude, filePath)) {
if (filePath.endsWith(extMatch)) {
const fileName = path.relative(entryDir, filePath).replace(/\\/g, '/').replace(extMatch, '')
if (entryLoader) {
entries[fileName] = ['babel-loader', entryLoader, filePath].join('!');
} else {
entries[fileName] = filePath;
}
}
}
}
});
};
dirs = Array.isArray(dirs) ? dirs : [dirs];
dirs.forEach(dir => {
if (fs.existsSync(dir)) {
entryDir = dir;
walk(dir, excludeRegex);
}
});
return entries;
};
utils.isMatch = (regexArray, strMatch) => {
if (!regexArray || !regexArray.length) {
return false;
}
return regexArray.some(item => new RegExp(item, '').test(strMatch));
};
utils.assetsPath = (prefix, filepath) => path.posix.join(prefix, filepath);
utils.loadNodeModules = (isCache) => {
let nodeModules = {};
const cacheFile = path.join(__dirname, '../temp/cache.json');
if(isCache && fs.existsSync(cacheFile)){
return require(cacheFile);
}
fs.readdirSync('node_modules').filter(x => ['.bin'].indexOf(x) === -1).forEach(mod => {
nodeModules[mod] = `commonjs2 ${mod}`;
});
if(isCache){
utils.writeFile(cacheFile, nodeModules);
}
return nodeModules;
};
utils.getIp = position => {
const os = require('os');
const interfaces = os.networkInterfaces();
const ips = [];
if (interfaces.en0) {
for (let i = 0; i < interfaces.en0.length; i++) {
if (interfaces.en0[i].family === 'IPv4') {
ips.push(interfaces.en0[i].address);
}
}
}
if (interfaces.en1) {
for (let i = 0; i < interfaces.en1.length; i++) {
if (interfaces.en1[i].family === 'IPv4') {
ips.push(interfaces.en1[i].address);
}
}
}
if (position > 0 && position <= ips.length) {
return ips[position - 1];
} else if (ips.length) {
return ips[0];
}
return '127.0.0.1';
};
utils.getHost = port => {
const ip = utils.getIp();
return `http://${ip}:${port}`;
};
utils.writeFile = (filepath, content) => {
try {
mkdirp.sync(path.dirname(filepath));
fs.writeFileSync(filepath, typeof content === 'string' ? content : JSON.stringify(content), 'utf8');
} catch (e) {
console.error(`writeFile ${filepath} err`, e);
}
};
utils.readFile = filepath => {
try {
if (fs.existsSync(filepath)) {
const content = fs.readFileSync(filepath, 'utf8');
return JSON.parse(content);
}
} catch (e) {
/* istanbul ignore next */
}
return null;
};
utils.saveBuildConfig = (filepath, buildConfig) => {
utils.writeFile(filepath, buildConfig);
};
module.exports = utils;