Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: background HMR on dev #16

Merged
merged 4 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"dev": "cross-env NODE_ENV=development run-p dev:*",
"dev:prepare": "esno scripts/prepare.ts",
"dev:web": "vite",
"dev:js": "npm run build:js -- --watch src",
"dev:js": "npm run build:js --watch src",
"build": "cross-env NODE_ENV=production run-s clear build:web build:prepare build:js",
"build:prepare": "esno scripts/prepare.ts",
"build:web": "vite build",
Expand Down
1 change: 1 addition & 0 deletions scripts/prepare.ts
Expand Up @@ -11,6 +11,7 @@ async function stubIndexHtml() {
const views = [
'options',
'popup',
'background',
]

for (const view of views) {
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.ts
Expand Up @@ -22,7 +22,7 @@ export async function getManifest(): Promise<Manifest.WebExtensionManifest> {
chrome_style: false,
},
background: {
scripts: ['./dist/background/index.global.js'],
page: './dist/background/index.html',
persistent: false,
},
content_scripts: [
Expand Down
4 changes: 2 additions & 2 deletions tsup.config.ts
Expand Up @@ -3,8 +3,8 @@ import type { Options } from 'tsup'
const isDev = process.env.NODE_ENV !== 'production'

export const tsup: Options = {
entryPoints: ['src/background', 'src/content'],
outDir: 'extension/dist',
entryPoints: ['src/content'],
outDir: 'extension/dist/content',
format: ['iife'],
splitting: false,
sourcemap: isDev ? 'inline' : false,
Expand Down
10 changes: 10 additions & 0 deletions views/background/index.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Background</title>
</head>
<body>
<script type="module" src="./main.ts"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions src/background/index.ts → views/background/main.ts
@@ -1,6 +1,10 @@
import { sendMessage, onMessage } from 'webext-bridge'
import browser, { Tabs } from 'webextension-polyfill'

if (import.meta.hot)
// @ts-expect-error for background HMR on dev
import('/@vite/client')

browser.runtime.onInstalled.addListener((): void => {
// eslint-disable-next-line no-console
console.log('Extension installed')
Expand Down
3 changes: 2 additions & 1 deletion vite.config.ts
Expand Up @@ -35,8 +35,9 @@ export default defineConfig(({ command }) => {
},
rollupOptions: {
input: {
popup: r('views/popup/index.html'),
background: r('views/background/index.html'),
options: r('views/options/index.html'),
popup: r('views/popup/index.html'),
},
},
},
Expand Down