forked from interstellard/chatgpt-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
182 lines (168 loc) · 6.07 KB
/
build.mjs
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
import esbuild from "esbuild";
import archiver from "archiver";
import fs from "fs-extra";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";
import postcssPlugin from "esbuild-style-plugin";
import copyStaticFilesPlugin from "esbuild-copy-files-plugin";
import path from 'path';
// Define the build directory and whether the build should be minified or not
const buildDir = "build";
const minify = process.argv.includes("--minify");
// Function to clean the build directory before building the project
async function cleanBuildDir() {
// Read the entries in the build directory
const entries = await fs.readdir(buildDir);
// Loop through the entries and remove everything except for .zip files
for (const entry of entries) {
if (path.extname(entry) === ".zip") continue;
await fs.remove(`${buildDir}/${entry}`);
}
}
// Function to build the project with esbuild
async function runEsbuild() {
// Specify the entry points for esbuild to process
await esbuild.build({
entryPoints: [
"src/content-scripts/mainUI.tsx",
"src/background/bg.ts",
"src/options/options.tsx",
],
// Specify the output directory for esbuild to write the processed files to
outdir: buildDir,
// Bundle the output into a single file
bundle: true,
// Minify the output, only enabled if --minify is passed as an argument
minify: minify,
// Use tree shaking to remove unused code
treeShaking: true,
// Define environment variables for the build
define: {
"process.env.NODE_ENV": '"production"',
},
// Set the JSX factory function to 'h'
jsxFactory: "h",
// Set the JSX fragment to 'Fragment'
jsxFragment: "Fragment",
// Automatically detect JSX syntax in TypeScript files
jsx: "automatic",
// Load .png files as data URLs
loader: {
".png": "dataurl",
},
// Add plugins to the esbuild build process
plugins: [
// Use postcss to process CSS with tailwindcss and autoprefixer
postcssPlugin({
postcss: {
plugins: [tailwindcss, autoprefixer],
},
}),
// Copy the manifest.json, assets and locales folders to the build directory
copyStaticFilesPlugin({
source: ["src/manifest.json", "src/assets/"],
target: buildDir,
copyWithFolder: false,
}),
// Use the esbuild-copy-files-plugin to copy the options.html file
copyStaticFilesPlugin({
// Source file to copy
source: ["src/options/options.html"],
// Target directory
target: buildDir + "/options",
copyWithFolder: false,
}),
copyStaticFilesPlugin({
source: ["src/_locales/"],
target: buildDir,
copyWithFolder: true,
}),
],
});
}
async function createZipExtensionForBrowser(browser) {
// Read the manifest.json file in the build directory
const manifest = await fs.readJson(`${buildDir}/manifest.json`);
// Get the version from the manifest file
const version = manifest.version;
// Create the archive name using the version and browser name
let archiveName = `build/webchatgpt-${version}-${browser}.zip`;
// Create a new zip archive using the archiver library
const archive = archiver("zip", { zlib: { level: 9 } });
// Create a write stream for the archive file
const stream = fs.createWriteStream(archiveName);
// Pipe the archive data to the write stream
archive.pipe(stream);
// Add files to the zip archive
await addFilesToZip(archive, browser);
// Log a message indicating that the zip archive is being created
console.log(`Creating ${archiveName}…`);
// Finalize the archive and write all data to the archive file
archive.finalize();
}
async function addFilesToZip(archive, browser) {
// Read the entries in the build directory
const entries = await fs.readdir("build");
// Loop through each entry
for (const entry of entries) {
// Get the entry's stat information
const entryStat = await fs.stat(`build/${entry}`);
// Check if the entry is a directory
if (entryStat.isDirectory()) {
// If so, add the directory to the archive
archive.directory(`build/${entry}`, entry);
} else {
// If not, check if the entry is a zip file or the manifest.json file
if (path.extname(entry) === ".zip") continue;
if (entry === "manifest.json") continue;
// If not, add the file to the archive
archive.file(`build/${entry}`, { name: entry });
}
}
// Check if the browser is firefox
if (browser === "firefox") {
// If so, add the firefox-specific manifest file to the archive
archive.file("src/manifest.v2.json", { name: "manifest.json" });
// Check if the browser is chrome
} else if (browser === "chrome") {
// If so, add the chrome-specific manifest file to the archive
archive.file("build/manifest.json", { name: "manifest.json" });
}
}
async function build() {
// Clean up the build directory
await cleanBuildDir();
// Compile the code using esbuild
await runEsbuild();
// Check if the --create-zips flag is present in the command line arguments
const createZips = process.argv.includes("--create-zips");
// If the flag is present, create the ZIP extensions for Chrome and Firefo
if (createZips) {
try {
// Delete any existing ZIP files in the build folde
await deleteZipsInBuildFolder();
// Create the ZIP extension for Chrome
await createZipExtensionForBrowser("chrome");
// Create the ZIP extension for Firefox
await createZipExtensionForBrowser("firefox");
// Log any errors that occur during the process
} catch (error) {
console.error(error);
}
}
// Log that the build process is complete
console.log("Build complete");
async function deleteZipsInBuildFolder() {
// Read the entries in the build folder
const entries = await fs.readdir("build");
// Iterate over each entry
for (const entry of entries) {
// If the entry is a ZIP file
if (path.extname(entry) === ".zip") {
// Delete the ZIP file
await fs.remove(`build/${entry}`);
}
}
}
}
build();