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

Add hot-reload of the extension in Chrome #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"description": "A boilerplate to chrome extension with webpack",
"scripts": {
"build": "node utils/build.js",
"build": "NODE_ENV=production node utils/build.js",
"start": "node utils/webserver.js"
},
"devDependencies": {
Expand Down
52 changes: 52 additions & 0 deletions src/js/hot-reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const filesInDirectory = dir => new Promise (resolve =>

dir.createReader ().readEntries (entries =>

Promise.all (entries.filter (e => e.name[0] !== '.').map (e =>

e.isDirectory
? filesInDirectory (e)
: new Promise (resolve => e.file (resolve))
))
.then (files => [].concat (...files))
.then (resolve)
)
)

const timestampForFilesInDirectory = dir =>
filesInDirectory (dir).then (files =>
files.map (f => f.name + f.lastModified).join ())

const reload = () => {

chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5

if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }

chrome.runtime.reload ()
})
}

const watchChanges = (dir, lastTimestamp) => {

timestampForFilesInDirectory (dir).then (timestamp => {

if (!lastTimestamp || (lastTimestamp === timestamp)) {

setTimeout (() => watchChanges (dir, timestamp), 1000) // retry after 1s

} else {

reload ()
}
})

}

chrome.management.getSelf (self => {

if (self.installType === 'development') {

chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
}
})
5 changes: 4 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "Chrome Extension Webpack",
"options_page": "options.html",
"background": {
"page": "background.html"
"scripts": [
"background.bundle.js",
"hot_reload.bundle.js"
]
},
"browser_action": {
"default_popup": "popup.html",
Expand Down
3 changes: 2 additions & 1 deletion utils/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var server =
headers: {
"Access-Control-Allow-Origin": "*"
},
disableHostCheck: true
disableHostCheck: true,
watchContentBase: true
});

server.listen(env.PORT);
6 changes: 4 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ var options = {
entry: {
popup: path.join(__dirname, "src", "js", "popup.js"),
options: path.join(__dirname, "src", "js", "options.js"),
background: path.join(__dirname, "src", "js", "background.js")
background: path.join(__dirname, "src", "js", "background.js"),
hot_reload: path.join(__dirname, "src", "js", "hot-reload.js")
},
output: {
path: path.join(__dirname, "build"),
filename: "[name].bundle.js"
filename: "[name].bundle.js",
publicPath: "build/"
},
module: {
rules: [
Expand Down