-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrspack.config.ts
169 lines (154 loc) · 4.46 KB
/
rspack.config.ts
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
import path from "node:path";
import { access, rm, mkdir, copyFile } from "node:fs/promises";
import execSync from "node:child_process";
import rspack from "@rspack/core";
import { RsdoctorRspackPlugin } from "@rsdoctor/rspack-plugin";
import createFeatureFlags from "./createFeatureFlags";
const liveBuildMode = "LIVE_BUILD" in process.env; // Live debugging
const debugMode = liveBuildMode || "DEBUG" in process.env;
const serverMode = process.env.SERVER_MODE;
const featureFlags = createFeatureFlags({ debugMode });
if (serverMode) {
// @ts-ignore
featureFlags.REQ_INTERCEPTION_CATCH_ALL = JSON.stringify("referrer");
if (serverMode === "winterjs")
// @ts-ignore
featureFlags.SERVER_ONLY = JSON.stringify("winterjs");
else if (serverMode === JSON.stringify("cf-workers"))
// @ts-ignore
featureFlags.SERVER_ONLY = JSON.stringify("cf-workers");
// @ts-ignore
} else {
// @ts-ignore
featureFlags.REQ_INTERCEPTION_CATCH_ALL = JSON.stringify("clients");
// @ts-ignore
featureFlags.SERVER_ONLY = JSON.stringify(false);
}
//@ts-ignore
featureFlags.GITHUB_REPO = JSON.stringify(featureFlags.GITHUB_REPO);
console.log("The chosen feature flags are:");
console.log(featureFlags);
// biome-ignore lint/suspicious/noExplicitAny: I don't know the exact type to use for this at the moment
const plugins: any = [
// @ts-ignore
new rspack.DefinePlugin(featureFlags)
];
if (debugMode)
plugins.push(
// There are currently a bug with Rsdoctor where the option `disableClientServer` doesn't work. You must launch the bundle analyzer through the cli instead.
new RsdoctorRspackPlugin({
//port: 3300,
// Do not pop up every time (annoying)
disableClientServer: liveBuildMode,
linter: {
rules: {
// Don't warn about using non ES5 features
"ecma-version-check": "off"
}
},
supports: {
generateTileGraph: true
}
})
);
const properDirType = debugMode ? "debug" : "prod";
const properDir = path.resolve(__dirname, "dist", properDirType, "sw");
console.log(`Building in ${properDirType} mode`);
if (liveBuildMode) console.log("Building in live build mode");
const sourceMapType = debugMode ? "eval-source-map" : "source-map";
const config: rspack.Configuration = {
mode: debugMode ? "development" : "production",
//devtool: sourceMapType,
entry: {
sw: path.resolve(__dirname, "./src/this/handleSW.ts")
// Building these bundles separately allows for the user to roll out their own config files without having to build aero as a whole
},
plugins,
resolve: {
extensions: [".ts"],
tsConfig: path.resolve(__dirname, "./tsconfig.json")
},
module: {
rules: [
{
test: /\.ts$/,
exclude: [/[\\/]node_modules[\\/]/],
loader: "builtin:swc-loader"
}
]
},
output: {
filename: "[name].js",
path: properDir,
iife: true,
libraryTarget: "es2022"
},
target: ["webworker", "es2022"]
};
if (debugMode) config.watch = true;
const distDir = path.resolve(__dirname, "dist");
const swDir = path.resolve(__dirname, "dist", properDirType, "sw");
initDist();
function initDist() {
console.info("Initializing the dist folder");
access(distDir)
.then(initProperDir)
// If dir doesn't exist
.catch(createDistDir);
}
function createDistDir() {
console.info("Creating the dist folder");
mkdir(distDir).then(initProperDir);
}
function initProperDir() {
console.info("Initializing the proper folder (...dist/<debug/prod>)");
access(properDir)
.then(() => {
rm(properDir, {
recursive: true
}).then(createProperDir);
})
// If dir doesn't exist
.catch(createProperDir);
}
function createProperDir() {
console.info("Creating the proper folder");
mkdir(properDir).then(initSW);
}
function initSW() {
console.info("Initializing the SW folder (...dist/<debug/prod>/sw");
access(swDir)
.then(() => {
rm(swDir, {
recursive: true
}).then(createSW);
})
// If dir doesn't exist
.catch(createSW);
}
function createSW() {
console.info("Creating the SW folder");
mkdir(path.resolve(swDir)).then(initFiles);
}
function initFiles() {
console.log("Copying over the default files to the dist folder");
copySWFiles();
initLogo();
}
function copySWFiles() {
copyFile(
path.resolve(__dirname, "src/defaultConfig.js"),
path.resolve(`${swDir}/defaultConfig.js`)
).catch(err => {
console.error("Error copying defaultConfig.js:", err);
});
}
function initLogo() {
copyFile(
path.resolve(__dirname, "aero.webp"),
path.resolve(`${swDir}/logo.webp`)
).catch(err => {
console.error("Error copying logo.webp:", err);
});
}
export default config;