-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
178 lines (152 loc) · 4.92 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
import esbuild from 'esbuild';
import fs from 'fs/promises';
import path from 'path';
import crypto from 'crypto';
import express from 'express';
import cors from 'cors';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rteManifestFileName = 'manifest.json';
const entryFile = path.resolve(__dirname, './src/rte/index.ts');
const outDir = path.resolve(__dirname, './dist/sfextensions/rte');
const tsconfigPath = path.resolve(__dirname, 'tsconfig.json');
const port = 4201;
function generateHash(content) {
return crypto.createHash('sha256').update(content).digest('hex').slice(0, 20);
}
const hashAndManifestPlugin = {
name: 'hash-and-manifest',
setup(build) {
build.onEnd(async (result) => {
if (result.errors.length > 0) {
console.error('Build failed with errors.');
return;
}
const manifest = {};
await fs.mkdir(outDir, { recursive: true });
for (const file of result.outputFiles) {
const content = file.contents;
const hash = generateHash(content);
const ext = path.extname(file.path);
const baseName = path.basename(file.path, ext);
const newFileName = `${baseName}.${hash}${ext}`;
const newFilePath = path.join(outDir, newFileName);
await fs.writeFile(newFilePath, content);
console.log(`Written file: ${newFileName}`);
manifest[baseName] = newFileName;
}
const manifestPath = path.join(outDir, rteManifestFileName);
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 4));
console.log(`Manifest generated at: ${rteManifestFileName}`);
});
},
};
// Function to clean the output directory
async function cleanOutDir() {
try {
await fs.rm(outDir, { recursive: true, force: true });
await fs.mkdir(outDir, { recursive: true });
console.log(`Cleaned output directory: ${outDir}`);
} catch (error) {
console.error(`Error cleaning output directory: ${error}`);
process.exit(1);
}
}
async function buildProject() {
try {
await esbuild.build({
entryPoints: [entryFile],
bundle: true,
format: 'iife',
minify: true,
keepNames: false,
splitting: false,
tsconfig: tsconfigPath,
outdir: outDir,
legalComments: 'none',
write: false,
plugins: [hashAndManifestPlugin],
metafile: true,
});
console.log('Build completed successfully.');
} catch (error) {
console.error(`Build failed: ${error}`);
process.exit(1);
}
}
async function watchProject() {
try {
let ctx = await esbuild.context({
entryPoints: [entryFile],
bundle: true,
format: 'iife',
minify: true,
keepNames: false,
splitting: false,
tsconfig: tsconfigPath,
outdir: outDir,
legalComments: 'none',
write: false,
plugins: [hashAndManifestPlugin],
metafile: true,
});
ctx.watch();
console.log('Watch completed successfully.');
} catch (error) {
console.error(`Watch failed: ${error}`);
process.exit(1);
}
}
async function startServer() {
await buildProject();
const context = await esbuild.context({
entryPoints: [entryFile],
bundle: true,
format: 'iife',
minify: true,
keepNames: false,
splitting: false,
tsconfig: tsconfigPath,
outdir: outDir,
legalComments: 'none',
write: false,
plugins: [hashAndManifestPlugin],
metafile: true,
});
// Start watching for changes
await context.watch();
// Initialize Express app
const app = express();
app.use(cors({
origin: '*', // Allow all origins
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ["*"],
}));
app.use("/sfextensions/rte", express.static(outDir));
app.options('*', cors());
app.listen(port, () => {
console.log(`Express server is running at http://localhost:${port}`);
});
console.log('Watching for changes...');
}
async function main() {
const args = process.argv.slice(2);
const mode = args[0];
if (mode === 'build') {
await cleanOutDir();
await buildProject();
} else if (mode === 'start') {
await cleanOutDir();
await startServer();
} else if (mode === 'watch') {
await cleanOutDir();
await watchProject();
}
else {
console.error('Invalid mode. Use "build", "watch" or "start".');
process.exit(1);
}
}
main();