UNPKG

@rjfwhite/random-lib

Version:

Core engine for MML

135 lines (95 loc) 3.28 kB
# AingineLib A TypeScript library that builds to a self-contained ES module for browser usage. ## Setup 1. Install dependencies: ```bash npm install ``` 2. Build the library: ```bash npm run build ``` This creates a bundled ES module at `dist/index.js` that can be included in any webpage. ## Usage ### In HTML with ES Modules ```html <script type="module"> import AingineLib from './dist/index.js'; const lib = new AingineLib({ debug: true }); console.log(lib.greet('World')); </script> ``` ### With Query Parameters You can pass configuration via query parameters in the script src: ```html <script type="module"> import AingineLib, { getModuleParam } from './dist/index.js?foo=bar&debug=true&theme=dark'; const lib = new AingineLib(); // Extract query parameters console.log(lib.getQueryParam('foo')); // 'bar' console.log(lib.getQueryParam('debug')); // 'true' console.log(lib.getQueryParams()); // { foo: 'bar', debug: 'true', theme: 'dark' } // Or use standalone functions console.log(getModuleParam('theme')); // 'dark' </script> ``` ### As a Package ```typescript import AingineLib from 'aingine-lib'; const lib = new AingineLib({ debug: true, theme: 'dark' }); // Use the library console.log(lib.greet('Developer')); const button = lib.createButton('Click me!', () => alert('Clicked!')); document.body.appendChild(button); ``` ## Development - `npm run build` - Build production bundle (minified) - `npm run build:dev` - Build with watch mode for development - `npm run build:types` - Generate TypeScript declaration files - `npm run build:all` - Build both bundle and types - `npm run dev` - Start development mode with watch - `npm run clean` - Clean dist folder - `npm run serve` - Start HTTP server on port 3000 - `npm run start` - Build and start server - `npm run demo` - Build, start server, and open browser ## Features - ✅ Self-contained ES module bundle - ✅ TypeScript support with type declarations - ✅ Minified production build - ✅ Source maps for debugging - ✅ Fast builds with esbuild - ✅ Works directly in browsers with `<script type="module">` - ✅ Query parameter extraction from module URL ## Query Parameter API The library provides several ways to extract query parameters from the module URL: ### Standalone Functions ```typescript import { getModuleParam, getModuleParamsObject, getModuleQueryParams } from './dist/index.js?foo=bar&debug=true'; // Get a specific parameter const foo = getModuleParam('foo'); // 'bar' // Get all parameters as an object const allParams = getModuleParamsObject(); // { foo: 'bar', debug: 'true' } // Get raw URLSearchParams object const params = getModuleQueryParams(); // URLSearchParams instance ``` ### Instance Methods ```typescript import AingineLib from './dist/index.js?user=john&config=prod'; const lib = new AingineLib(); // Get a specific parameter lib.getQueryParam('user'); // 'john' // Get all parameters lib.getQueryParams(); // { user: 'john', config: 'prod' } // Check if parameter exists lib.hasQueryParam('user'); // true lib.hasQueryParam('missing'); // false ``` ## Browser Support The generated ES module works in all modern browsers that support ES modules: - Chrome 61+ - Firefox 60+ - Safari 10.1+ - Edge 16+