Skip to content

Commit

Permalink
chore(scripts/dev): reload page on content script change
Browse files Browse the repository at this point in the history
Avoids need to manually reload every time there's change in background
or content scripts.
  • Loading branch information
sidvishnoi committed Sep 27, 2024
1 parent d274559 commit d0d7022
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions esbuild/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ function liveReloadPlugin({ target }: { target: Target }): ESBuildPlugin {
}
);`;

const reloadScriptContent = `
new EventSource("http://localhost:${port}/esbuild").addEventListener(
"change",
(ev) => {
const patterns = ["background.js", "content.js", "polyfill.js"];
const data = JSON.parse(ev.data);
if (data.updated.some((s) => patterns.some(e => s.includes(e)))) {
globalThis.location.reload();
}
},
);`;

return {
name: 'live-reload',
setup(build) {
Expand All @@ -83,6 +95,13 @@ function liveReloadPlugin({ target }: { target: Target }): ESBuildPlugin {
loader: 'tsx' as const,
};
});
build.onLoad({ filter: /src\/content\// }, async (args) => {
const contents = await readFile(args.path, 'utf8');
return {
contents: contents + '\n\n\n' + reloadScriptContent,
loader: 'tsx' as const,
};
});
},
};
}

0 comments on commit d0d7022

Please sign in to comment.