From 2ff6a979e87914a2d975199d898a0e0558338d72 Mon Sep 17 00:00:00 2001 From: David Payr <1696106+Vanilagy@users.noreply.github.com> Date: Mon, 2 Sep 2024 01:06:35 +0300 Subject: [PATCH] Add build step --- .gitattributes | 1 + build.mjs | 66 ++++++++++++++++++++++++++++++++++++++++++ dist/metamuxer.js | 6 ++++ dist/metamuxer.min.js | 2 ++ dist/metamuxer.min.mjs | 1 + dist/metamuxer.mjs | 2 ++ package.json | 3 +- 7 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100644 build.mjs create mode 100644 dist/metamuxer.js create mode 100644 dist/metamuxer.min.js create mode 100644 dist/metamuxer.min.mjs create mode 100644 dist/metamuxer.mjs diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..831b29a --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +build/* linguist-generated \ No newline at end of file diff --git a/build.mjs b/build.mjs new file mode 100644 index 0000000..26498b1 --- /dev/null +++ b/build.mjs @@ -0,0 +1,66 @@ +import * as esbuild from 'esbuild'; +import process from 'node:process'; + +const baseConfig = { + entryPoints: ['src/index.ts'], + bundle: true, + logLevel: 'info' +}; + +const umdConfig = { + ...baseConfig, + format: 'iife', + + // The following are hacks to basically make this an UMD module. No native support for that in esbuild as of today + globalName: 'Metamuxer', + + footer: { + js: +`if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, Metamuxer)` + } +}; + +const esmConfig = { + ...baseConfig, + format: 'esm' +}; + +let ctxUmd = await esbuild.context({ + ...umdConfig, + outfile: 'dist/metamuxer.js' +}); +let ctxEsm = await esbuild.context({ + ...esmConfig, + outfile: 'dist/metamuxer.mjs' +}); +let ctxUmdMinified = await esbuild.context({ + ...umdConfig, + outfile: 'dist/metamuxer.min.js', + minify: true +}); +let ctxEsmMinified = await esbuild.context({ + ...esmConfig, + outfile: 'dist/metamuxer.min.mjs', + minify: true +}); + +if (process.argv[2] === '--watch') { + await Promise.all([ + ctxUmd.watch(), + ctxEsm.watch(), + ctxUmdMinified.watch(), + ctxEsmMinified.watch(), + ]); +} else { + ctxUmd.rebuild(); + ctxEsm.rebuild(); + ctxUmdMinified.rebuild(); + ctxEsmMinified.rebuild(); + + await Promise.all([ + ctxUmd.dispose(), + ctxEsm.dispose(), + ctxUmdMinified.dispose(), + ctxEsmMinified.dispose(), + ]); +} diff --git a/dist/metamuxer.js b/dist/metamuxer.js new file mode 100644 index 0000000..3098679 --- /dev/null +++ b/dist/metamuxer.js @@ -0,0 +1,6 @@ +"use strict"; +var Metamuxer = (() => { + // src/index.ts + console.log("hi"); +})(); +if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, Metamuxer) diff --git a/dist/metamuxer.min.js b/dist/metamuxer.min.js new file mode 100644 index 0000000..eb12a67 --- /dev/null +++ b/dist/metamuxer.min.js @@ -0,0 +1,2 @@ +"use strict";var Metamuxer=(()=>{console.log("hi");})(); +if (typeof module === "object" && typeof module.exports === "object") Object.assign(module.exports, Metamuxer) diff --git a/dist/metamuxer.min.mjs b/dist/metamuxer.min.mjs new file mode 100644 index 0000000..d914c60 --- /dev/null +++ b/dist/metamuxer.min.mjs @@ -0,0 +1 @@ +console.log("hi"); diff --git a/dist/metamuxer.mjs b/dist/metamuxer.mjs new file mode 100644 index 0000000..8ac0165 --- /dev/null +++ b/dist/metamuxer.mjs @@ -0,0 +1,2 @@ +// src/index.ts +console.log("hi"); diff --git a/package.json b/package.json index 5d96c9d..b703853 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "TODO", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "build": "node build.mjs", + "watch": "node build.mjs --watch" }, "author": "", "license": "MIT",