mirror: A fast canonicalisation utility for stable object equality
0

Configure Feed

Select the types of activity you want to include in your feed.

chore: build npm my own way

Well Luke's way, but adapted

Marais Rossouw (Sep 5, 2024, 1:42 PM +1000) 1a575aa8 4069101d

+93 -46
+21
package.json
··· 1 + { 2 + "name": "object-identity", 3 + "version": "0.1.2", 4 + "repository": "maraisr/object-identity", 5 + "license": "MIT", 6 + "author": "Marais Rossow <me@marais.dev> (https://marais.io)", 7 + "keywords": [ 8 + "object", 9 + "identity", 10 + "hash", 11 + "fingerprint" 12 + ], 13 + "sideEffects": false, 14 + "exports": { 15 + ".": { 16 + "types": "./index.d.mts", 17 + "import": "./index.mjs" 18 + }, 19 + "./package.json": "./package.json" 20 + } 21 + }
+72 -46
scripts/build.ts
··· 1 - import { build, emptyDir } from '@deno/dnt'; 1 + // Credit @lukeed https://github.com/lukeed/empathic/blob/main/scripts/build.ts 2 2 3 - await emptyDir('./npm'); 3 + // Publish: 4 + // -> edit package.json version 5 + // -> edit deno.json version 6 + // $ git commit "release: x.x.x" 7 + // $ git tag "vx.x.x" 8 + // $ git push origin main --tags 9 + // #-> CI builds w/ publish 4 10 5 - await build({ 6 - entryPoints: ['./mod.ts'], 7 - outDir: './npm', 8 - shims: { 9 - deno: 'dev', 10 - }, 11 + import oxc from 'npm:oxc-transform@^0.25'; 12 + import { join, resolve } from '@std/path'; 13 + 14 + import denoJson from '../deno.json' with { type: 'json' }; 15 + 16 + const outdir = resolve('npm'); 17 + 18 + let Inputs; 19 + if (typeof denoJson.exports === 'string') Inputs = { '.': denoJson.exports }; 20 + else Inputs = denoJson.exports; 11 21 12 - declaration: 'inline', 13 - declarationMap: false, 14 - scriptModule: 'cjs', 15 - typeCheck: 'both', 16 - test: true, 22 + async function transform(name: string, filename: string) { 23 + if (name === '.') name = 'index'; 24 + name = name.replace(/^\.\//, ''); 17 25 18 - importMap: 'deno.json', 26 + let entry = resolve(filename); 27 + let source = await Deno.readTextFile(entry); 19 28 20 - package: { 21 - name: 'object-identity', 22 - version: Deno.args[0], 23 - repository: 'maraisr/object-identity', 24 - license: 'MIT', 25 - author: { 26 - name: 'Marais Rososuw', 27 - email: 'me@marais.dev', 28 - url: 'https://marais.io', 29 + let xform = oxc.transform(entry, source, { 30 + typescript: { 31 + onlyRemoveTypeImports: true, 32 + declaration: true, 29 33 }, 30 - keywords: [ 31 - 'object', 32 - 'identity', 33 - 'hash', 34 - 'fingerprint', 35 - ], 36 - }, 34 + }); 37 35 38 - compilerOptions: { 39 - target: 'ES2022', 40 - lib: ['ES2022', 'WebWorker'], 41 - }, 36 + if (xform.errors.length > 0) bail('transform', xform.errors); 37 + 38 + let outfile = `${outdir}/${name}.d.mts`; 39 + console.log('> writing "%s" file', outfile); 40 + await Deno.writeTextFile(outfile, xform.declaration!); 41 + 42 + outfile = `${outdir}/${name}.mjs`; 43 + console.log('> writing "%s" file', outfile); 44 + await Deno.writeTextFile(outfile, xform.sourceText); 45 + } 46 + 47 + if (exists(outdir)) { 48 + console.log('! removing "npm" directory'); 49 + await Deno.remove(outdir, { recursive: true }); 50 + } 51 + await Deno.mkdir(outdir); 52 + 53 + for (let [name, filename] of Object.entries(Inputs)) await transform(name, filename); 54 + 55 + await copy('package.json'); 56 + await copy('readme.md'); 57 + await copy('license'); 58 + 59 + // --- 60 + 61 + function bail(label: string, errors: string[]): never { 62 + console.error('[%s] error(s)\n', label, errors.join('')); 63 + Deno.exit(1); 64 + } 42 65 43 - filterDiagnostic(diag) { 44 - let txt = diag.messageText.toString(); 45 - return !txt.includes( 46 - // ignore type error for missing Deno built-in information 47 - `Type 'ReadableStream<string>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator`, 48 - ); 49 - }, 66 + function exists(path: string) { 67 + try { 68 + Deno.statSync(path); 69 + return true; 70 + } catch (_) { 71 + return false; 72 + } 73 + } 50 74 51 - async postBuild() { 52 - await Deno.copyFile('license', 'npm/license'); 53 - await Deno.copyFile('readme.md', 'npm/readme.md'); 54 - }, 55 - }); 75 + function copy(file: string) { 76 + if (exists(file)) { 77 + let outfile = join(outdir, file); 78 + console.log('> writing "%s" file', outfile); 79 + return Deno.copyFile(file, outfile); 80 + } 81 + }