From e8a3cee22c820d5f9d52fbd3ae1260c107668d60 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Tue, 3 Dec 2024 00:14:15 +0530 Subject: [PATCH] chore: make build script more generic --- deno.json | 9 +++++++ scripts/build.ts | 66 ++++++++++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/deno.json b/deno.json index db2a56b..57f4afd 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,15 @@ { "name": "prettier-plugin-vitepress", "version": "0.0.5", + "description": "a prettier plugin to format vue in markdown syntax used in vitepress", + "keywords": ["prettier", "vitepress", "vue", "markdown"], + "repository": { + "type": "git", + "url": "git+https://github.com/brc-dd/prettier-plugin-vitepress.git" + }, + "funding": "https://github.com/sponsors/brc-dd", + "license": "MIT", + "author": "Divyansh Singh (https://github.com/brc-dd)", "exports": "./src/index.ts", "compilerOptions": { "noFallthroughCasesInSwitch": true, diff --git a/scripts/build.ts b/scripts/build.ts index bfc06b6..fa052df 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -4,47 +4,59 @@ import * as esbuild from 'esbuild' import tsid from 'unplugin-isolated-decl/esbuild' import denoJson from '../deno.json' with { type: 'json' } +const pick = [ + 'name', + 'version', + 'description', + 'keywords', + 'repository', + 'funding', + 'license', + 'author', +] as const + +const peerDeps = ['prettier'] as const +const minify = Deno.env.get('CI') === 'true' +const exports = typeof denoJson.exports === 'string' ? { '.': denoJson.exports } : denoJson.exports + await emptyDir('dist') -await esbuild.build({ - plugins: [ - tsid(), - ...denoPlugins(), - ], - entryPoints: [denoJson.exports], +const res = await esbuild.build({ + plugins: [tsid(), ...denoPlugins()], + entryPoints: Object.values(exports), outdir: 'dist', bundle: true, format: 'esm', platform: 'neutral', target: 'es2022', external: ['prettier'], - minify: !!Deno.env.get('CI'), + metafile: true, + minify, }) +const outputs = Object.fromEntries( + Object.entries(res.metafile.outputs).flatMap(([key, value]) => + value.entryPoint ? [['./' + value.entryPoint, key.replace('dist/', './')]] : [] + ), +) + await Deno.writeTextFile( 'dist/package.json', - JSON.stringify({ - name: denoJson.name, - version: denoJson.version, - description: 'a prettier plugin to format vue in markdown syntax used in vitepress', - keywords: ['prettier', 'vitepress', 'vue', 'markdown'], - repository: { - type: 'git', - url: 'git+https://github.com/brc-dd/prettier-plugin-vitepress.git', - }, - funding: 'https://github.com/sponsors/brc-dd', - license: 'MIT', - author: 'Divyansh Singh (https://github.com/brc-dd)', - sideEffects: false, - type: 'module', - main: 'index.js', - peerDependencies: { - prettier: denoJson.imports.prettier.split('@')[1], + JSON.stringify( + { + ...Object.fromEntries(pick.map((key) => [key, denoJson[key]])), + type: 'module', + exports: Object.fromEntries( + Object.entries(exports).map(([key, value]) => [key, outputs[value]]), + ), + peerDependencies: Object.fromEntries( + peerDeps.map((dep) => [dep, denoJson.imports[dep].split('@')[1]]), + ), }, - }), + null, + minify ? 0 : 2, + ), ) await Deno.copyFile('LICENSE.md', 'dist/LICENSE.md') await Deno.copyFile('README.md', 'dist/README.md') - -// TODO: make it more generic