-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
92 lines (80 loc) · 2.53 KB
/
esbuild.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
import * as esbuild from 'esbuild';
import { promises as fs } from "fs";
import express from 'express';
import path from 'path';
import cors from 'cors';
const esbuildSharedOptions = {
bundle: true,
write: false,
outdir: 'out',
packages: 'external',
platform: 'node',
format: 'iife',
}
async function processRepository(ctx, repositoryPath) {
// Check and create the dist directory if it doesn't exist
const distDir = `${repositoryPath}/../dist`;
try {
await fs.access(distDir);
} catch {
await fs.mkdir(distDir, { recursive: true });
}
// TODO: add multiple possible entry points
let result = await ctx.rebuild({
entryPoints: [`${repositoryPath}/plugin.js`],
bundle: true,
write: false, // Don't write to disk, return in outputFiles instead
outdir: 'out',
packages: 'external',
platform: 'node',
format: 'iife',
});
for (let out of result.outputFiles) {
// Append "return plugin;" at the end of the generated iife, before the closing brackets
let result = out.text.replace(/^}\)\(\);$/gm, " return plugin;\n})()");
// Remove any lines attempting to import module using the esbuild __require
result = result.replace(/^\s+var import_.+= (?:__toESM\()?__require\(".+"\).*;/gm, "");
const outputFile = `${distDir}/out.plugin.js`;
console.log('File has been written successfully');
return fs.writeFile(outputFile, result);
}
}
async function startServer(ctx, repositoryPath) {
const app = express();
const port = 3000;
app.use(cors());
// Endpoint to provide the file contents
app.get('/code', async (req, res) => {
const result = await processRepository(ctx, repositoryPath);
res.sendFile(path.resolve(`${repositoryPath}/../dist/out.plugin.js`));
});
let server = app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
server.on('close', () => {
console.log('Server has stopped');
ctx.dispose();
});
}
async function main() {
const repositoryPath = process.argv[2];
if (!repositoryPath) {
console.error('Please provide the path of the repository as a command-line argument.');
process.exit(1);
}
let opts = esbuildSharedOptions;
opts.entryPoints = [`${repositoryPath}/plugin.js`];
let ctx = await esbuild.context(opts);
if (process.argv.includes('--server')) {
await startServer(ctx, repositoryPath);
} else {
await processRepository(ctx, repositoryPath);
ctx.dispose()
}
}
main().then(() => {
console.log("Finished execution");
}
).catch(() => {
console.log("error");
})