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 🙏

© 2025 – Pkg Stats / Ryan Hefner

babel-plugin-namespace

v0.2.2

Published

Babel plugin to enable namespace in require() and ES6 import

Readme

babel-plugin-namespace

Build Status npm downloads Coverage Status

A babel plugin to enable namespacing and rewrite these namespace as an alias for directories as different directories during the Babel process.

Description

Instead of using relative paths in your project, you'll be able to use a namespace to allow you to require a dependency in a way that is more loosely coupled to the directory structure on disk.

Note:

  • In this plugin when say namespace, it's actually just a module alias to translate path of your module from the directory structure on disk. So please don't be confused with it.
  • This plugin also work with require() function.
  • If you're using eslint-plugin-import, you should use eslint-import-resolver-babel-namespace to avoid having false errors.

Usage Instructions

Requirements

This is a Babel plugin so it requires Babel v6 to run.

Installation

Install the plugin

$ npm install -D babel-plugin-namespace

Usage

Given the directory structure:

app
|__ .babelrc
|__ foo
|   |__ bar
|       |__baz.js
|__ src
|   |__ models
|       |__ User.js
|   |__ controllers
|       |__ User.js
|__ package.json

Specify the plugin in your .babelrc with the custom configuration.

{
  "plugins": [
    "namespace"
  ]
}

In package.json:

The most important things in your package.json is the name field

{
  "name": ["my-package"]
}

Then the plugins will generate the source maps:

{
  "my-package": [
    "/(...realpath-of...)/my-package/src"
  ],
  "my-package/foo": "/(...realpath-of...)/my-package/foo"
}

Example:

In src/controllers/User.js:

// Instead of using this;
import UserModel from '../models/User';

// Use that:
import UserModel from 'my-package/models/User';

// => resolves: '../models/User.js';
// NOTE: "my-package" is come from your package.json
// Instead of using this;
import baz from '../../foo/bar/baz';

// Use that:
import baz from 'my-package/foo/bar/baz';

// => resolves: '../../foo/bar/baz.js';
// NOTE: "foo" directory is created by "includes" field from our configuration

If you've a very, very long package name. This plugin also supports sign expansion.

  • Tilde (~) (Use at your own risk)
import baz from '~/foo/bar/baz';

// => resolves: '../../foo/bar/baz.js';

// You can also remove the first path separator
import baz from '~foo/bar/baz';

// => resolves: '../../foo/bar/baz.js';
  • Colon (:)
import baz from ':/foo/bar/baz';

// => resolves: '../../foo/bar/baz.js';

// You can also remove the first path separator
import baz from ':foo/bar/baz';

// => resolves: '../../foo/bar/baz.js';

Options

Use Babel's plugin options by replacing the plugin string with an array of the plugin name and an object with the options:

{
  "plugins": [
    ["namespace", {
      "disableSync": false,
      "sources": [
        "src"
      ],
      "includes": [
        "foo"
      ],
      "excludes": [
        "node_modules"
      ],
      "namespaces": {
        "test": "tests",
        "foo/bar/baz": "path/to/foo/bar/baz",
      }
    }]
  ]
}

From these options the plugins will generate the source maps:

{
  "my-package": [
    "/(...realpath-of...)/my-package/src"
  ],
  "my-package/foo": "/(...realpath-of...)/my-package/foo",
  "foo/bar/baz": "/(...realpath-of...)/my-package/path/to/foo/bar/baz",
  "test": "/(...realpath-of...)/my-package/path/to/foo/bar/tests"
}

These options are currently available:

Field | Type | Default | Description --------------|----------------|----------------|------------ disableSync | Boolean | false | If true, doesn’t actually includes all directories in the first depth of your project root directory. See: includes sources | String|Array | src | The lists of the source directory. The plugin will translate all values as a source path of the package name (e.g. Pakage name: "my-package"; Source Directory: "src"; Import Syntax: import "my-package/foo"; Transformed: import "./src/foo"). If the given value is a string, it should separated with comma (,) or single space ( ). includes | String|Array | depth + 1 | The lists of the included directories. The plugin will translate all values as a suffix of the package name (e.g. Pakage name: "my-package"; Include Directory: "tests"; Import Syntax: import "my-package/tests"; Transformed: import "./tests"). By default this plugin will fetch all directories in the first depth of your project root directory. You may want to disable this option by changing the disableSync to true. If the given value is a string, it should separated with comma (,) or single space ( ). excludes | String|Array | node_modules | Exclude all of these directories from the source map generator. This option is still Buggy, use at your own risk. If the given value is a string, it should separated with comma (,) or single space ( ). namespaces | Object | {} | The keys of the namespaces object will be used to match against as an import statement. To use a namespace in a file, simply place the name of the namespace in your import statement and continue writing the path from there.

Why use babel-plugin-namespace?

It's up to you. There's nothing wrong with the current import system. Regardless, you may still consider it useful to namespace your modules under a name of your choosing, such as M or $, freeing up those "global" modules for use without conflicts.

License

MIT, see LICENSE for details.