@ts-intl/dependency
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

I18n keys dependency analysis

Useful tools to help collect and reveal i18n keys usage, features:

  1. generate dependency graph
  2. generate used i18n keys which grouped by file path Easy integration, reduce size of dictionary/messages and helpful to manage locale files.
type DepsGraph = Record<string, string[]>;
const depsGraphExample: DepsGraph = {
  '/src/a.tsx': ['/src/b.ts', '/src/c.tsx'],
  '/src/b.ts': ['/src/c.tsx'],
};

type PathIntlKeysMap = Record<string, string[]>;
const pathIntlKeysMapExample = {
  '/src/a.tsx': ['global.keyA', 'global.keyB', 'global.keyC'],
  '/src/b.ts': ['global.keyB', 'global.keyC'],
  '/src/c.tsx': ['global.keyC'],
};

Installation

npm install @ts-intl/dependency

getDependencies

Interface

const getDependencies = (
  statuses: FileStatus[],
  entries: string[],
  opts: {
    extractIntlKeys?: (module: string) => string[] | Promise<string[]>;
    ignoreCollectDeps?: boolean;
    madgeConfig: MadgeConfig;
  },
  ctx: {
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
  },
  extractIntlKeysOpts?: {
    funcNamePattern?: string;
    hookNamePattern?: string;
    richNamePattern?: string;
    argIdx?: number;
  }
) =>
  Promise<{
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
    modules: string[];
  }>;

Usage

const { graph, pathIntlKeysMap, modules } = await getDependencies(statuses, entries, { ...opts, ignoreCollectDeps: true }, ctx, extractIntlKeysOpts);

Configuration

Property Type Default Description
statuses FileStatus[] null based on increment update, provide a file changed list to specify which files need to update
entries string[] null absolute entry path list, to reduce unused result
opts.extractIntlKeys (module: string) => string[] | Promise<string[]> undefined custom intl keys extract method, using built-in one if not provide
opts.ignoreCollectDeps boolean undefined whether re-collect dependency graph, provide true to improve performance
opts.madgeConfig MadgeConfig null visit madge. baseDir is required and should be absolute path of root of your project. The provided config would merge with default config and pass to madge.
ctx.graph DepsGraph null latest dependency graph of your project(before oldest modified time of statuses)
ctx.pathIntlKeysMap PathIntlKeysMap null latest path-keys map of your project(before oldest modified time of statuses)
extractIntlKeysOpts { funcNamePattern?: string; hookNamePattern?: string; richNamePattern?: string; argIdx?: number; } undefined localization function syntax config for built-in extractIntlKeys

getDependenciesByEntries

getDependencies aim at increment update, getDependenciesByEntries is based on getDependencies and aiming at full update, which generate statuses and ctx by itself.

Interface

const getDependenciesByEntries = (
  entries: string[],
  opts: {
    extractIntlKeys?: (module: string) => string[] | Promise<string[]>;
    ignoreCollectDeps?: boolean;
    madgeConfig: MadgeConfig;
  },
  extractIntlKeysOpts?: {
    funcNamePattern?: string;
    hookNamePattern?: string;
    richNamePattern?: string;
    argIdx?: number;
  }
) =>
  Promise<{
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
    modules: string[];
  }>;

opts.ignoreCollectDeps would set to true to improve performance.

getDependenciesFs

based on getDependenciesByEntries, automatically generate entries by providing entry directory, for example in nextjs app, entry could be [nextjs-app-absolute-path]/src/pages

Interface

const getDependenciesFs = (
  entry: string,
  opts: {
    extractIntlKeys?: (module: string) => string[] | Promise<string[]>;
    ignoreCollectDeps?: boolean;
    madgeConfig: MadgeConfig;
  },
  extractIntlKeysOpts?: {
    funcNamePattern?: string;
    hookNamePattern?: string;
    richNamePattern?: string;
    argIdx?: number;
  }
) =>
  Promise<{
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
    modules: string[];
  }>;

pipeDependenciesRes

Return more useful information

Interface

pipeDependenciesRes(
  res: {
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
    modules: string[];
  }
): {
    graph: DepsGraph;
    pathIntlKeysMap: PathIntlKeysMap;
    modules: string[];
    moduleIntlKeysMap: {
      [module: string]: string[]; // used intl key list of a module(include offspring of this module)
    };
    usedIntlKeys: string[]; // all used intl keys
  }

License

License: MIT

Package Sidebar

Install

npm i @ts-intl/dependency

Weekly Downloads

19

Version

2.0.1

License

MIT

Unpacked Size

112 kB

Total Files

145

Last publish

Collaborators

  • hakonleung