MCPcopy Index your code
hub / github.com/darkreader/darkreader / createBundleJSTask

Function createBundleJSTask

tasks/bundle-js.js:186–258  ·  view source on GitHub ↗
(jsEntries)

Source from the content-addressed store, hash-verified

184 * @returns {ReturnType<typeof createTask>}
185 */
186export function createBundleJSTask(jsEntries) {
187 /** @type {string[]} */
188 let currentWatchFiles;
189
190 const getRelevantWatchFiles = () => {
191 const watchFiles = new Set();
192 jsEntries.forEach((entry) => {
193 entry.watchFiles?.forEach((file) => watchFiles.add(file));
194 });
195 return Array.from(watchFiles);
196 };
197
198 /** @type {(options: Partial<TaskOptions> & {platforms: TaskOptions['platforms']}, entries?: JSEntry[]) => Promise<void>} */
199 const bundleEachPlatform = async ({platforms, debug, watch, log, test}, entries) => {
200 const allPlatforms = Object.values(PLATFORM).filter((platform) => platform !== PLATFORM.API);
201 for (const entry of (entries || jsEntries)) {
202 const possiblePlatforms = entry.platform ? [entry.platform] : allPlatforms;
203 const targetPlatforms = possiblePlatforms.filter((platform) => platforms[platform]);
204 for (const platform of targetPlatforms) {
205 await bundleJS(entry, platform, debug, watch, log, test);
206 }
207 }
208 };
209
210 /** @type {(changedFiles: string[], watcher: FSWatcher, platforms: any) => Promise<void>} */
211 const onChange = async (changedFiles, watcher, initialPlatforms) => {
212 /** @type {any} */
213 let platforms = {};
214 const connectedBrowsers = reload.getConnectedBrowsers();
215 if (connectedBrowsers.includes('chrome')) {
216 platforms.chrome = initialPlatforms.chrome;
217 platforms['chrome-mv3'] = initialPlatforms['chrome-mv3'];
218 platforms['chrome-plus'] = initialPlatforms['chrome-plus'];
219 }
220 if (connectedBrowsers.includes('firefox')) {
221 platforms.firefox = true;
222 }
223 if (connectedBrowsers.length === 0) {
224 platforms = initialPlatforms;
225 }
226
227 const entries = jsEntries.filter((entry) => {
228 return changedFiles.some((changed) => {
229 return entry.watchFiles?.includes(changed);
230 });
231 });
232 await bundleEachPlatform({platforms, debug: true, watch: true}, entries);
233
234 const newWatchFiles = getRelevantWatchFiles();
235 watcher.unwatch(
236 currentWatchFiles.filter((oldFile) => !newWatchFiles.includes(oldFile))
237 );
238 watcher.add(
239 newWatchFiles.filter((newFile) => currentWatchFiles.includes(newFile))
240 );
241
242 const isUIOnly = entries.every((entry) => entry.reloadType === reload.UI);
243 reload.reload({

Callers 1

bundle-js.jsFile · 0.85

Calls 3

createTaskFunction · 0.90
getRelevantWatchFilesFunction · 0.85
addWatcherMethod · 0.80

Tested by

no test coverage detected