Skip to content

Commit

Permalink
Merge branch 'CreativeTechGuy-bs-auto-sync' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Jan 19, 2022
2 parents a6a8da5 + 5995258 commit b9bf888
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"lint-staged": "^12.1.5",
"lunr": "^2.3.9",
"npm-check-updates": "^12.1.0",
"open": "^8.4.0",
"pascal-case": "^3.1.2",
"plop": "^3.0.5",
"prettier": "^2.5.1",
Expand Down
39 changes: 33 additions & 6 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import esbuild from 'esbuild';
import getPort, { portNumbers } from 'get-port';
import { globby } from 'globby';
import { execSync } from 'child_process';

const bs = browserSync.create();
import open from 'open';

const { bundle, copydir, dir, serve, types } = commandLineArgs([
{ name: 'bundle', type: Boolean },
Expand Down Expand Up @@ -92,17 +91,16 @@ fs.mkdirSync(outdir, { recursive: true });

// Dev server
if (serve) {
const bs = browserSync.create();
const port = await getPort({
port: portNumbers(4000, 4999)
});

// Make sure docs/dist is empty since we're serving it virtually
del.sync('docs/dist');

console.log(chalk.cyan(`Launching the Shoelace dev server at http://localhost:${port}! 🥾\n`));

// Launch browser sync
bs.init({
const browserSyncConfig = {
open: false,
startPath: '/',
port,
logLevel: 'silent',
Expand All @@ -116,7 +114,36 @@ fs.mkdirSync(outdir, { recursive: true });
routes: {
'/dist': './dist'
}
},
socket: {
socketIoClientConfig: {
// Configure socketIO to retry forever when disconnected to enable the auto-reattach timeout below to work
reconnectionAttempts: Infinity,
reconnectionDelay: 500,
reconnectionDelayMax: 500,
timeout: 1000
}
}
};

// Launch browser sync
bs.init(browserSyncConfig, () => {
// This init callback gets executed after the server has started
const socketIoConfig = browserSyncConfig.socket.socketIoClientConfig;

// Wait enough time for any open, detached clients to have a chance to reconnect. This will be used to determine
// if we reload an existing tab or open a new one.
const tabReattachDelay = socketIoConfig.reconnectionDelayMax * 2 + socketIoConfig.timeout;

setTimeout(() => {
const url = `http://localhost:${port}`;
console.log(chalk.cyan(`Launched the Shoelace dev server at ${url} 🥾\n`));
if (Object.keys(bs.sockets.sockets).length === 0) {
open(url);
} else {
bs.reload();
}
}, tabReattachDelay);
});

// Rebuild and reload when source files change
Expand Down

0 comments on commit b9bf888

Please sign in to comment.