Skip to content

Commit

Permalink
Introduce simple auto-reload mechanism for the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdvdmt committed Jul 14, 2024
1 parent fdec7c8 commit 42e445d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ let tabIdToBeActivated: undefined | number
let testHelper: undefined | BackgroundTestHelper
if (E2E) {
testHelper = new BackgroundTestHelper()
testHelper.initContentScript()
testHelper.registerListeners()
}

if (DEVELOPMENT) {
chrome.tabs.onCreated.addListener(async (tab) => {
const url = tab.url || tab.pendingUrl
const isReloadUrl = url === 'https://popuptabswitcher/reload'
if (isReloadUrl) {
log(`[Reload extension]`)
chrome.tabs.remove(tab.id!)
chrome.runtime.reload()
}
})
}

initListeners()

function initListeners() {
testHelper?.initContentScript()
testHelper?.registerListeners()
/** NOTE:
* The order of events on tab creation in a new window:
* 1. Tab created
Expand Down
19 changes: 10 additions & 9 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const deepmerge = require('deepmerge')
const {exec} = require('child_process')

const buildProdDir = path.join(__dirname, 'build-prod')
const buildDevDir = path.join(__dirname, 'build-dev')
Expand Down Expand Up @@ -114,7 +115,7 @@ module.exports = (env) => {
const e2eProps = {
key: developmentProps.key,
}
return JSON.stringify(
const updatedManifest = JSON.stringify(
deepmerge.all([
original,
env.development ? developmentProps : {},
Expand All @@ -123,6 +124,7 @@ module.exports = (env) => {
null,
2
)
return updatedManifest
},
},
},
Expand Down Expand Up @@ -160,14 +162,13 @@ module.exports = (env) => {
PRODUCTION: 'false',
DEVELOPMENT: 'true',
}),
// env.WEBPACK_WATCH
// ? new ChromeExtensionReloader({
// entries: {
// contentScript: 'content',
// background: 'background',
// },
// })
// : () => {},
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('ReloadExtensionPlugin', () => {
exec('open "https://popuptabswitcher/reload"')
})
},
},
]
} else if (env.e2e) {
conf.mode = 'production'
Expand Down

0 comments on commit 42e445d

Please sign in to comment.