Repository navigation
Expand file tree
/
Copy pathesbuild.mjs
More file actions
158 lines (144 loc) · 4.5 KB
/
Copy pathesbuild.mjs
File metadata and controls
158 lines (144 loc) · 4.5 KB
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
import * as esbuild from "esbuild";
import * as fs from "fs";
import * as path from "path";
import * as process from "process";
import { typecheckPlugin } from "@jgoz/esbuild-plugin-typecheck";
/**
* @type {import('esbuild').LogLevel}
*/
const LOG_LEVEL = "error";
/**
* @type {boolean}
*/
const production = process.argv.includes("--production");
/**
* @type {import('esbuild').BuildOptions[]}
*/
const projects = [
{
entryPoints: ["./vscode-client/extension"],
format: "cjs",
platform: "node",
// The extension runs in the Node.js of VS Code's Electron, not in the one that builds it.
target: "node24",
outfile: "out/extension.js",
external: ["vscode"],
},
{
entryPoints: ["./vscode-client/rendererLog"],
format: "esm",
platform: "browser",
outfile: "out/rendererLog.js",
external: ["vscode"],
loader: {
".ttf": "file",
".css": "text",
},
},
{
// The page of the Documentation Viewer; codicon.css is linked by the page, as vscode-elements expects.
entryPoints: [
"./vscode-client/documentationViewer",
"./vscode-client/documentationViewer/viewer.css",
"./node_modules/@vscode/codicons/dist/codicon.css",
],
format: "esm",
platform: "browser",
outdir: "out/documentationViewer",
entryNames: "[name]",
loader: {
".ttf": "file",
},
},
];
/**
*
* @param {import('esbuild').BuildOptions} project
*/
async function buildProject(project) {
const ctx = await esbuild.context({
bundle: true,
minify: production,
sourcemap: !production,
sourcesContent: false,
logLevel: LOG_LEVEL,
// for the third-party notices
metafile: production,
...project,
plugins: [
typecheckPlugin({ configFile: project.entryPoints[0] + "/tsconfig.json" }),
/* add to the end of plugins array */
//esbuildProblemMatcherPlugin,
],
});
const result = await ctx.rebuild();
await ctx.dispose();
return result.metafile;
}
/**
* The directory of the npm package a file of a build comes from, by the last `node_modules` in its path.
*
* @param {string} file
*/
function packageDirectory(file) {
const parts = file.split(/[\\/]/);
const index = parts.lastIndexOf("node_modules");
if (index < 0 || index + 1 >= parts.length) return undefined;
return parts.slice(0, index + (parts[index + 1].startsWith("@") ? 3 : 2)).join("/");
}
/**
* Writes `out/ThirdPartyNotices.txt`: name, version, licence and licence texts of every npm package with files in
* the bundles or among their assets.
*
* @param {import('esbuild').Metafile[]} metafiles
*/
function writeThirdPartyNotices(metafiles) {
const directories = new Set();
for (const metafile of metafiles) {
for (const file of Object.keys(metafile.inputs)) {
const directory = packageDirectory(file);
if (directory !== undefined) directories.add(directory);
}
}
const entries = [...directories]
.map((directory) => {
const manifest = JSON.parse(fs.readFileSync(path.join(directory, "package.json"), "utf8"));
const license = typeof manifest.license === "string" ? manifest.license : (manifest.license?.type ?? "");
const texts = fs
.readdirSync(directory)
.filter((name) => /^(licen[cs]e|copying|notice)([-._].*)?$/i.test(name))
.sort()
.map((name) => fs.readFileSync(path.join(directory, name), "utf8").trim());
return { name: manifest.name, version: manifest.version, license, homepage: manifest.homepage, texts };
})
.sort((a, b) => a.name.localeCompare(b.name));
const separator = "=".repeat(80);
const text = [
"Third-party notices",
"",
"The RobotCode extension for VS Code includes the following open source software. Its licences and notices follow.",
...entries.flatMap((entry) => [
"",
separator,
`${entry.name} ${entry.version}${entry.license ? ` (${entry.license})` : ""}`,
...(entry.homepage ? [entry.homepage] : []),
separator,
...entry.texts.flatMap((license) => ["", license]),
]),
"",
].join("\n");
fs.writeFileSync("out/ThirdPartyNotices.txt", text);
}
async function main() {
// Files of earlier builds, such as source maps of a development build, would be packaged too.
if (production) fs.rmSync("out", { recursive: true, force: true });
const metafiles = [];
for (const project of projects) {
metafiles.push(await buildProject(project));
}
if (production) writeThirdPartyNotices(metafiles);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});