mirror of
https://github.com/enso-org/enso.git
synced 2024-11-24 00:27:16 +03:00
Remove unnecessary watcher in dev server (#3896)
This commit is contained in:
parent
5a0aad16eb
commit
ae9380856f
@ -110,11 +110,6 @@ const config: esbuild.BuildOptions = {
|
|||||||
publicPath: '/assets',
|
publicPath: '/assets',
|
||||||
incremental: true,
|
incremental: true,
|
||||||
color: true,
|
color: true,
|
||||||
watch: {
|
|
||||||
onRebuild(error, result) {
|
|
||||||
if (error) console.error('watch build failed:', error)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
logOverride: {
|
logOverride: {
|
||||||
// Happens in ScalaJS-generated parser (scala-parser.js):
|
// Happens in ScalaJS-generated parser (scala-parser.js):
|
||||||
// 6 │ "fileLevelThis": this
|
// 6 │ "fileLevelThis": this
|
||||||
@ -134,8 +129,17 @@ const config: esbuild.BuildOptions = {
|
|||||||
/**
|
/**
|
||||||
* Spawn the esbuild watch process. It continuously runs, rebuilding the package.
|
* Spawn the esbuild watch process. It continuously runs, rebuilding the package.
|
||||||
*/
|
*/
|
||||||
export async function watch() {
|
export async function watch(onRebuild?: () => void, inject?: esbuild.BuildOptions['inject']) {
|
||||||
return esbuild.build(config)
|
return esbuild.build({
|
||||||
|
...config,
|
||||||
|
inject: [...(config.inject ?? []), ...(inject ?? [])],
|
||||||
|
watch: {
|
||||||
|
onRebuild(error, result) {
|
||||||
|
if (error) console.error('watch build failed:', error)
|
||||||
|
else onRebuild?.()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,15 +6,12 @@
|
|||||||
<!-- FIXME https://github.com/validator/validator/issues/917 -->
|
<!-- FIXME https://github.com/validator/validator/issues/917 -->
|
||||||
<!-- FIXME Security Vulnerabilities: https://github.com/enso-org/ide/issues/226 -->
|
<!-- FIXME Security Vulnerabilities: https://github.com/enso-org/ide/issues/226 -->
|
||||||
<!-- NOTE `frame-src` section of `http-equiv` required only for authorization -->
|
<!-- NOTE `frame-src` section of `http-equiv` required only for authorization -->
|
||||||
<!-- The sha-256 hash for `script-src` below is for the code injected by the live-server.
|
|
||||||
In case of bumping the server, it might need to be updated.
|
|
||||||
Fortunately, the error message gives an updated hash. -->
|
|
||||||
<meta
|
<meta
|
||||||
http-equiv="Content-Security-Policy"
|
http-equiv="Content-Security-Policy"
|
||||||
content="
|
content="
|
||||||
default-src 'self';
|
default-src 'self';
|
||||||
frame-src 'self' data: https://accounts.google.com https://enso-org.firebaseapp.com;
|
frame-src 'self' data: https://accounts.google.com https://enso-org.firebaseapp.com;
|
||||||
script-src 'self' 'unsafe-eval' data: https://* 'sha256-iN7wpJdxHlpujRppkOA8N0+Mzp0ZqZr3lCtxM00Y63c=';
|
script-src 'self' 'unsafe-eval' data: https://*;
|
||||||
style-src 'self' 'unsafe-inline' data: https://*;
|
style-src 'self' 'unsafe-inline' data: https://*;
|
||||||
connect-src 'self' data: ws://localhost:* ws://127.0.0.1:* http://localhost:* https://*;
|
connect-src 'self' data: ws://localhost:* ws://127.0.0.1:* http://localhost:* https://*;
|
||||||
worker-src 'self' blob:;
|
worker-src 'self' blob:;
|
||||||
|
@ -2,5 +2,9 @@ import bundler from './esbuild-config.js'
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import * as server from 'enso-gui-server'
|
import * as server from 'enso-gui-server'
|
||||||
|
|
||||||
await bundler.watch()
|
await bundler.watch(() => liveServer?.reload(), [server.LIVE_RELOAD_LISTENER_PATH])
|
||||||
await server.start({ root: bundler.output_path, assets: bundler.output_path })
|
|
||||||
|
const liveServer = await server.start({
|
||||||
|
root: bundler.output_path,
|
||||||
|
assets: bundler.output_path,
|
||||||
|
})
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
"url": "https://github.com/enso-org/enso/issues"
|
"url": "https://github.com/enso-org/enso/issues"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"live-server": "^1.2.2",
|
"connect": "^3.7.0",
|
||||||
"portfinder": "^1.0.28"
|
"morgan": "^1.10.0",
|
||||||
|
"portfinder": "^1.0.28",
|
||||||
|
"serve-static": "^1.15.0",
|
||||||
|
"ws": "^8.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,37 @@
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import liveServer from 'live-server'
|
import url from 'node:url'
|
||||||
import * as portfinder from 'portfinder'
|
import portfinder from 'portfinder'
|
||||||
|
import connect from 'connect'
|
||||||
|
import { WebSocketServer } from 'ws'
|
||||||
|
import serveStatic from 'serve-static'
|
||||||
|
import logger from 'morgan'
|
||||||
|
|
||||||
export const DEFAULT_PORT = 8080
|
export const DEFAULT_PORT = 8080
|
||||||
|
|
||||||
async function findPort(startPort = DEFAULT_PORT) {
|
let dirname = path.dirname(url.fileURLToPath(import.meta.url))
|
||||||
return portfinder.getPortPromise({ startPort, port: startPort })
|
// Path of a file that needs to be injected into the bundle for live-reload to work.
|
||||||
}
|
export const LIVE_RELOAD_LISTENER_PATH = path.join(dirname, 'live-reload.js')
|
||||||
|
|
||||||
export async function start({ root, assets, port }) {
|
export async function start({ root, assets, port }) {
|
||||||
assets = assets ?? path.join(root, 'assets')
|
assets = assets ?? path.join(root, 'assets')
|
||||||
const parameters = {
|
|
||||||
cors: true,
|
const freePort = await portfinder.getPortPromise({ port: port ?? DEFAULT_PORT })
|
||||||
open: false, // When false, it won't load your browser by default.
|
|
||||||
// When set, serve this file (server root relative) for every 404 (useful for single-page
|
const app = connect()
|
||||||
// applications).
|
.use(logger('dev', { skip: (req, res) => res.statusCode < 400 }))
|
||||||
file: '/assets/index.html',
|
.use(serveStatic(root))
|
||||||
wait: 0, // Waits for all changes, before reloading. Defaults to 0 sec.
|
.use('/assets', serveStatic(assets))
|
||||||
logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
|
|
||||||
port: await findPort(port ?? DEFAULT_PORT),
|
const server = app.listen(freePort)
|
||||||
root: root ?? '.',
|
const wsServer = new WebSocketServer({ server, clientTracking: true, path: '/live-reload' })
|
||||||
assets,
|
|
||||||
mount: [['/assets', assets]], // Mount a directory to a route.
|
var serverUrl = `http://localhost:${freePort}`
|
||||||
|
console.log('Serving %s', serverUrl)
|
||||||
|
|
||||||
|
return {
|
||||||
|
port: freePort,
|
||||||
|
reload() {
|
||||||
|
wsServer.clients.forEach(sock => sock.send('reload'))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
console.log(`Server configuration:`, parameters)
|
|
||||||
const server = liveServer.start(parameters)
|
|
||||||
console.log(`Server started.`)
|
|
||||||
return parameters.port
|
|
||||||
}
|
}
|
||||||
|
9
app/ide-desktop/lib/server/src/live-reload.js
Normal file
9
app/ide-desktop/lib/server/src/live-reload.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// This file is injected into every entry point, but it needs to run only once.
|
||||||
|
// A global variable is used to ensure that.
|
||||||
|
if (!window.live_reload_listening) {
|
||||||
|
window.live_reload_listening = true
|
||||||
|
const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'
|
||||||
|
const address = protocol + window.location.host + '/live-reload'
|
||||||
|
const socket = new WebSocket(address)
|
||||||
|
socket.onmessage = msg => msg.data == 'reload' && window.location.reload()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user