-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathvite.config.ts
356 lines (315 loc) · 13.8 KB
/
vite.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* eslint-disable no-console */
import { spawnSync } from "child_process";
import fs from "fs";
import path from "path";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import rollupPluginLicense, { type Dependency } from "rollup-plugin-license";
import { sveltePreprocess } from "svelte-preprocess";
import { defineConfig } from "vite";
import { DynamicPublicDirectory as viteMultipleAssets } from "vite-multiple-assets";
const projectRootDir = path.resolve(__dirname);
// Keep this list in sync with those in `/about.toml` and `/deny.toml`.
const ALLOWED_LICENSES = [
"Apache-2.0 WITH LLVM-exception",
"Apache-2.0",
"BSD-2-Clause",
"BSD-3-Clause",
"BSL-1.0",
"CC0-1.0",
"ISC",
"MIT-0",
"MIT",
"MPL-2.0",
"OpenSSL",
"Unicode-3.0",
"Unicode-DFS-2016",
"Zlib",
];
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
svelte({
preprocess: [sveltePreprocess()],
onwarn(warning, defaultHandler) {
// NOTICE: Keep this list in sync with the list in `.vscode/settings.json`
const suppressed = ["css-unused-selector", "vite-plugin-svelte-css-no-scopable-elements", "a11y-no-static-element-interactions", "a11y-no-noninteractive-element-interactions"];
if (suppressed.includes(warning.code)) return;
defaultHandler?.(warning);
},
}),
viteMultipleAssets(["../demo-artwork"]),
],
resolve: {
alias: [
{ find: /@graphite-frontend\/(.*\.svg)/, replacement: path.resolve(projectRootDir, "$1?raw") },
{ find: "@graphite-frontend", replacement: projectRootDir },
{ find: "@graphite/../assets", replacement: path.resolve(projectRootDir, "assets") },
{ find: "@graphite/../public", replacement: path.resolve(projectRootDir, "public") },
{ find: "@graphite", replacement: path.resolve(projectRootDir, "src") },
],
},
server: {
port: 8080,
host: "0.0.0.0",
},
build: {
rollupOptions: {
plugins: [
rollupPluginLicense({
thirdParty: {
allow: {
test: `(${ALLOWED_LICENSES.join(" OR ")})`,
failOnUnlicensed: true,
failOnViolation: true,
},
output: {
file: path.resolve(__dirname, "./dist/third-party-licenses.txt"),
template: formatThirdPartyLicenses,
},
},
}),
],
output: {
// Inject `.min` into the filename of minified CSS files to tell Cloudflare not to minify it again.
// Cloudflare's minifier breaks the CSS due to a bug where it removes whitespace around calc() plus operators.
assetFileNames: (info) => `assets/[name]-[hash]${info.name?.endsWith(".css") ? ".min" : ""}[extname]`,
},
},
},
});
type LicenseInfo = {
licenseName: string;
licenseText: string;
noticeText?: string;
packages: PackageInfo[];
};
type PackageInfo = {
name: string;
version: string;
author: string;
repository: string;
};
function formatThirdPartyLicenses(jsLicenses: Dependency[]): string {
// Generate the Rust license information.
let licenses = generateRustLicenses() || [];
// Ensure we have license information to work with before proceeding.
if (licenses.length === 0) {
// This is probably caused by `cargo about` not being installed.
console.error("Could not run `cargo about`, which is required to generate license information.");
console.error("To install cargo-about on your system, you can run `cargo install cargo-about`.");
console.error("License information is required in production builds. Aborting.");
process.exit(1);
}
if (jsLicenses.length === 0) {
console.error("No JavaScript package licenses were found by `rollup-plugin-license`. Please investigate.");
console.error("License information is required in production builds. Aborting.");
process.exit(1);
}
// Find then duplicate this license if one of its packages is `path-bool`, adding its notice text.
let foundLicensesIndex;
let foundPackagesIndex;
licenses.forEach((license, licenseIndex) => {
license.packages.forEach((pkg, pkgIndex) => {
if (pkg.name === "path-bool") {
foundLicensesIndex = licenseIndex;
foundPackagesIndex = pkgIndex;
}
});
});
if (foundLicensesIndex !== undefined && foundPackagesIndex !== undefined) {
const license = licenses[foundLicensesIndex];
const pkg = license.packages[foundPackagesIndex];
license.packages = license.packages.filter((pkg) => pkg.name !== "path-bool");
const noticeText = fs.readFileSync(path.resolve(__dirname, "../libraries/path-bool/NOTICE"), "utf8");
licenses.push({
licenseName: license.licenseName,
licenseText: license.licenseText,
noticeText,
packages: [pkg],
});
}
// Augment the imported Rust license list with the provided JS license list.
jsLicenses.forEach((jsLicense) => {
const name = jsLicense.name || "";
const version = jsLicense.version || "";
const author = jsLicense.author?.text() || "";
const licenseName = jsLicense.license || "";
const licenseText = trimBlankLines(jsLicense.licenseText || "");
const noticeText = trimBlankLines(jsLicense.noticeText || "");
let repository = jsLicense.repository || "";
if (repository && typeof repository === "object") repository = repository.url;
// Remove the `git+` or `git://` prefix and `.git` suffix.
const repo = repository ? repository.replace(/^.*(github.com\/.*?\/.*?)(?:.git)/, "https://$1") : repository;
const matchedLicense = licenses.find(
(license) => license.licenseName === licenseName && trimBlankLines(license.licenseText || "") === licenseText && trimBlankLines(license.noticeText || "") === noticeText,
);
const pkg: PackageInfo = { name, version, author, repository: repo };
if (matchedLicense) matchedLicense.packages.push(pkg);
else licenses.push({ licenseName, licenseText, noticeText, packages: [pkg] });
});
// Combine any license notices into the license text.
licenses.forEach((license, index) => {
if (license.noticeText) {
licenses[index].licenseText += "\n\n";
licenses[index].licenseText += " _______________________________________\n";
licenses[index].licenseText += "│ │\n";
licenses[index].licenseText += "│ THE FOLLOWING NOTICE FILE IS INCLUDED │\n";
licenses[index].licenseText += "│ │\n";
licenses[index].licenseText += " ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\n\n";
licenses[index].licenseText += `${license.noticeText}\n`;
licenses[index].noticeText = undefined;
}
});
// De-duplicate any licenses with the same text by merging their lists of packages.
const licensesNormalizedWhitespace = licenses.map((license) => license.licenseText.replace(/[\n\s]+/g, " ").trim());
licenses.forEach((currentLicense, currentLicenseIndex) => {
licenses.slice(0, currentLicenseIndex).forEach((comparisonLicense, comparisonLicenseIndex) => {
if (licensesNormalizedWhitespace[currentLicenseIndex] === licensesNormalizedWhitespace[comparisonLicenseIndex]) {
currentLicense.packages.push(...comparisonLicense.packages);
comparisonLicense.packages = [];
// After emptying the packages, the redundant license with no packages will be removed in the next step's `filter()`.
}
});
});
// Filter out first-party internal Graphite crates.
licenses = licenses.filter((license) => {
license.packages = license.packages.filter(
(packageInfo) =>
!(packageInfo.repository && packageInfo.repository.toLowerCase().includes("github.com/GraphiteEditor/Graphite".toLowerCase())) &&
!(
packageInfo.author &&
packageInfo.author.toLowerCase().includes("contact@graphite.rs") &&
// Exclude a comma which indicates multiple authors, which we need to not filter out
!packageInfo.author.toLowerCase().includes(",")
),
);
return license.packages.length > 0;
});
// Sort the licenses by the number of packages using the same license, and then alphabetically by license name.
licenses.sort((a, b) => a.licenseText.localeCompare(b.licenseText));
licenses.sort((a, b) => a.licenseName.localeCompare(b.licenseName));
licenses.sort((a, b) => b.packages.length - a.packages.length);
// Sort the individual packages using each license alphabetically.
licenses.forEach((license) => {
license.packages.sort((a, b) => a.name.localeCompare(b.name));
});
// Prepare a header for the license notice.
let formattedLicenseNotice = "";
formattedLicenseNotice += "▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐\n";
formattedLicenseNotice += "▐▐ ▐▐\n";
formattedLicenseNotice += "▐▐ GRAPHITE THIRD-PARTY SOFTWARE LICENSE NOTICES ▐▐\n";
formattedLicenseNotice += "▐▐ ▐▐\n";
formattedLicenseNotice += "▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐\n";
// Append a block for each license shared by multiple packages with identical license text.
licenses.forEach((license) => {
let packagesWithSameLicense = license.packages.map((packageInfo) => {
const { name, version, author, repository } = packageInfo;
return `${name} ${version}${author ? ` - ${author}` : ""}${repository ? ` - ${repository}` : ""}`;
});
const multi = packagesWithSameLicense.length !== 1;
const saysLicense = license.licenseName.toLowerCase().includes("license");
const header = `The package${multi ? "s" : ""} listed here ${multi ? "are" : "is"} licensed under the terms of the ${license.licenseName}${saysLicense ? "" : " license"} printed beneath`;
const packagesLineLength = Math.max(header.length, ...packagesWithSameLicense.map((line) => line.length));
packagesWithSameLicense = packagesWithSameLicense.map((line) => `│ ${line}${" ".repeat(packagesLineLength - line.length)} │`);
formattedLicenseNotice += "\n";
formattedLicenseNotice += ` ${"_".repeat(packagesLineLength + 2)}\n`;
formattedLicenseNotice += `│ ${" ".repeat(packagesLineLength)} │\n`;
formattedLicenseNotice += `│ ${header}${" ".repeat(packagesLineLength - header.length)} │\n`;
formattedLicenseNotice += `│${"_".repeat(packagesLineLength + 2)}│\n`;
formattedLicenseNotice += `${packagesWithSameLicense.join("\n")}\n`;
formattedLicenseNotice += ` ${"‾".repeat(packagesLineLength + 2)}\n`;
formattedLicenseNotice += `${license.licenseText}\n`;
});
return formattedLicenseNotice;
}
function generateRustLicenses(): LicenseInfo[] | undefined {
// Log the starting status to the build output.
console.info("\n\nGenerating license information for Rust code\n");
try {
// Call `cargo about` in the terminal to generate the license information for Rust crates.
// The `about.hbs` file is written so it generates a valid JavaScript array expression which we evaluate below.
const { stdout, stderr, status } = spawnSync("cargo", ["about", "generate", "about.hbs"], {
cwd: path.join(__dirname, ".."),
encoding: "utf8",
timeout: 60000, // One minute
shell: true,
windowsHide: true, // Hide the terminal on Windows
});
// If the command failed, print the error message and exit early.
if (status !== 0) {
// Cargo returns 101 when the subcommand (`about`) wasn't found, so we skip printing the below error message in that case.
if (status !== 101) {
console.error("cargo-about failed", status, stderr);
}
return undefined;
}
// Make sure the output starts with this expected label, which lets us know the file generated with expected output.
// We don't want to eval an error message or something else, so we fail early if that happens.
if (!stdout.trim().startsWith("GENERATED_BY_CARGO_ABOUT:")) {
console.error("Unexpected output from cargo-about", stdout);
return undefined;
}
// Convert the array JS syntax string into an actual JS array in memory.
// Security-wise, eval() isn't any worse than require(), but it's able to work without a temporary file.
// We call eval indirectly to avoid a warning as explained here: <https://esbuild.github.io/content-types/#direct-eval>.
const indirectEval = eval;
const licensesArray = indirectEval(stdout) as LicenseInfo[];
// Remove the HTML character encoding caused by Handlebars.
const rustLicenses = (licensesArray || []).map(
(rustLicense): LicenseInfo => ({
licenseName: htmlDecode(rustLicense.licenseName),
licenseText: trimBlankLines(htmlDecode(rustLicense.licenseText)),
packages: rustLicense.packages.map(
(packageInfo): PackageInfo => ({
name: htmlDecode(packageInfo.name),
version: htmlDecode(packageInfo.version),
author: htmlDecode(packageInfo.author)
.replace(/\[(.*), \]/, "$1")
.replace("[]", ""),
repository: htmlDecode(packageInfo.repository),
}),
),
}),
);
return rustLicenses;
} catch (_) {
return undefined;
}
}
function htmlDecode(input: string): string {
if (!input) return input;
const htmlEntities = {
nbsp: " ",
copy: "©",
reg: "®",
lt: "<",
gt: ">",
amp: "&",
apos: "'",
quot: `"`,
};
return input.replace(/&([^;]+);/g, (entity: string, entityCode: string) => {
const maybeEntity = Object.entries(htmlEntities).find(([key, _]) => key === entityCode);
if (maybeEntity) return maybeEntity[1];
let match;
// eslint-disable-next-line no-cond-assign
if ((match = entityCode.match(/^#x([\da-fA-F]+)$/))) {
return String.fromCharCode(parseInt(match[1], 16));
}
// eslint-disable-next-line no-cond-assign
if ((match = entityCode.match(/^#(\d+)$/))) {
return String.fromCharCode(~~match[1]);
}
return entity;
});
}
function trimBlankLines(input: string): string {
let result = input.replace(/\r/g, "");
while (result.charAt(0) === "\r" || result.charAt(0) === "\n") {
result = result.slice(1);
}
while (result.slice(-1) === "\r" || result.slice(-1) === "\n") {
result = result.slice(0, -1);
}
return result;
}