-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from SmaatoUI/BT-104-styles-hmr
[BT-104] HMR styles support
- Loading branch information
Showing
11 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ dist | |
.sass-cache | ||
reports | ||
*.log | ||
src/cssWebsocket/client.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
import ReconnectingWebSocket from 'reconnectingwebsocket'; | ||
|
||
// These variables will be supplied from gulp task config on the build step | ||
const cssHref = '#{cssHref}'; | ||
const port = '#{port}'; | ||
|
||
const host = window.document.location.host.replace(/:.*/, ''); | ||
const ws = new ReconnectingWebSocket('ws://' + host + ':' + port); | ||
|
||
ws.onmessage = () => { | ||
document.querySelector("link[href^='" + cssHref + "']") | ||
.setAttribute('href', cssHref + '?v=' + new Date().getTime()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
const fs = require('fs'); | ||
|
||
module.exports = (cssReloadPort, cssReloadPath) => { | ||
// Read the template for a client JS file | ||
const lines = fs.readFileSync(__dirname + '/client.txt') | ||
.toString() | ||
.split('\n'); | ||
|
||
fs.open(__dirname + '/client.js', 'w', (err, fd) => { | ||
lines.forEach(line => { | ||
// Gulp task variables are supplied to a client JS file | ||
const lineReplaced = line.toString() | ||
.replace('#{cssHref}', cssReloadPath) | ||
.replace('#{port}', cssReloadPort) | ||
.concat('\n'); | ||
|
||
fs.writeSync(fd, lineReplaced); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
const WebSocketServer = require('ws').Server; | ||
|
||
const server = { | ||
|
||
instance: null, | ||
|
||
start: (port) => { | ||
if (server.instance) return; | ||
|
||
server.instance = new WebSocketServer({ | ||
port, | ||
}); | ||
}, | ||
|
||
stop: () => { | ||
if (!server.instance) return; | ||
|
||
server.instance.close(); | ||
}, | ||
|
||
sendUpdate: () => { | ||
if (!server.instance || !server.instance.clients) return; | ||
|
||
// In case site is opened in a number of browsers/tabs/windows | ||
// there will be many clients connected. | ||
// Send an update to all of them. | ||
server.instance.clients.forEach( | ||
client => client.send() | ||
); | ||
}, | ||
|
||
}; | ||
|
||
module.exports = server; |