-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhotReload.ts
210 lines (179 loc) · 5.77 KB
/
hotReload.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
// Avoid typescript "isolatedModules" error;
export {};
type FileEntity = {
file: File;
insideBackground: boolean;
insideContent: boolean;
locale: boolean;
};
type ChecksumSnapshot = {
common: string;
background: string;
content: string;
locales: string;
manifest: string;
};
const RELOAD_TAB_FLAG = "__hr_reload_tab";
const SLOW_DOWN_AFTER = 5 * 60_000; // 5 min
let contentScripts = getStaticContentScripts();
// Wait for the fresh content script to be loaded
// via browser.scripting.registerContentScripts()
setTimeout(scheduleContentScriptsUpdate, 300);
let backgroundScripts: string[] = [];
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === "__hr_bg_scripts") {
backgroundScripts = msg.scripts;
}
});
chrome.management.getSelf(async (self) => {
if (self.installType === "development") {
chrome.runtime.getPackageDirectoryEntry(watchChanges);
// NB: see https://github.com/xpl/crx-hotreload/issues/5
const { [RELOAD_TAB_FLAG]: reloadTabURL } =
await chrome.storage.local.get(RELOAD_TAB_FLAG);
if (reloadTabURL) {
await chrome.storage.local.remove(RELOAD_TAB_FLAG);
queryTabs({ url: reloadTabURL }).then((tabs) => {
if (tabs.length > 0) {
chrome.tabs.reload(tabs[0].id!);
} else {
chrome.tabs.create({ url: reloadTabURL, active: true });
}
});
}
}
});
async function watchChanges(
dir: DirectoryEntry,
lastChecksum?: ChecksumSnapshot,
lastChangedAt = Date.now(),
) {
const entities = await findFiles(dir);
const checksum: ChecksumSnapshot = {
common: toChecksum(entities),
background: toChecksum(entities.filter((e) => e.insideBackground)),
content: toChecksum(entities.filter((e) => e.insideContent)),
locales: toChecksum(entities.filter((e) => e.locale)),
manifest: toChecksum(
entities.filter((e) => e.file.name === "manifest.json"),
),
};
if (lastChecksum && checksum.common !== lastChecksum.common) {
try {
if (
checksum.content !== lastChecksum.content ||
checksum.locales !== lastChecksum.locales ||
checksum.manifest !== lastChecksum.manifest ||
checksum.background !== lastChecksum.background
) {
const activeTab = await getActiveMainTab();
if (activeTab?.url)
await chrome.storage.local.set({ [RELOAD_TAB_FLAG]: activeTab.url });
chrome.runtime.reload();
} else {
const tabs = await queryTabs({
url: getExtensionUrlPattern(),
});
// Reload extension tabs
for (const tab of tabs) {
if (tab.url?.includes("hotreload")) continue;
chrome.tabs.reload(tab.id!);
}
// Reload popup
chrome.extension.getViews({ type: "popup" }).forEach((popupWindow) => {
popupWindow.location.reload();
});
}
} catch (err) {
console.error(err);
} finally {
lastChangedAt = Date.now();
}
}
const retryAfter =
Date.now() - lastChangedAt > SLOW_DOWN_AFTER ? 5_000 : 1_000;
setTimeout(() => watchChanges(dir, checksum, lastChangedAt), retryAfter);
}
function findFiles(dir: DirectoryEntry) {
return new Promise<FileEntity[]>((resolve) => {
dir.createReader().readEntries((entries) => {
Promise.all(
entries
.filter((entry) => entry.name[0] !== ".")
.map((entry) =>
entry.isDirectory
? findFiles(entry as DirectoryEntry)
: new Promise((res) =>
(entry as FileEntry).file((file) => {
const insideBackground = isEntryInside(
entry,
backgroundScripts,
);
const insideContent = isEntryInside(entry, contentScripts);
const locale = /.*\_locales.*\.json/.test(entry.fullPath);
res({ file, insideBackground, insideContent, locale });
}),
),
),
)
.then((entities: any[]) => [].concat(...entities))
.then(resolve);
});
});
}
function toChecksum(entities: FileEntity[]) {
return entities.map(({ file }) => `${file.name}${file.lastModified}`).join();
}
function isEntryInside(entry: Entry, paths: string[]) {
return paths.some((p) => entry.fullPath.endsWith(p));
}
function getStaticContentScripts() {
const manifest = chrome.runtime.getManifest();
const scriptSet = new Set<string>();
if (manifest.web_accessible_resources) {
for (const resource of manifest.web_accessible_resources) {
if (typeof resource === "string") {
scriptSet.add(resource);
} else {
resource.resources.forEach((r) => scriptSet.add(r));
}
}
}
if (manifest.content_scripts) {
for (const contentScript of manifest.content_scripts) {
if (contentScript.js) {
for (const s of contentScript.js) {
scriptSet.add(s);
}
}
}
}
return Array.from(scriptSet);
}
async function scheduleContentScriptsUpdate() {
const registered = await chrome.scripting
.getRegisteredContentScripts()
.catch(() => []);
const scriptSet = new Set(contentScripts);
for (const { js } of registered) {
js?.forEach((s) => scriptSet.add(s));
}
contentScripts = Array.from(scriptSet);
setTimeout(scheduleContentScriptsUpdate, 5_000);
}
async function getActiveMainTab(): Promise<chrome.tabs.Tab | undefined> {
const tabs = await queryTabs({
active: true,
lastFocusedWindow: true,
url: getExtensionUrlPattern("main.html"),
});
return tabs[0];
}
function queryTabs(params: chrome.tabs.QueryInfo) {
return new Promise<chrome.tabs.Tab[]>((res) =>
chrome.tabs.query(params, res),
);
}
function getExtensionUrlPattern(path = "**") {
return `chrome-extension://${chrome.runtime.id}/${path}`;
}