@rjfwhite/random-lib
Version:
Core engine for MML
135 lines (95 loc) • 3.28 kB
Markdown
A TypeScript library that builds to a self-contained ES module for browser usage.
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.
```html
<script type="module">
import AingineLib from './dist/index.js';
const lib = new AingineLib({ debug: true });
console.log(lib.greet('World'));
</script>
```
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>
```
```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);
```
- `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
The library provides several ways to extract query parameters from the module URL:
```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
```
```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
```
The generated ES module works in all modern browsers that support ES modules:
- Chrome 61+
- Firefox 60+
- Safari 10.1+
- Edge 16+