forked from dfinity/internet-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.plugins.ts
90 lines (81 loc) · 2.45 KB
/
vite.plugins.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
import { assertNonNullish } from "@dfinity/utils";
import { readFileSync } from "fs";
import { minify } from "html-minifier-terser";
import { extname } from "path";
import { Plugin } from "vite";
import viteCompression from "vite-plugin-compression";
/**
* Read the II canister ID from dfx's local state
*/
const readCanisterId = (): string => {
const canisterIdsJsonFile = "./.dfx/local/canister_ids.json";
try {
const {
internet_identity: { local: canisterId },
} = JSON.parse(readFileSync(canisterIdsJsonFile, "utf-8"));
assertNonNullish(
canisterId,
`Could not get canister ID from ${canisterIdsJsonFile}`
);
console.log("Read canister ID:", canisterId);
return canisterId;
} catch (e) {
throw Error(`Could not get canister ID from ${canisterIdsJsonFile}: ${e}`);
}
};
/**
* Inject the II canister ID as a <script /> tag in index.html for local development.
*/
export const injectCanisterIdPlugin = (): {
name: "html-transform";
transformIndexHtml(html: string): string;
} => ({
name: "html-transform",
transformIndexHtml(html): string {
return html.replace(
`<script id="setupJs"></script>`,
`<script data-canister-id="${readCanisterId()}" id="setupJs"></script>`
);
},
});
/**
* Remove the <script /> that loads the index.js for production build.
* A script loader is injected by the backend.
*/
export const stripInjectJsScript = (): {
name: "html-transform";
transformIndexHtml(html: string): string;
} => ({
name: "html-transform",
transformIndexHtml(html): string {
const match = `<script type="module" crossorigin src="/index.js"></script>`;
if (!html.includes(match)) {
throw new Error("Expecting script tag to replace, found none");
}
return html.replace(match, ``);
},
});
/**
* GZip generated resources e.g. index.js => index.js.gz
*/
export const compression = (): Plugin =>
viteCompression({
// II canister only supports one content type per resource. That is why we remove the original file.
deleteOriginFile: true,
filter: (file: string): boolean =>
![".html", ".css", ".webp", ".png", ".ico", ".svg"].includes(
extname(file)
),
});
/**
* Minify HTML
*/
export const minifyHTML = (): {
name: "html-transform";
transformIndexHtml(html: string): Promise<string>;
} => ({
name: "html-transform",
async transformIndexHtml(html): Promise<string> {
return minify(html, { collapseWhitespace: true });
},
});