Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions builder.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
const fs = require('fs');
const mkdir = require('mk-dirs');
const pretty = require('pretty-bytes');
const { minify } = require('terser');
const sizer = require('gzip-size');
const pkg = require('./package');
const ESM = fs.readFileSync('src/index.js', 'utf8');
const regexparam = require('regexparam').toString();
import { readFileSync, writeFileSync } from 'node:fs';
import { mkdir } from 'mk-dirs';
import prettyBytes from 'pretty-bytes';
import { gzipSizeSync } from 'gzip-size';
import { minify } from 'terser';
import pkg from './package.json' with { type: 'json' };

mkdir('dist').then(() => {
// Copy as is for ESM
fs.writeFileSync(pkg.module, ESM);
const ESM = readFileSync('src/index.js', 'utf8');

// Mutate (im|ex)ports for CJS
const CJS = regexparam.replace('function ', 'function convert ').concat(
ESM.replace(`import convert from 'regexparam';`, '')
.replace(/export default/, 'module.exports =')
);
await mkdir('dist');

fs.writeFileSync(pkg.main, CJS);
// Write ESM bundle as-is
writeFileSync(pkg.module, ESM);

// Minify & print gzip-size
const { code } = minify(CJS, { toplevel:true, compress:{ passes:10 } });
console.log(`> gzip size: ${pretty(sizer.sync(code))}`);

// Write UMD bundle
const name = pkg['umd:name'] || pkg.name;
let UMD = `!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.${name}=t()}(this,function(){`;
UMD += code.replace(/module.exports=/, 'return ') + '});';
fs.writeFileSync(pkg.unpkg, UMD);
});
// Minify ESM source to report gzip size only
const minOut = await minify(ESM, { toplevel: true, compress: { passes: 10 } });
if (minOut.error) throw minOut.error;
console.log(`> gzip size: ${prettyBytes(gzipSizeSync(minOut.code))}`);
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "navaid",
"version": "1.2.0",
"version": "2.0.0",
"repository": "lukeed/navaid",
"description": "A navigation aid (aka, router) for the browser in 865 bytes~!",
"unpkg": "dist/navaid.min.js",
"module": "dist/navaid.mjs",
"main": "dist/navaid.js",
"type": "module",
"exports": "./dist/navaid.mjs",
"types": "index.d.ts",
"license": "MIT",
"author": {
Expand All @@ -14,12 +14,12 @@
"url": "https://lukeed.com"
},
"engines": {
"node": ">= 6"
"node": ">= 20"
},
"scripts": {
"build": "node builder",
"pretest": "npm run build",
"test": "uvu -r esm test"
"test": "uvu test"
},
"files": [
"*.d.ts",
Expand All @@ -31,14 +31,13 @@
"router"
],
"dependencies": {
"regexparam": "^1.0.2"
"regexparam": "^3.0.0"
},
"devDependencies": {
"esm": "3.2.25",
"gzip-size": "5.1.1",
"mk-dirs": "2.1.0",
"pretty-bytes": "5.3.0",
"terser": "4.8.0",
"uvu": "0.3.3"
"gzip-size": "7.0.0",
"mk-dirs": "3.0.0",
"pretty-bytes": "7.1.0",
"terser": "5.44.0",
"uvu": "0.5.6"
}
}
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import convert from 'regexparam';
import { parse } from 'regexparam'

export default function Navaid(base, on404) {
var rgx, curr, routes=[], $={};
Expand All @@ -18,7 +18,8 @@ export default function Navaid(base, on404) {
}

$.on = function (pat, fn) {
(pat = convert(pat)).fn = fn;
pat = parse(pat)
pat.fn = fn
routes.push(pat);
return $;
}
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import navaid from '../src';
import navaid from '../src/index.js';

global.history = {};

Expand Down Expand Up @@ -210,7 +210,7 @@ run('$.run (wildcard)', () => {
let ctx = new navaid();
ctx.on('foo/bar/*', o => {
let wild = ran ? 'baz/bat/quz' : 'baz';
assert.equal(o, { wild }, '~> o.wild is expected');
assert.equal(o, { ['*']: wild }, '~> o["*"] is expected');
plan -= 1;
ran = true;
});
Expand All @@ -228,7 +228,7 @@ run('$.run (query)', () => {
navaid()
.on('foo/*', o => {
plan -= 1;
assert.is(o.wild, 'baz/bat', '~> trims query from "wild" key');
assert.is(o['*'], 'baz/bat', '~> trims query from "*" key');
})
.on('/bar/:id', o => {
plan -= 1;
Expand Down