-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
174 lines (152 loc) · 4.85 KB
/
next.config.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
/* eslint-disable */
const Os = require("os");
const fs = require("fs");
const defaultRuntimeCaching = require("next-pwa/cache");
const withPWA = require('next-pwa')({
dest: 'public',
// Disable PWA in dev mode, if you need to debug PWA in dev mode, plz turn it on
disable: process.env.NODE_ENV === 'development',
/*
Force next-pwa to generate worker box production build by specify the option mode: 'production' in your pwa section of next.config.js. Though next-pwa automatically generate the worker box development build during development (by running next) and worker box production build during production (by running next build and next start). You may still want to force it to production build even during development of your web app for following reason:
Reduce logging noise due to production build doesn't include logging.
Improve performance a bit duOe to production build is optimized and minified.
*/
// mode: 'production', // Disable this if you want to debug PWA in dev mode
publicExcludes: [
'!version.json', // try to avoid version.json cache
],
// runtimeCaching: [
// {
// urlPattern: /\/api\/todoitem/,
// method: "GET",
// handler: "StaleWhileRevalidate",
// options: {
// cacheName: "todoApp-api",
// expiration: {
// maxEntries: 64,
// maxAgeSeconds: 24 * 60 * 60, // 24 hours
// },
// },
// },
// ...defaultRuntimeCaching,
// ],
})
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
// https://nextjs.org/docs/advanced-features/output-file-tracing#automatically-copying-traced-files
output: "standalone",
// pwa: {}, // the same as you passed into next-pwa option above
/**
* From Static structure: /about.html
* To Static structure: /about/index.html
*
* But this also make all client url end with a `/`, so we will turn it off
* https://nextjs.org/docs/api-reference/next.config.js/exportPathMap#adding-a-trailing-slash
*/
// trailingSlash: false,
// images: { loader: "custom" },
images: {
loader: "imgix",
path: "",
},
/**
* Custom webpack config
* https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config
*/
webpack: (config, { isServer }) => {
const rules = config.module.rules;
inject_git_commit_id_to_page(rules);
inject_app_env(rules);
return config;
},
};
module.exports = withPWA(nextConfig);
/**
* Determine whether the Node.js process runs on Windows.
*
* @returns {Boolean}
*/
function isWindows() {
return Os.platform() === "win32";
}
function inject_git_commit_id_to_page(rules) {
if (isWindows()) {
throw new Error("NOTE: You need to run in on Mac, Linux, or WSL, We prohibit Windows");
}
/**
* Inject git commit id into debug page
*/
let git_commit_id = "";
try {
git_commit_id = require("child_process").execSync("git rev-parse --short HEAD").toString().trim();
} catch (e) {
throw new Error("Please install git first");
}
// Insert into Env.ts
const stringReplaceLoaderRule = {
test: /src\/utils\/env\.ts$/,
loader: "string-replace-loader",
options: {
search: "LUCIS_VERSION_COMMIT_ID",
replace: git_commit_id,
},
};
rules.push(stringReplaceLoaderRule);
// Insert into version.json
const versionJsonContent = { "version": git_commit_id, "created": Date.now() };
fs.writeFileSync('public/version.json', JSON.stringify(versionJsonContent));
}
function inject_app_env(rules) {
if (isWindows()) {
throw new Error("NOTE: You need to run in on Mac, Linux, or WSL, We prohibit Windows");
}
const git_branch = require("child_process")
/**
* NOTE: You need to run in on Mac, Linux, or WSL, We prohibit Windows
*/
.execSync("cat .git/HEAD")
// .execSync('git branch --show-current')
.toString()
.trim();
let app_env = "";
if (git_branch === "ref: refs/heads/main") {
app_env = "prod";
} else if (git_branch === "ref: refs/heads/beta") {
app_env = "beta";
} else if (git_branch === "ref: refs/heads/test") {
app_env = "stg";
} else {
app_env = "dev";
}
rules.push({
test: /src\/utils\/env\.ts$/,
loader: "string-replace-loader",
options: {
search: '"APP_ENV"',
replace: `"${app_env}"`,
},
});
}
function show_testnet_text_on_header(rules) {
/**
* Show testnet text on the header
*/
const git_branch = require("child_process")
/**
* NOTE: You need to run in on Mac, Linux, or WSL, We prohibit Windows
*/
.execSync("cat .git/HEAD")
// .execSync('git branch --show-current')
.toString()
.trim();
rules.push({
test: /components\/ui\/header\/Header\.tsx$/,
loader: "string-replace-loader",
options: {
search: '"IS_TESTNET"',
replace: (git_branch === "ref: refs/heads/trial").toString(),
},
});
}