Options
All
  • Public
  • Public/Protected
  • All
Menu

Module hunt-affected

hunt-affected

Detect where your file exports are used and potentially afffected by it's changes.

Quick start

Intalling via npm:

npm i hunt-affected

And feed the function with a list of absolute file paths you would like to check, and the entry points like this:

const huntAffected = require('hunt-affected');

huntAffected(['a.js', 'b.js', 'c.js'], [{ source: 'a.js', name: 'default' }]);

Following @babel/parser plugins are enabled on .js|.jsx|.ts|.tsx files by default:

  • dynamicImport
  • classProperties
  • flowComments
  • objectRestSpread
  • functionBind
  • jsx
  • flow (.js and .jsx only)
  • typescript (.ts and .tsx only)

Other than aboves, you will need to enable by:

huntAffected(
  ['a.js', 'b.js', 'c.js'],
  [{ source: 'a.js', name: 'default' }]
  {
    parserOptions: {
      plugins: ['jsx', 'dynamicImport']
    }
  }
);

All the options in parserOptions will be passed to @babel/parser directly. @babel/parser options can be found here.

By default, it will try to read file with NodeJs default file system and decode them with utf-8.

You may replace this behavior by passing a customised loader function:

huntAffected(
  ['a.js', 'b.js', 'c.js'],
  [{ source: 'a.js', name: 'default' }]
  {
    loader: async (path) {
      return await myWayToReadFile(path);
    }
  }
);

And when it tries to resolve file imported module paths to absolute file path, it will use Webpack's enhanced-resolve by default, and tries to resolve to real files.

You may replace this behavior by passing a customised resolver function:

huntAffected(
  ['a.js', 'b.js', 'c.js'],
  [{ source: 'a.js', name: 'default' }]
  {
    resolver: async (base: string, target: string) => {
      return 'resolved/file/path.js';
    }
  }
);

Index

References

Affected

Renames and re-exports Visited

fileDepMap

Re-exports fileDepMap

mergeDepMap

Re-exports mergeDepMap

visitDepMap

Renames and re-exports visitDependencyMap

Type aliases

Options

Options: { loader?: undefined | ((path: string) => Promise<string | void>); parserOptions?: ParserOptions; resolver?: undefined | ((base: string, target: string) => Promise<string | void>); resolverOptions?: ResolverFactory.ResolverOption }

Type declaration

  • Optional loader?: undefined | ((path: string) => Promise<string | void>)

    Function for reading file content from absolute file path. Uses fs.readFileSync to read with decoding utf8 by default.

  • Optional parserOptions?: ParserOptions

    Options provided by @babel/parser. Allow customize babel parser options while parsing file content to AST.

  • Optional resolver?: undefined | ((base: string, target: string) => Promise<string | void>)

    Customised resolver function to resolve a module import path to absolute file path. If this options is used, resolverOptions will be ignored.

  • Optional resolverOptions?: ResolverFactory.ResolverOption

Visited

Visited: {}

Type declaration

  • [module: string]: Set<Member>

Variables

Const MODULE_ALL

MODULE_ALL: "*" = "*"

Const MODULE_DEFAULT

MODULE_DEFAULT: "default" = "default"

Functions

appendEntries

  • appendEntries(depMap: DependencyMap, mod: Module, member: Member, entries: Entry[]): void

denodeify

  • denodeify<Response>(func: Function): (Anonymous function)

fileDepMap

  • fileDepMap(filePath: string, __namedParameters?: { loader: undefined | ((path: string) => Promise<string | void>); parserOptions: undefined | ParserOptions }): Promise<DependencyMap>
  • Get a file's dependency map

    Parameters

    • filePath: string

      Absolute file path

    • Default value __namedParameters: { loader: undefined | ((path: string) => Promise<string | void>); parserOptions: undefined | ParserOptions } = {}
      • loader: undefined | ((path: string) => Promise<string | void>)
      • parserOptions: undefined | ParserOptions

    Returns Promise<DependencyMap>

huntAffected

  • huntAffected(sources: string[], entries: Entry[], opts?: Options): Promise<Affected>
  • Find a list of module exports affected by given module exports in given list of files. Example:

    huntAffected(
     ['a.js', 'b.js', 'c.js'],
     [{ source: 'a.js', name: 'default' }]
    );

    Avaiable options:

    • resolver: ((base: string, target: string) => Promise<string | void>) | undefined
    • loader: Loader | undefined
    • parserOptions?: ParserOptions | undefined;

    Parameters

    • sources: string[]

      Source file absolute paths

    • entries: Entry[]

      A list of entry objects with source file absolute path, and export declaration name

    • Default value opts: Options = {}

      Options

    Returns Promise<Affected>

    A promise resolves to a key, value pair, key is an absolute path of a module, value is a Set of declaration names

mergeDepMap

  • mergeDepMap(sources: string[], opts?: Options): Promise<DependencyMap>
  • Extract the dependency map of a list of source files.

    Parameters

    • sources: string[]

      A list of absolute source file paths

    • Default value opts: Options = {}

    Returns Promise<DependencyMap>

visitDependencyMap

  • visitDependencyMap(dependencyMap: DependencyMap, entries: Entry[]): Visited

Generated using TypeDoc