diff --git a/dts-config.js b/dts-config.js new file mode 100644 index 00000000..add8b0ed --- /dev/null +++ b/dts-config.js @@ -0,0 +1,42 @@ +const path = require('path') +const fs = require('fs') + +const NGX_META_DIR = path.join('.', 'projects', 'ngx-meta') +const NGX_META_SRC_DIR = path.join(NGX_META_DIR, 'src') + +const entrypoints = [] +const getDirectories = (source) => + fs + .readdirSync(source, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name) +const getFiles = (source) => fs.readdirSync(source) + +console.info('ℹ️ Looking for entry points') +const NG_PACKAGE_JSON_FILENAME = 'ng-package.json' +const ngxMetaSrcSubdirectories = getDirectories(NGX_META_SRC_DIR) +const ngxMetaEntrypoints = ngxMetaSrcSubdirectories.filter((subDirectory) => { + const ngxMetaSrcSubdirectory = path.join(NGX_META_SRC_DIR, subDirectory) + const files = getFiles(ngxMetaSrcSubdirectory) + return files.includes(NG_PACKAGE_JSON_FILENAME) +}) +ngxMetaEntrypoints.forEach((entrypoint) => { + console.info('- ', entrypoint) +}) + +const entries = ngxMetaEntrypoints.map((entrypoint) => ({ + filePath: path.join(NGX_META_DIR, 'out', entrypoint, 'index.d.ts'), + outFile: path.join(NGX_META_DIR, 'dist', entrypoint, 'bundled.d.ts'), + output: { + exportReferencedTypes: false, + }, +})) + +const config = { + compilationOptions: { + preferredConfigPath: path.join(NGX_META_SRC_DIR, 'tsconfig.lib.prod.json'), + }, + entries, +} + +module.exports = config diff --git a/package.json b/package.json index 8f92c3e8..266c2b83 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,10 @@ "scripts": { "ng": "ng", "start": "ng serve", - "build": "ng build", + "build": "ng build && ./postbuild.sh", "watch": "ng build --watch --configuration development", "test": "ng test", "lint": "ng lint", - "ngx-meta:build": "ng build ngx-meta", "ngx-meta:test": "ng test ngx-meta", "ngx-meta:lint": "ng lint ngx-meta", "ngx-meta:tsc": "pnpm run '/ngx-meta:tsc:.*/'", @@ -63,6 +62,7 @@ "@typescript-eslint/eslint-plugin": "7.1.1", "@typescript-eslint/parser": "7.1.1", "conventional-changelog-conventionalcommits": "7.0.2", + "dts-bundle-generator": "9.3.1", "eslint": "8.57.0", "eslint-config-prettier": "9.1.0", "husky": "9.0.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5dab04b..81c6fbc7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -91,6 +91,9 @@ devDependencies: conventional-changelog-conventionalcommits: specifier: 7.0.2 version: 7.0.2 + dts-bundle-generator: + specifier: 9.3.1 + version: 9.3.1 eslint: specifier: 8.57.0 version: 8.57.0 @@ -5204,6 +5207,15 @@ packages: engines: {node: '>=12'} dev: true + /dts-bundle-generator@9.3.1: + resolution: {integrity: sha512-1/nMT7LFOkXbrL1ZvLpzrjNbfX090LZ64nLIXVmet557mshFCGP/oTiQiZenafJZ6GsmRQLTYKSlQnkxK8tsTw==} + engines: {node: '>=14.0.0'} + hasBin: true + dependencies: + typescript: 5.2.2 + yargs: 17.7.2 + dev: true + /duplexer2@0.1.4: resolution: {integrity: sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==} dependencies: diff --git a/postbuild.sh b/postbuild.sh new file mode 100755 index 00000000..b677def1 --- /dev/null +++ b/postbuild.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env sh +# Commands to run after building +set -eu + +# ngx-meta Typescript definitions rollup +echo "ℹ️ Bundling Typescript definitions" +dts-bundle-generator --config dts-config.js + +# Replace `index.d.ts` with bundled definitions +echo "ℹ️ Replacing Typescript index files with bundled definitions" +NGX_META_DIST_DIR="projects/ngx-meta/dist" +for dist_file_entry in "$NGX_META_DIST_DIR"/*; do + BUNDLED_DEFINITIONS_FILENAME="bundled.d.ts" + INDEX_DEFINITIONS_FILENAME="index.d.ts" + bundled_definitions_file="$dist_file_entry/$BUNDLED_DEFINITIONS_FILENAME" + index_definitions_file="$dist_file_entry/$INDEX_DEFINITIONS_FILENAME" + if [ -f "$bundled_definitions_file" ]; then + echo "- $bundled_definitions_file -> $index_definitions_file" + mv "$bundled_definitions_file" "$index_definitions_file" + fi +done + +# Remove Typescript source directories +echo "ℹ️ Removing Typescript source directories" +for dist_file_entry in "$NGX_META_DIST_DIR"/*; do + entrypoint_src_dir="$dist_file_entry/src" + if [ -d "$entrypoint_src_dir" ]; then + echo "- $entrypoint_src_dir" + rm -rf "$entrypoint_src_dir" + fi +done