-
Notifications
You must be signed in to change notification settings - Fork 11
/
metro.config.base.js
120 lines (113 loc) · 3.18 KB
/
metro.config.base.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
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
const fs = require("fs");
const crypto = require("crypto");
const defaultCreateModuleIdFactory = require("./node_modules/metro/src/lib/createModuleIdFactory");
const BUILD_TYPE_COMMON = "common";
const BUILD_TYPE_DEFAULT = "default";
const BUILD_TYPE_DIFF = "diff";
const moduleIdsMapFilePath = "./repo_for_module_id.json";
/**
*
* @param {*} filepath
*/
function getOrCreateModuleIdsJsonObj(filepath) {
if (fs.existsSync(filepath)) {
console.log(`init map from file : ${filepath}`);
let data = fs.readFileSync(filepath, "utf-8");
return JSON.parse(data);
} else {
return {};
}
}
/**
*
* @param {*} filepath
* @param {*} jsonObj
*/
function saveModuleIdsJsonObj(filepath, jsonObj) {
let data = JSON.stringify(jsonObj);
fs.writeFile(filepath, data, err => {
if (err) throw err;
console.log(`Save ${filepath} SUCCESS.`);
});
}
/**
* Get the key, which used to find the Id in local storage
* @param {get} path
*/
function getFindKey(path) {
let md5 = crypto.createHash("md5");
md5.update(path);
let findKey = md5.digest("hex");
return findKey;
}
var moduleIdsJsonObj = {};
/**
* Create a Id with local storage.
*/
buildCreateModuleIdFactoryWithLocalStorage = function(buildConfig) {
if (buildConfig.type == BUILD_TYPE_DEFAULT) {
// return the orginal createModuleIdFactory implements.
// Please. See node_modules / metro / src / lib / createModuleIdFactory.js
return defaultCreateModuleIdFactory;
} else {
let currentModuleId = 0;
// init moduleIdsJsonObj from file;
moduleIdsJsonObj = getOrCreateModuleIdsJsonObj(moduleIdsMapFilePath);
// init currentModuleId;
for (var key in moduleIdsJsonObj) {
currentModuleId =
currentModuleId > moduleIdsJsonObj[key].id
? currentModuleId
: moduleIdsJsonObj[key].id;
}
console.log("currentModuleId = " + currentModuleId);
return () => {
return path => {
// console.log(`buildType: ${buildType}`);
let findKey = getFindKey(path);
if (moduleIdsJsonObj[findKey] == null) {
moduleIdsJsonObj[findKey] = {
id: ++currentModuleId,
type: buildConfig.type
};
saveModuleIdsJsonObj(moduleIdsMapFilePath, moduleIdsJsonObj);
}
let id = moduleIdsJsonObj[findKey].id;
// console.log(`createModuleIdFactory id = ${id} for ${path}`);
return id;
};
};
}
};
buildProcessModuleFilter = function(buildConfig) {
return moduleObj => {
let path = moduleObj.path;
if (!fs.existsSync(path)) {
return true;
}
if (buildConfig.type == BUILD_TYPE_DIFF) {
let findKey = getFindKey(path);
let storeObj = moduleIdsJsonObj[findKey];
if (storeObj != null && storeObj.type == BUILD_TYPE_COMMON) {
return false;
}
return true;
}
return true;
};
};
module.exports = {
BuildType: {
COMMON: BUILD_TYPE_COMMON,
DEFAULT: BUILD_TYPE_DEFAULT,
DIFF: BUILD_TYPE_DIFF
},
buildCreateModuleIdFactory: buildCreateModuleIdFactoryWithLocalStorage,
buildProcessModuleFilter: buildProcessModuleFilter
};