Skip to content

Commit

Permalink
fix: use dts-bundle-generator to generate bundled ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Mar 10, 2024
1 parent b312362 commit e553ffd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
42 changes: 42 additions & 0 deletions dts-config.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:.*/'",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions postbuild.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e553ffd

Please sign in to comment.