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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-autoimport-generator

v1.0.6

Published

Generate files with imports based on globs

Readme

ts-autoimport-generator

A CLI tool that automatically generates TypeScript barrel files by collecting exports from files matching glob patterns. Configure output files once in tsag.ts, then run or watch — it handles the rest.

Install

npm install ts-autoimport-generator

Quick start

1. Create tsag.ts in your project root:

import type { Config } from 'ts-autoimport-generator';

export const config: Config = {
  stripExtensions: true,
  files: {
    './src/imports.generated.ts': {
      patterns: ['src/**/*.module.ts'],
    },
  },
};

2. Run:

npx tsag

This generates ./src/imports.generated.ts importing every file matched by the pattern.


Commands

| Command | Description | | -------------- | -------------------------------------------------------- | | tsag | Build all output files once and exit | | tsag watch | Build all output files, then watch for additions/removals and rebuild automatically |


Configuration

The config file must be named tsag.ts and placed in the project root (the directory where you run tsag). It must export a config object.

import type { Config } from 'ts-autoimport-generator';

export const config: Config = {
  stripExtensions: true,         // optional
  files: {
    '<output file path>': {
      patterns: ['<glob pattern>', ...],
      exportName: 'collected',   // optional
      prefix: './',              // optional
      tree: false,               // optional
    },
  },
};

Config

| Field | Type | Default | Description | | ----------------- | -------------------------------------- | ------- | -------------------------------------------------------------- | | stripExtensions | boolean | false | When true, strips .ts/.tsx from generated import paths | | files | { [outputPath: string]: FileConfig } | | Map of output file paths to their config |

FileConfig

| Field | Type | Default | Description | | ------------ | ---------- | ------------- | --------------------------------------------------------------------------- | | patterns | string[] | (required) | Glob patterns used to find input files | | exportName | string | "collected" | Name of the exported constant in the generated file | | prefix | string | "./" | Path prefix prepended to each import path | | tree | boolean | false | When true, emits a nested object tree reflecting the directory structure |


Output modes

Flat (default)

All matched files are imported and spread into a single flat object.

Config:

'./src/imports.generated.ts': {
  patterns: ['src/modules/**/*.module.ts'],
  exportName: 'modules',
}

Generated output:

/* GENERATED FILE, do not edit! */

import * as modules_auth_module from './auth.module';
import * as modules_user_module from './user.module';

export const modules = { ...modules_auth_module, ...modules_user_module };

Tree

Files are nested into an object matching the directory structure of the matched files.

Config:

'./src/features.generated.ts': {
  patterns: ['src/features/**/*.ts'],
  exportName: 'features',
  tree: true,
}

Generated output:

/* GENERATED FILE, do not edit! */

import * as features_auth_login from './auth/login';
import * as features_auth_logout from './auth/logout';
import * as features_user_profile from './user/profile';

export const features = {
  auth: {
    login: { ...features_auth_login },
    logout: { ...features_auth_logout },
  },
  user: {
    profile: { ...features_user_profile },
  },
};

Example tsag.ts

import type { Config } from 'ts-autoimport-generator';

export const config: Config = {
  stripExtensions: true,

  files: {
    // Flat barrel of all glob files
    './src/imports.generated.ts': {
      patterns: ['src/**/*.glob.ts', 'src/**/*.actions.ts'],
    },

    // Nested tree of all actions, grouped by directory
    './src/features/actions.generated.ts': {
      patterns: ['src/features/**/*.actions.ts'],
      exportName: 'actions',
      tree: true,
    },
  },
};

How import names are derived

Each matched file gets an import identifier generated by:

  1. Taking the file path relative to the common root of all patterns
  2. Replacing every character that is not alphanumeric or $ with _

For example, features/auth/login.actions.ts becomes features_auth_login_actions_ts.

Set stripExtensions: true in the root config to strip .ts/.tsx from import paths. When omitted or false, the full filename including extension is used.


TypeScript types

The Config and FileConfig types are exported from the package and can be used to type your config file:

import type { Config, FileConfig } from 'ts-autoimport-generator';

Repository

github.com/marcusrognes/ts-autoimport-generator