-
Notifications
You must be signed in to change notification settings - Fork 32
/
devBuilder.ts
177 lines (137 loc) · 4.8 KB
/
devBuilder.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
import { copy, emptyDir, ensureDir, readFile, writeFile } from "fs-extra";
import path from "path";
import { ResolvedConfig, ViteDevServer, normalizePath } from "vite";
import { getContentScriptLoaderFile } from "../utils/loader";
import { getInputFileName, getOutputFileName } from "../utils/file";
import { getVirtualModule } from "../utils/virtualModule";
export default abstract class DevBuilder<
Manifest extends chrome.runtime.Manifest
> {
protected hmrServerOrigin = "";
protected inlineScriptHashes = new Set<string>();
protected outDir: string;
constructor(
private viteConfig: ResolvedConfig,
private viteDevServer?: ViteDevServer
) {
this.outDir = this.viteConfig.build.outDir;
}
async writeBuild({
devServerPort,
manifest,
manifestHtmlFiles,
}: {
devServerPort: number;
manifest: Manifest;
manifestHtmlFiles: string[];
}) {
this.hmrServerOrigin = this.getHmrServerOrigin(devServerPort);
await emptyDir(this.outDir);
copy("public", this.outDir);
await this.writeManifestHtmlFiles(manifestHtmlFiles);
await this.writeManifestContentScriptFiles(manifest);
await this.writeBuildFiles(manifest, manifestHtmlFiles);
this.updateContentSecurityPolicyForHmr(manifest);
await writeFile(
`${this.outDir}/manifest.json`,
JSON.stringify(manifest, null, 2)
);
}
protected abstract updateContentSecurityPolicyForHmr(
manifest: Manifest
): Manifest;
protected async writeBuildFiles(
_manifest: Manifest,
_manifestHtmlFiles: string[]
): Promise<void> {}
protected getContentSecurityPolicyWithHmrSupport(
contentSecurityPolicy: string | undefined
): string {
const scriptSrcs = [
"'self'",
[...this.inlineScriptHashes].join(" "),
this.hmrServerOrigin,
];
const cspHmrScriptSrc = `script-src ${scriptSrcs.join(
" "
)}; object-src 'self'`;
if (!contentSecurityPolicy) {
return cspHmrScriptSrc;
}
if (contentSecurityPolicy.includes("script-src")) {
return contentSecurityPolicy.replace(`script-src`, cspHmrScriptSrc);
}
return (contentSecurityPolicy += `; ${cspHmrScriptSrc}`);
}
protected async writeManifestHtmlFiles(htmlFileNames: string[]) {
for (const fileName of htmlFileNames) {
const absoluteFileName = getInputFileName(fileName, this.viteConfig.root);
await this.writeManifestHtmlFile(fileName, absoluteFileName);
this.viteDevServer!.watcher.on("change", async (path) => {
if (normalizePath(path) !== absoluteFileName) {
return;
}
await this.writeManifestHtmlFile(fileName, absoluteFileName);
});
}
}
private async writeManifestHtmlFile(
fileName: string,
absoluteFileName: string
): Promise<void> {
let content =
getVirtualModule(absoluteFileName) ??
(await readFile(absoluteFileName, {
encoding: "utf-8",
}));
// apply plugin transforms
content = await this.viteDevServer!.transformIndexHtml(fileName, content);
// update root paths
content = content.replace(/src="\//g, `src="${this.hmrServerOrigin}/`);
content = content.replace(/from "\//g, `from "${this.hmrServerOrigin}/`);
// update relative paths
const inputFileDir = path.dirname(fileName);
content = content.replace(
/src="\.\//g,
`src="${this.hmrServerOrigin}/${inputFileDir ? `${inputFileDir}/` : ""}`
);
this.parseInlineScriptHashes(content);
const outFile = `${this.outDir}/${fileName}`;
const outFileDir = path.dirname(outFile);
await ensureDir(outFileDir);
await writeFile(outFile, content);
}
protected parseInlineScriptHashes(_content: string): void {}
protected async writeManifestContentScriptFiles(manifest: Manifest) {
if (!manifest.content_scripts) {
return;
}
for (const [
contentScriptIndex,
script,
] of manifest.content_scripts.entries()) {
if (!script.js) {
continue;
}
for (const [scriptJsIndex, fileName] of script.js.entries()) {
const outputFileName = getOutputFileName(fileName);
const scriptLoaderFile = getContentScriptLoaderFile(
outputFileName,
`${this.hmrServerOrigin}/${fileName}`
);
manifest.content_scripts[contentScriptIndex].js![scriptJsIndex] =
scriptLoaderFile.fileName;
const outFile = `${this.outDir}/${scriptLoaderFile.fileName}`;
const outFileDir = path.dirname(outFile);
await ensureDir(outFileDir);
await writeFile(outFile, scriptLoaderFile.source);
}
}
}
private getHmrServerOrigin(devServerPort: number): string {
if (typeof this.viteConfig.server.hmr! === "boolean") {
throw new Error("Vite HMR is misconfigured");
}
return `http://${this.viteConfig.server.hmr!.host}:${devServerPort}`;
}
}