1.1.0 • Published 10 months ago

@ts-intl/dependency v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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

PropertyTypeDefaultDescription
statusesFileStatus[]nullbased on increment update, provide a file changed list to specify which files need to update
entriesstring[]nullabsolute entry path list, to reduce unused result
opts.extractIntlKeys(module: string) => string[] \| Promise<string[]>undefinedcustom intl keys extract method, using built-in one if not provide
opts.ignoreCollectDepsbooleanundefinedwhether re-collect dependency graph, provide true to improve performance
opts.madgeConfigMadgeConfignullvisit 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.graphDepsGraphnulllatest dependency graph of your project(before oldest modified time of statuses)
ctx.pathIntlKeysMapPathIntlKeysMapnulllatest path-keys map of your project(before oldest modified time of statuses)
extractIntlKeysOpts{ funcNamePattern?: string; hookNamePattern?: string; richNamePattern?: string; argIdx?: number; }undefinedlocalization 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

1.1.0

10 months ago

1.0.0

10 months ago

0.2.5

10 months ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.0

1 year ago