Skip to content

Commit 147ff00

Browse files
committed
JS: Swapped livereload lib for esbuild livereload setup
1 parent 1e768ce commit 147ff00

File tree

4 files changed

+79
-77
lines changed

4 files changed

+79
-77
lines changed

dev/build/esbuild.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/usr/bin/env node
22

3-
const esbuild = require('esbuild');
4-
const path = require('path');
5-
const fs = require('fs');
3+
import * as esbuild from 'esbuild';
4+
import * as path from 'node:path';
5+
import * as fs from 'node:fs';
6+
import * as process from "node:process";
7+
68

79
// Check if we're building for production
810
// (Set via passing `production` as first argument)
9-
const isProd = process.argv[2] === 'production';
11+
const mode = process.argv[2];
12+
const isProd = mode === 'production';
13+
const __dirname = import.meta.dirname;
1014

1115
// Gather our input files
1216
const entryPoints = {
@@ -17,11 +21,16 @@ const entryPoints = {
1721
wysiwyg: path.join(__dirname, '../../resources/js/wysiwyg/index.ts'),
1822
};
1923

24+
// Watch styles so we can reload on change
25+
if (mode === 'watch') {
26+
entryPoints['styles-dummy'] = path.join(__dirname, '../../public/dist/styles.css');
27+
}
28+
2029
// Locate our output directory
2130
const outdir = path.join(__dirname, '../../public/dist');
2231

23-
// Build via esbuild
24-
esbuild.build({
32+
// Define the options for esbuild
33+
const options = {
2534
bundle: true,
2635
metafile: true,
2736
entryPoints,
@@ -33,6 +42,7 @@ esbuild.build({
3342
minify: isProd,
3443
logLevel: 'info',
3544
loader: {
45+
'.html': 'copy',
3646
'.svg': 'text',
3747
},
3848
absWorkingDir: path.join(__dirname, '../..'),
@@ -45,6 +55,28 @@ esbuild.build({
4555
js: '// See the "/licenses" URI for full package license details',
4656
css: '/* See the "/licenses" URI for full package license details */',
4757
},
48-
}).then(result => {
49-
fs.writeFileSync('esbuild-meta.json', JSON.stringify(result.metafile));
50-
}).catch(() => process.exit(1));
58+
};
59+
60+
if (mode === 'watch') {
61+
options.inject = [
62+
path.join(__dirname, './livereload.js'),
63+
];
64+
}
65+
66+
const ctx = await esbuild.context(options);
67+
68+
if (mode === 'watch') {
69+
// Watch for changes and rebuild on change
70+
ctx.watch({});
71+
let {hosts, port} = await ctx.serve({
72+
servedir: path.join(__dirname, '../../public'),
73+
cors: {
74+
origin: '*',
75+
}
76+
});
77+
} else {
78+
// Build with meta output for analysis
79+
ctx.rebuild().then(result => {
80+
fs.writeFileSync('esbuild-meta.json', JSON.stringify(result.metafile));
81+
}).catch(() => process.exit(1));
82+
}

dev/build/livereload.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
if (!window.__dev_reload_listening) {
2+
listen();
3+
window.__dev_reload_listening = true;
4+
}
5+
6+
7+
function listen() {
8+
console.log('Listening for livereload events...');
9+
new EventSource("http://127.0.0.1:8000/esbuild").addEventListener('change', e => {
10+
const { added, removed, updated } = JSON.parse(e.data);
11+
12+
if (!added.length && !removed.length && updated.length > 0) {
13+
const updatedPath = updated.filter(path => path.endsWith('.css'))[0]
14+
if (!updatedPath) return;
15+
16+
const links = [...document.querySelectorAll("link[rel='stylesheet']")];
17+
for (const link of links) {
18+
const url = new URL(link.href);
19+
const name = updatedPath.replace('-dummy', '');
20+
21+
if (url.pathname.endsWith(name)) {
22+
const next = link.cloneNode();
23+
next.href = name + '?' + Math.random().toString(36).slice(2);
24+
next.onload = function() {
25+
link.remove();
26+
};
27+
link.after(next);
28+
return
29+
}
30+
}
31+
}
32+
33+
location.reload()
34+
});
35+
}

package-lock.json

Lines changed: 0 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"private": true,
3+
"type": "module",
34
"scripts": {
45
"build:css:dev": "sass ./resources/sass:./public/dist --embed-sources",
56
"build:css:watch": "sass ./resources/sass:./public/dist --watch --embed-sources",
67
"build:css:production": "sass ./resources/sass:./public/dist -s compressed",
78
"build:js:dev": "node dev/build/esbuild.js",
8-
"build:js:watch": "chokidar --initial \"./resources/**/*.js\" \"./resources/**/*.mjs\" \"./resources/**/*.ts\" -c \"npm run build:js:dev\"",
9+
"build:js:watch": "node dev/build/esbuild.js watch",
910
"build:js:production": "node dev/build/esbuild.js production",
1011
"build": "npm-run-all --parallel build:*:dev",
1112
"production": "npm-run-all --parallel build:*:production",
12-
"dev": "npm-run-all --parallel watch livereload",
13+
"dev": "npm-run-all --parallel build:*:watch",
1314
"watch": "npm-run-all --parallel build:*:watch",
14-
"livereload": "livereload ./public/dist/",
1515
"permissions": "chown -R $USER:$USER bootstrap/cache storage public/uploads",
1616
"lint": "eslint \"resources/**/*.js\" \"resources/**/*.mjs\"",
1717
"fix": "eslint --fix \"resources/**/*.js\" \"resources/**/*.mjs\"",
@@ -29,7 +29,6 @@
2929
"eslint-plugin-import": "^2.32.0",
3030
"jest": "^30.2.0",
3131
"jest-environment-jsdom": "^30.2.0",
32-
"livereload": "^0.10.3",
3332
"npm-run-all": "^4.1.5",
3433
"sass": "^1.94.2",
3534
"ts-jest": "^29.4.5",

0 commit comments

Comments
 (0)