-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.config.js
66 lines (59 loc) · 1.86 KB
/
common.config.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
const fs = require('fs');
const path = require('path');
const pathSep = path.sep;
const makeCommonDependencyDir = (depPath) => {
const outputsPath = `${process.cwd()}${pathSep}outputs`;
// path: /User/linjb/split_bundle_demo/outputs/common_dependency
const depFilePath = `${outputsPath}${pathSep}common_dependency`;
// remove client user path
const basename = path.basename(process.cwd());
const writeDepPath = depPath.substr(depPath.indexOf(basename));
if (fs.existsSync(outputsPath)) {
fs.appendFileSync(depFilePath, `\n${writeDepPath}`);
} else {
fs.mkdirSync(outputsPath);
fs.writeFileSync(depFilePath, writeDepPath);
}
};
/**
* A filter function to discard specific modules from the output.
*/
const processModuleFilter = (module) => {
const modulePath = module.path;
if (modulePath.indexOf('__prelude__') >= 0) {
return false;
}
makeCommonDependencyDir(modulePath);
return true;
};
/**
* Used to generate the module id for require statements.
*/
const createModuleIdFactory = () => {
const projectPath = process.cwd();
return (modulePath) => {
let moduleName = '';
// react-native目录下的,取相对路径
if (
modulePath.indexOf(
`node_modules${pathSep}react-native${pathSep}Libraries${pathSep}`,
) > 0
) {
moduleName = modulePath.substr(modulePath.lastIndexOf(pathSep) + 1);
} else if (modulePath.indexOf(projectPath) === 0) {
// 当前项目下的业务代码
moduleName = modulePath.substr(projectPath.length + 1);
}
moduleName = moduleName.replace('.js', '');
moduleName = moduleName.replace('.png', '');
const regExp = new RegExp(pathSep === '\\' ? '\\\\' : pathSep, 'gm');
moduleName = moduleName.replace(regExp, '_');
return moduleName;
};
};
module.exports = {
serializer: {
createModuleIdFactory,
processModuleFilter,
},
};