From 93e20cb4667fd6bb32b63ed400dd087aca5fde53 Mon Sep 17 00:00:00 2001 From: cynic Date: Fri, 29 Mar 2024 12:39:07 +0200 Subject: [PATCH] Add watch function to guide, in alignment with updated code --- README.md | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f611311..0022790 100644 --- a/README.md +++ b/README.md @@ -282,18 +282,39 @@ and add the following code to it: const esbuild = require('esbuild'); const ElmPlugin = require('esbuild-plugin-elm'); -esbuild.build({ - entryPoints: ['src/index.js'], - bundle: true, - outdir: '../js', - watch: process.argv.includes('--watch'), - plugins: [ - ElmPlugin({ - debug: true, - clearOnWatch: true, - }), - ], -}).catch(_e => process.exit(1)) +const isProduction = process.env.MIX_ENV === "prod" + +async function watch() { + const ctx = await esbuild.context({ + entryPoints: ['src/index.js'], + bundle: true, + outfile: '../js/elm.js', + plugins: [ + ElmPlugin({ + debug: true + }), + ], + }).catch(_e => process.exit(1)) + await ctx.watch() +} + + +async function build() { + await esbuild.build({ + entryPoints: ['src/index.js'], + bundle: true, + minify: true, + outfile: '../js/elm.js', + plugins: [ + ElmPlugin(), + ], + }).catch(_e => process.exit(1)) +} + +if (isProduction) + build() +else + watch() ``` Ref: @@ -495,4 +516,4 @@ gzip elm.min.js Now it's **33kb**. (488-33)/488 = **93%** bandwidth saving. ---> \ No newline at end of file +-->