-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraco.config.js
More file actions
112 lines (94 loc) · 3.3 KB
/
craco.config.js
File metadata and controls
112 lines (94 loc) · 3.3 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
// craco.config.js
const path = require("path");
require("dotenv").config();
// Environment variable overrides
const config = {
disableHotReload: process.env.DISABLE_HOT_RELOAD === "true",
enableVisualEdits: process.env.REACT_APP_ENABLE_VISUAL_EDITS === "true",
enableHealthCheck: process.env.ENABLE_HEALTH_CHECK === "true",
};
// Conditionally load visual editing modules only if enabled
let babelMetadataPlugin;
let setupDevServer;
if (config.enableVisualEdits) {
babelMetadataPlugin = require("./plugins/visual-edits/babel-metadata-plugin");
setupDevServer = require("./plugins/visual-edits/dev-server-setup");
}
// Conditionally load health check modules only if enabled
let WebpackHealthPlugin;
let setupHealthEndpoints;
let healthPluginInstance;
if (config.enableHealthCheck) {
WebpackHealthPlugin = require("./plugins/health-check/webpack-health-plugin");
setupHealthEndpoints = require("./plugins/health-check/health-endpoints");
healthPluginInstance = new WebpackHealthPlugin();
}
const webpackConfig = {
webpack: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
configure: (webpackConfig) => {
// Disable hot reload completely if environment variable is set
if (config.disableHotReload) {
// Remove hot reload related plugins
webpackConfig.plugins = webpackConfig.plugins.filter(plugin => {
return !(plugin.constructor.name === 'HotModuleReplacementPlugin');
});
// Disable watch mode
webpackConfig.watch = false;
webpackConfig.watchOptions = {
ignored: /.*/, // Ignore all files
};
} else {
// Add ignored patterns to reduce watched directories
webpackConfig.watchOptions = {
...webpackConfig.watchOptions,
ignored: [
'**/node_modules/**',
'**/.git/**',
'**/build/**',
'**/dist/**',
'**/coverage/**',
'**/public/**',
],
};
}
// Add health check plugin to webpack if enabled
if (config.enableHealthCheck && healthPluginInstance) {
webpackConfig.plugins.push(healthPluginInstance);
}
return webpackConfig;
},
},
};
// Only add babel plugin if visual editing is enabled
if (config.enableVisualEdits) {
webpackConfig.babel = {
plugins: [babelMetadataPlugin],
};
}
// Setup dev server with visual edits and/or health check
if (config.enableVisualEdits || config.enableHealthCheck) {
webpackConfig.devServer = (devServerConfig) => {
// Apply visual edits dev server setup if enabled
if (config.enableVisualEdits && setupDevServer) {
devServerConfig = setupDevServer(devServerConfig);
}
// Add health check endpoints if enabled
if (config.enableHealthCheck && setupHealthEndpoints && healthPluginInstance) {
const originalSetupMiddlewares = devServerConfig.setupMiddlewares;
devServerConfig.setupMiddlewares = (middlewares, devServer) => {
// Call original setup if exists
if (originalSetupMiddlewares) {
middlewares = originalSetupMiddlewares(middlewares, devServer);
}
// Setup health endpoints
setupHealthEndpoints(devServer, healthPluginInstance);
return middlewares;
};
}
return devServerConfig;
};
}
module.exports = webpackConfig;