-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcraco.config.js
More file actions
199 lines (176 loc) · 6.22 KB
/
Copy pathcraco.config.js
File metadata and controls
199 lines (176 loc) · 6.22 KB
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
const path = require('path');
const CracoCSSModules = require('craco-css-modules');
const CracoEnvPlugin = require('craco-plugin-env')
const BabelRcPlugin = require('@jackwilsdon/craco-use-babelrc');
const isProd = process.env.APPMODE === "production";
const nodeModulesWatchIgnore = '**/node_modules/**';
const watchPollInterval = 1000;
/**
* Appends the node_modules ignore pattern to an existing watch ignore config.
*
* @param {RegExp|string|Array<RegExp|string>|undefined} ignored - Existing ignore config from webpack or dev server watches.
* @returns {Array<RegExp|string>} Watch ignore config with node_modules excluded. Used by CRACO watch overrides.
* @throws This function does not throw.
*/
function withNodeModulesWatchIgnore(ignored) {
return [
...(Array.isArray(ignored) ? ignored : ignored ? [ignored] : []),
nodeModulesWatchIgnore,
];
}
/**
* Converts CRA's webpack-dev-server v4 hook registrations into v5 middleware entries.
*
* @param {Function|undefined} hook - Legacy CRA middleware hook.
* @param {object} devServer - Active webpack-dev-server instance.
* @param {string} namePrefix - Stable prefix used for middleware diagnostics.
* @returns {Array<object>} Middleware entries accepted by webpack-dev-server v5.
*/
function collectLegacyMiddlewares(hook, devServer, namePrefix) {
if (!hook) {
return [];
}
const originalApp = devServer.app;
const appProxy = Object.create(originalApp);
const registrations = [];
appProxy.use = (...args) => {
registrations.push(args);
return appProxy;
};
devServer.app = appProxy;
try {
hook(devServer);
} finally {
devServer.app = originalApp;
}
return registrations.flatMap((registration, registrationIndex) => {
const args = [...registration];
const path = typeof args[0] === 'string' ? args.shift() : undefined;
return args.map((middleware, middlewareIndex) => ({
name: `${namePrefix}-${registrationIndex}-${middlewareIndex}`,
...(path ? { path } : {}),
middleware,
}));
});
}
/**
* Preserves CRA's dev-server static config while disabling public asset watches.
*
* @param {object} devServerConfig - CRA webpack-dev-server config passed through CRACO for yarn start.
* @returns {object} Dev-server config that serves public assets without consuming file watchers for them.
* @throws This function does not throw.
*/
function configureDevServer(devServerConfig) {
const {
https,
onAfterSetupMiddleware,
onBeforeSetupMiddleware,
setupMiddlewares,
...supportedConfig
} = devServerConfig;
const staticConfig = devServerConfig.static || {};
return {
...supportedConfig,
...(https ? {
server: {
type: 'https',
options: typeof https === 'object' ? https : {},
},
} : {}),
static: {
...staticConfig,
watch: false,
},
setupMiddlewares: (middlewares, devServer) => {
// CRA 5 still calls the webpack-dev-server v4 shutdown method.
if (!devServer.close && devServer.stopCallback) {
devServer.close = devServer.stopCallback.bind(devServer);
}
const configuredMiddlewares = setupMiddlewares
? setupMiddlewares(middlewares, devServer)
: middlewares;
const beforeMiddlewares = collectLegacyMiddlewares(
onBeforeSetupMiddleware,
devServer,
'cra-before-setup',
);
const afterMiddlewares = collectLegacyMiddlewares(
onAfterSetupMiddleware,
devServer,
'cra-after-setup',
);
return [
...beforeMiddlewares,
...configuredMiddlewares,
...afterMiddlewares,
];
},
};
}
/**
* Preserves CRA's webpack config while polling watch mode and ignoring node_modules.
*
* @param {object} webpackConfig - CRA webpack config passed through CRACO.
* @returns {object} Webpack config with polling enabled and node_modules excluded from watchOptions.
* @throws This function does not throw.
*/
function configureWebpack(webpackConfig) {
return {
...webpackConfig,
watchOptions: {
...webpackConfig.watchOptions,
ignored: withNodeModulesWatchIgnore(webpackConfig.watchOptions?.ignored),
poll: watchPollInterval,
},
};
}
function getModeName() {
const index = process.argv.indexOf('--mode');
return index === -1 ? '' : process.argv[index + 1] || ''
}
console.log({buildMode: getModeName()});
const localIdentName = isProd
? "[hash:base64:6]"
: "[name]_[local]__[hash:base64:6]";
const resolve = dir => path.resolve(__dirname, dir);
module.exports = {
jest: {
configure: {
moduleNameMapper: {
// Jest 27 cannot resolve React Router 7's nested conditional export.
'^react-router/dom$': '<rootDir>/test/react-router-dom.cjs',
},
},
},
style: {
modules: {
localIdentName,
},
},
plugins: [
{ plugin: BabelRcPlugin },
{ plugin: CracoCSSModules },
{ plugin: CracoEnvPlugin, options: {
envDir: './.environments',
} },
],
devServer: configureDevServer,
webpack: {
alias: {
// aliases used in JS/TS
'~': resolve('src'),
'@earn': resolve('src/apps/earn/src'),
'@learn': resolve('src/apps/learn/src'),
'@devCenter': resolve('src/apps/dev-center/src'),
'@gamificationAdmin': resolve('src/apps/gamification-admin/src'),
'@profiles': resolve('src/apps/profiles/src'),
'@wallet': resolve('src/apps/wallet/src'),
'@walletAdmin': resolve('src/apps/wallet-admin/src'),
'@engagements': resolve('src/apps/engagements/src'),
'@platform': resolve('src/apps/platform/src'),
// aliases used in SCSS files
'@libs/ui/styles': resolve('src/libs/ui/lib/styles'),
},
configure: configureWebpack,
}
}