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

haste-mapper

v0.4.0

Published

[![npm version](https://badge.fury.io/js/haste-mapper.svg)](https://badge.fury.io/js/haste-mapper)

Downloads

8

Readme

npm version

haste-mapper

NPM

Inspired from Facebook's node-haste, haste-mapper is a modules list builder.

How it works?

It will scan all the JavaScript files in the root directory you provide, read the docblock comment at the beginning of the file check for attributes. It will check for @providesModule attribute.

How can I use it?

All you have to do is to add a docblock comment at the beginnging of a .js file in your project's root containing something like

/**
 * @providesModule MyCustomModule
 */

and haste-mapper will recognize it as a named module and will add it to the modules list.

Additionally, a module can be ignored from the modules list, even if it is named using the @providesModule syntax. You can use the @ignoreModule property and the module will not be added to the modules map. Example:

/**
 * @providesModule MyCustomModule
 * @ignoreModule
 */

Now for actually using the haste-mapper to generate the modules list you want, the following prototype must be introduced:

type ModuleScannerInitDataType = {
    /**
     * Root directory to scan files in.
     */
    root: string|string[],

    /**
     * Paths to additional files to be scanned and added
     * to the modules map.
     */
    files: string[],
};
type ModulesList = Map<string, Module>;

scanModules(data: ModuleScannerInitDataType): Promise<ModulesList>;

You will have to use it as follows:

const haste = require('haste-mapper');

haste.scanModules({ root: '/something' }).then(modulesList => {
    console.log(modulesList);
});

Or, if you want to also scan specific files:

const haste = require('haste-mapper');

haste.scanModules({
    root: '/some-root',
    files: [
        path.resolve('./myFile.js'),
        path.resolve('./myOtherFile.js'),
    ],
}).then(modulesList => {
    console.log(modulesList);
});

Note: Passing the root key is currently required. This will not be necessary in a future release. Passing either root or files will be required then.

API

Type ModuleScannerInitDataType

Data type used for scanModules function, defined as:

type ModuleScannerInitDataType = {
    rootDir: string|string[],
    files: string[],
};

Type ModulesList

Return type of scanModules, defined as as a Map of string-keyed Modules.

Method scanModules(data: ModuleScannerInitDataType): Promise<ModulesList>

Recursively scans the files and directories given in the data object, looking for correct docblock modules definitions. When a module is found, it is added to the returned modules map.

Class Module

Module data encapsulation type. It provides methods for getting information about a given module.

Method Module#srcPath()

Returns the source file address of the module as a string.

Method Module#moduleName()

Returns the module name as documented in the module definition of the file.

Method Module#isIgnored()

Checks if the module should be ignored. A module is ignored if the property ignoreModule is found in the module definition block. If a module is ignored it should not be taken into account by the associated babel-plugin-haste-require.

Class ModulesMapUtils

Utilities for the ModulesMap type.

Type PlainModulesMap

Used as return type for ModulesMapUtils#toPlainObject. Defined as:

type PlainModulesMap = { [moduleName: string]: string };

Static method toPlainObject(modules: ModulesList): PlainModulesMap

Converts a map of type ModulesList to a plain object, where keys represent the modules names and values represent the source file path. Note that this will not include the ignored modules.