/
cra.js
255 lines (184 loc) · 6.53 KB
/
cra.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const path = require("path");
const semver = require("semver");
const { log } = require("./logger");
const { projectRoot } = require("./paths");
let envLoaded = false;
const CRA_LATEST_SUPPORTED_MAJOR_VERSION = "4.0.0";
/************ Common *******************/
function resolveConfigFilePath(cracoConfig, fileName) {
if (!envLoaded) {
// Environment variables must be loaded before the CRA paths, otherwise they will not be applied.
require(resolveConfigFilePathInner(cracoConfig, "env.js"));
envLoaded = true;
}
return resolveConfigFilePathInner(cracoConfig, fileName);
}
function resolveConfigFilePathInner(cracoConfig, fileName) {
return require.resolve(path.join(cracoConfig.reactScriptsVersion, "config", fileName), { paths: [projectRoot] });
}
function resolveScriptsFilePath(cracoConfig, fileName) {
return require.resolve(path.join(cracoConfig.reactScriptsVersion, "scripts", fileName), { paths: [projectRoot] });
}
function resolveReactDevUtilsPath(fileName) {
return require.resolve(path.join("react-dev-utils", fileName), { paths: [projectRoot] });
}
function overrideModule(modulePath, newModule) {
require.cache[modulePath].exports = newModule;
log(`Overrided require cache for module: ${modulePath}`);
}
function resolvePackageJson(cracoConfig) {
return require.resolve(path.join(cracoConfig.reactScriptsVersion, "package.json"), { paths: [projectRoot] });
}
function getReactScriptVersion(cracoConfig) {
const reactScriptPackageJsonPath = resolvePackageJson(cracoConfig);
const { version } = require(reactScriptPackageJsonPath);
return {
version,
isSupported: semver.gte(version, CRA_LATEST_SUPPORTED_MAJOR_VERSION)
};
}
/************ Paths *******************/
let _resolvedCraPaths = null;
function getCraPaths(cracoConfig) {
if (!_resolvedCraPaths) {
_resolvedCraPaths = require(resolveConfigFilePath(cracoConfig, "paths.js"));
}
return _resolvedCraPaths;
}
/************ Webpack Dev Config *******************/
function getWebpackDevConfigPath(cracoConfig) {
try {
return {
filepath: resolveConfigFilePath(cracoConfig, "webpack.config.js"),
isLegacy: false
};
} catch (e) {
return {
filepath: resolveConfigFilePath(cracoConfig, "webpack.config.dev.js"),
isLegacy: true
};
}
}
function loadWebpackDevConfig(cracoConfig) {
const result = getWebpackDevConfigPath(cracoConfig);
log("Found Webpack dev config at: ", result.filepath);
if (result.isLegacy) {
return require(result.filepath);
}
return require(result.filepath)("development");
}
function overrideWebpackDevConfig(cracoConfig, newConfig) {
const result = getWebpackDevConfigPath(cracoConfig);
if (result.isLegacy) {
overrideModule(result.filepath, newConfig);
} else {
overrideModule(result.filepath, () => newConfig);
}
log("Overrided Webpack dev config.");
}
/************ Webpack Prod Config *******************/
function getWebpackProdConfigPath(cracoConfig) {
try {
return {
filepath: resolveConfigFilePath(cracoConfig, "webpack.config.js"),
isLegacy: false
};
} catch (e) {
return {
filepath: resolveConfigFilePath(cracoConfig, "webpack.config.prod.js"),
isLegacy: true
};
}
}
function loadWebpackProdConfig(cracoConfig) {
const result = getWebpackProdConfigPath(cracoConfig);
log("Found Webpack prod config at: ", result.filepath);
if (result.isLegacy) {
return require(result.filepath);
}
return require(result.filepath)("production");
}
function overrideWebpackProdConfig(cracoConfig, newConfig) {
const result = getWebpackProdConfigPath(cracoConfig);
if (result.isLegacy) {
overrideModule(result.filepath, newConfig);
} else {
overrideModule(result.filepath, () => newConfig);
}
log("Overrided Webpack prod config.");
}
/************ Dev Server *******************/
function getDevServerConfigPath(cracoConfig) {
return resolveConfigFilePath(cracoConfig, "webpackDevServer.config.js");
}
function getDevServerUtilsPath() {
return resolveReactDevUtilsPath("WebpackDevServerUtils.js");
}
function loadDevServerConfigProvider(cracoConfig) {
const filepath = getDevServerConfigPath(cracoConfig);
log("Found dev server config at: ", filepath);
return require(filepath);
}
function overrideDevServerConfigProvider(cracoConfig, configProvider) {
const filepath = getDevServerConfigPath(cracoConfig);
overrideModule(filepath, configProvider);
log("Overrided dev server config provider.");
}
function loadDevServerUtils() {
const filepath = getDevServerUtilsPath();
log("Found dev server utils at: ", filepath);
return require(filepath);
}
function overrideDevServerUtils(newUtils) {
const filepath = getDevServerUtilsPath();
overrideModule(filepath, newUtils);
log("Overrided dev server utils.");
}
/************ Jest *******************/
function getCreateJestConfigPath(cracoConfig) {
return resolveScriptsFilePath(cracoConfig, "utils/createJestConfig.js");
}
function loadJestConfigProvider(cracoConfig) {
const filepath = getCreateJestConfigPath(cracoConfig);
log("Found jest config at: ", filepath);
return require(filepath);
}
function overrideJestConfigProvider(cracoConfig, configProvider) {
const filepath = getCreateJestConfigPath(cracoConfig);
overrideModule(filepath, configProvider);
log("Overrided Jest config provider.");
}
/************ Scripts *******************/
function start(cracoConfig) {
const filepath = resolveScriptsFilePath(cracoConfig, "start.js");
log("Starting CRA at: ", filepath);
require(filepath);
}
function build(cracoConfig) {
const filepath = resolveScriptsFilePath(cracoConfig, "build.js");
log("Building CRA at: ", filepath);
require(filepath);
}
function test(cracoConfig) {
const filepath = resolveScriptsFilePath(cracoConfig, "test.js");
log("Testing CRA at: ", filepath);
require(filepath);
}
/************ Exports *******************/
module.exports = {
loadWebpackDevConfig,
overrideWebpackDevConfig,
loadWebpackProdConfig,
overrideWebpackProdConfig,
loadDevServerConfigProvider,
overrideDevServerConfigProvider,
loadDevServerUtils,
overrideDevServerUtils,
loadJestConfigProvider,
overrideJestConfigProvider,
getCraPaths,
getReactScriptVersion,
start,
build,
test
};