diff --git a/bundle.mjs b/bundle.mjs new file mode 100644 index 00000000..0f4ffe9b --- /dev/null +++ b/bundle.mjs @@ -0,0 +1,46 @@ +import * as esbuild from 'esbuild'; +import fs from 'fs'; + +const plugins = [{ + name: 'my-plugin', + setup(build) { + let count = 0; + build.onEnd(({ errors, warnings }) => { + count++; + const message = errors.length === 0 && warnings.length === 0 + ? 'Build completed.' + : `Build completed with ${errors.length} error(s) and ${warnings.length} warning(s).`; + console.log(`[BUILD #${count.toString().padStart(3, '0')}]:`, message); }); + }, +}]; + +const buildOptions = { + bundle: true, + entryPoints: ['./src/js/L.PM.js'], + loader: { + '.js': 'jsx', + '.css': 'css', + '.svg': 'dataurl' }, + minify: true, + outfile: './dist/leaflet-geoman.js', + //plugins: plugins, + sourcemap: true, +} + +const ctx = await esbuild.context({ ...buildOptions, plugins }); + +if (process.env.DEV) { + await ctx.watch(); + console.log('watching...'); + const { host, port } = await ctx.serve({ + port: 5500, + servedir: '.', + fallback: "./index.html" + }); + console.log(`Serving app at ${host}:${port}.`); +} else { + await ctx.rebuild(); + ctx.dispose(); + fs.copyFileSync('leaflet-geoman.d.ts', './dist/leaflet-geoman.d.ts'); + fs.copyFileSync('./dist/leaflet-geoman.js', './dist/leaflet-geoman.min.js'); +} diff --git a/bundle.ts b/bundle.ts deleted file mode 100644 index 569fd7a1..00000000 --- a/bundle.ts +++ /dev/null @@ -1,39 +0,0 @@ -const esbuild = require('esbuild'); -const path = require('path'); -const fs = require('fs'); - -// Define the entry point -const entryPoint = './src/js/L.PM.js'; - -// Define the output directory -const outdir = path.resolve(__dirname, 'dist'); -fs.mkdirSync(outdir, { recursive: true }); - -// Define an async function to handle the build -async function build() { - try { - await esbuild.build({ - entryPoints: [entryPoint], - bundle: true, - minify: true, - sourcemap: false, - outdir, - loader: { - '.js': 'jsx', - '.css': 'css', - '.svg': 'dataurl', - }, - }); - - // Copy the leaflet-geoman.d.ts file - fs.copyFileSync('leaflet-geoman.d.ts', path.join(outdir, 'leaflet-geoman.d.ts')); - fs.renameSync(path.join(outdir, 'L.PM.js'), path.join(outdir, 'leaflet-geoman.min.js')); - fs.renameSync(path.join(outdir, 'L.PM.css'), path.join(outdir, 'leaflet-geoman.css')); - } catch (error) { - console.error(error); - process.exit(1); - } -} - -// Call the build function -build(); \ No newline at end of file diff --git a/demo/customcontrols.html b/demo/customcontrols.html index e3aad608..d11bafd2 100644 --- a/demo/customcontrols.html +++ b/demo/customcontrols.html @@ -28,9 +28,9 @@