npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-unalias

v1.2.2

Published

A TypeScript transformer that rewrites module alias paths in emitted .d.ts files to their relative paths as per tsconfig.json compilerOptions.paths.

Downloads

5

Readme

ts-unalias

A TypeScript transformer that rewrites module alias paths in emitted .d.ts files to their relative paths as per tsconfig.json compilerOptions.paths.

Installation

You can install ts-unalias via npm:

npm install ts-unalias

Usage

import ts from 'typescript';
import { unaliasTransformerFactory } from 'ts-unalias';

// Create TypeScript program
const program = ts.createProgram([...], {
    /* Compiler options */
});

// Create transformer factory
const transformer = unaliasTransformerFactory(program, {
    /* Options */
});

// Apply transformer to emit process
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
    afterDeclarations: [transformer],
});

// Handle emit result
// ...

Features

  • Rewrites module alias paths in emitted .d.ts files
  • Supports tsconfig.json compilerOptions.paths configuration

Types

Notifier

Represents a Notify Function

type Notifier<T> = (item: T) => void

NotifierType

Represents a Notify Function Type to Generate a Notifier (string has ${item} placeholder)

type NotifierType<T> = boolean | string | Notifier<T>;

TsConfigPath

Represents a TypeScript configuration path.

type TsConfigPath = {
    name: string;
    to: string[];
    baseUrl?: string;
};

PathAlias

Represents a path alias.

type PathAlias = {
    name: string;
    full: string;
    to: string;
    regex: RegExp;
};

ExternalModuleDirection

Represents the direction of an external module resolution.

type ExternalModuleDirection = 'import' | 'export';

ExternalModuleType

Represents the type of an external module.

type ExternalModuleType = 'module' | 'path' | 'alias';

ExternalModule

Represents the result of resolving an external module.

type ExternalModule = {    
    direction: ExternalModuleDirection;
    type: ExternalModuleType;    
    from: string;    
    to: string;    
    originalTo: string;    
    fullToPath?: string;    
    relativeToPath?: string;
};

WebpackAlias

Represents a Webpack alias configuration.

type WebpackAlias = {
    name: string;
    to: string;
    resolved: string;
};

JestAlias

Represents a Jest alias configuration.

type JestAlias = {
    from: string;
    to: string;
};

WebpackAliasesOptions

Options for generating Webpack aliases.

type WebpackAliasesOptions = {
    searchPath?: string;
    configName?: string;
    onTsPath?: NotifierType<TsConfigPath>;
    onPathAlias?: NotifierType<PathAlias>;
    onWebpackAlias?: NotifierType<WebpackAlias>;
};

JestAliasesOptions

Options for generating Webpack aliases.

type WebpackAliasesOptions = {
    searchPath?: string;
    configName?: string;
    onTsPath?: NotifierType<TsConfigPath>;
    onPathAlias?: NotifierType<PathAlias>;
    onJestAlias?: NotifierType<JestAlias>;
};

UnaliasTransformOptions

Options for the unalias transformer.

type UnaliasTransformOptions = {
    onTsPath?: NotifierType<TsConfigPath>;
    onPathAlias?: NotifierType<PathAlias>;
    onExternalModule?: NotifierType<ExternalModule>;
};

Functions

getTsCompilerOptions

Gets TypeScript compiler options from a tsconfig file.

getTsCompilerOptions(
    searchPath?: string,
    configName?: string
): ts.CompilerOptions

extractTsConfigPaths

Extracts TypeScript configuration paths.

extractTsConfigPaths(
    options: ts.CompilerOptions, 
    onItem?:  NotifierType<TsConfigPath>
): TsConfigPath[]

extractPathAliases

Extracts path aliases from TypeScript configuration paths.

extractPathAliases(
    paths: TsConfigPath[], 
    onItem?: NotifierType<PathAlias>
): PathAlias[]

extractWebpackAliases

Extracts Webpack alias configurations from path aliases.

extractWebpackAliases(
    aliases: PathAlias[], 
    basePath: string,
    onItem?: NotifierType<WebpackAlias>
): Record<string, string>

resolveExternalModule

Resolves and classifies an external module.

resolveExternalModule(
    fromPath: string,
    toModule: string,
    aliases: PathAlias[]
): ExternalModule

unaliasTransformerFactory

Creates a TypeScript transformer that resolves aliased import/export paths.

unaliasTransformerFactory(
    program: ts.Program,
    options?: UnaliasTransformOptions
): ts.TransformerFactory<ts.SourceFile>

webpackAliases

Generates Webpack alias configurations based on TypeScript path mappings.

webpackAliases(
    basePath: string,
    options?: WebpackAliasesOptions
): Record<string, string>

jestAliases

Generates Jest alias configurations based on TypeScript path mappings.

jestAliases(
    options?: JestAliasesOptions
): Record<string, string>

Example: Using with Webpack

import path from 'path';
import { Configuration } from 'webpack';
import nodeExternals from 'webpack-node-externals';
import { unaliasTransformerFactory, webpackAliases } from 'ts-unalias';

const config: Configuration = {
    entry: './src/index.ts',
    mode: 'production',
    devtool: 'inline-source-map',
    target: 'node',
    externals: [nodeExternals()],
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, '..', '..', 'dist'),
        library: {
            name: 'tester',
            type: 'this'
        },
        libraryTarget: 'umd'
    },
    resolve: {
        extensions: ['.ts', '.js'],
        modules: ['node_modules'],
        mainFiles: ['index'],
        // Use to auto populate aliases from tsconfig file
        alias: webpackAliases(path.resolve(__dirname, '..', '..'), {
            onTsPath: item => console.log(item),
            onPathAlias: true,
            onWebpackAlias: false
        })
    },
    module: {
        rules: [
            {
                test: /.ts$/,
                use: [{
                    loader: 'ts-loader',
                    options: {
                        getCustomTransformers: (program: any) => ({
                            // Use to unalias all imports/exports in d.ts files
                            afterDeclarations: [unaliasTransformerFactory(program, {
                                onPathAlias: true
                                onExternalModule: '[EXTERNAL MODULE]: ${item}'
                            })]
                        }),
                    },
                }]
            }
        ]
    }
};

export default config;