From 58bc7b8d5ffaa344017fcfc5f90f57714c931b14 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Thu, 25 Feb 2021 13:23:22 +0100 Subject: [PATCH] Add build and watch mode --- ui/build.js | 52 ++++++++++++++++++++++++++++++----------------- ui/package.json | 4 ++-- ui/src/index.html | 4 +++- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/ui/build.js b/ui/build.js index 7d073e45..9767336a 100644 --- a/ui/build.js +++ b/ui/build.js @@ -1,22 +1,25 @@ const fs = require('fs') +const esbuild = require('esbuild') const bs = require('browser-sync').create() const historyApiFallback = require('connect-history-api-fallback') const production = process.env.NODE_ENV == 'production' +const watchMode = process.argv[2] == 'watch' -let builderPromise = require('esbuild').build({ +const esbuildOptions = { entryPoints: ['src/index.js'], + publicPath: '/', outdir: 'dist', - // format: 'esm', + format: 'esm', bundle: true, platform: 'browser', target: ['chrome58', 'firefox57', 'safari11', 'edge16'], - // splitting: true, + splitting: true, minify: production, sourcemap: !production, loader: { '.js': 'jsx', - '.svg': 'text', + '.svg': 'file', '.woff': 'file', '.woff2': 'file', '.ttf': 'file', @@ -24,24 +27,35 @@ let builderPromise = require('esbuild').build({ '.png': 'file', }, define: { + 'process.env.PHOTOVIEW_API_ENDPOINT': '"http://localhost:4001/"', 'process.env.NODE_ENV': '"development"', }, - incremental: true, -}) + incremental: watchMode, +} +fs.rmdirSync('dist/', { + recursive: true, +}) +fs.mkdirSync('dist/') fs.copyFileSync('src/index.html', 'dist/index.html') -bs.init({ - server: { - baseDir: './dist', - middleware: [historyApiFallback()], - }, - port: 1234, - open: false, -}) +if (watchMode) { + let builderPromise = esbuild.build(esbuildOptions) -bs.watch('src/**/*.js').on('change', async args => { - console.log('reloading', args) - builderPromise = (await builderPromise).rebuild() - bs.reload(args) -}) + bs.init({ + server: { + baseDir: './dist', + middleware: [historyApiFallback()], + }, + port: 1234, + open: false, + }) + + bs.watch('src/**/*.js').on('change', async args => { + console.log('reloading', args) + builderPromise = (await builderPromise).rebuild() + bs.reload(args) + }) +} else { + esbuild.buildSync(esbuildOptions) +} diff --git a/ui/package.json b/ui/package.json index 032b947a..de7d8aa3 100644 --- a/ui/package.json +++ b/ui/package.json @@ -23,8 +23,8 @@ "url-join": "^4.0.1" }, "scripts": { - "start": "parcel start src/index.html", - "build": "parcel build src/index.html --no-source-maps", + "start": "node build.js watch", + "build": "NODE_ENV=production node build.js", "test": "npm run lint && npm run jest", "lint": "eslint ./src --max-warnings 0 --cache", "jest": "jest" diff --git a/ui/src/index.html b/ui/src/index.html index 33db08aa..9ddd4acf 100644 --- a/ui/src/index.html +++ b/ui/src/index.html @@ -5,6 +5,8 @@ + + @@ -13,6 +15,6 @@
- +