-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig-overrides.js
76 lines (67 loc) · 1.94 KB
/
config-overrides.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
// config-overrides.js
const fs = require('fs');
const path = require('path');
const gitSha =
process.env.VERCEL_GIT_COMMIT_SHA ??
process.env.GITHUB_SHA ??
process.env.GIT_SHA;
module.exports = {
// The Webpack config to use when compiling your react app for development or production.
webpack: function (config, env) {
// New config, e.g. config.plugins.push...
delete config.module.rules[1].oneOf[3].include;
// Add a hook to write the version file after build
config.plugins.push({
apply: (compiler) => {
compiler.hooks.afterEmit.tap('CreateVersionFile', (compilation) => {
const versionPath = path.join(
compiler.options.output.path,
'version'
);
try {
fs.writeFileSync(versionPath, gitSha ?? env);
} catch (error) {
console.error('Error writing version file:', error);
}
});
compiler.hooks.afterEmit.tap('GenerateEnvJS', (compilation) => {
const envPath = path.join(compiler.options.output.path, 'env.js');
try {
// Filter out any keys that start with 'npm_'
const filteredEnv = Object.fromEntries(
Object.entries(process.env).filter(
([key]) =>
key.startsWith('REACT_APP') ||
key.startsWith('NEXT_PUBLIC') ||
key.startsWith('PUBLIC_URL')
)
);
fs.writeFileSync(
envPath,
`window.env = ${JSON.stringify(filteredEnv)}`
);
} catch (error) {
console.error('Error writing env file:', error);
}
});
}
});
return config;
},
devServer: function (configFunction) {
return function (proxy, allowedHost) {
const config = configFunction(proxy, allowedHost);
// Configure dev server to serve env.js
config.devMiddleware = {
writeToDisk: true
};
// Add static serving configuration
config.static = {
directory: path.join(__dirname, 'build'),
publicPath: process.env.PUBLIC_URL ?? '/login',
watch: true
};
return config;
};
}
};