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

babel-plugin-import-glob

v2.0.0

Published

Babel plugin to enable importing modules using a glob pattern

Downloads

17,643

Readme

babel-plugin-import-glob

Babel plugin to enable importing modules using a glob pattern. Tested with Node.js 4 and above.

Installation

npm install --save-dev babel-plugin-import-glob

Then add import-glob to your .babelrc file, like:

{
  "plugins": ["import-glob"]
}

Usage

This plugin is useful if you have multiple modules but you don't want to import them one at a time.

Maybe you're using the handlebars-inline-precompile plugin and are putting your modules in a templates directory. Or you need to dynamically reference one out of several classes and don't want to maintain the lookup by hand. Perhaps you need to load multiple modules for their side-effects and wish to simply add them to a directory without additional work. If so, this plugin is for you!

Of course in the vast majority of cases you should just use normal import statements. Don't go overboard using this plugin.

You can import the default members of any matching module. Let's say you have a directory layout like this:

  • index.js
  • templates/main.handlebars.js
  • templates/_partial.handlebars.js

In index.js you can write:

import { main, _partial } from './templates/**/*.handlebars.js'

You can add an optional glob: prefix:

import { main, _partial } from 'glob:./templates/**/*.handlebars.js'

You can alias members:

import { main, _partial as partial } from './templates/**/*.handlebars.js'

Or import all matches into a namespace object:

import * as templates from './templates/**/*.handlebars.js'
// Provides `templates.main` and `templates._partial`

Note that you cannot import the default from the glob pattern. The following won't work and throws a SyntaxError:

import myTemplates from './templates/**/*.handlebars.js' // This will throw a SyntaxError

You can load modules for their side-effects though:

import './modules-with-side-effects/*.js'

Glob patterns

The plugin uses the glob package. Please refer to its documentation regarding the pattern syntax.

The glob pattern must be relative. It must start with ./ or ../. A SyntaxError is thrown otherwise.

Import members

Identifiers are generated for all matches using the dynamic portions of the pattern. File-separators in the resulting strings are replaced by dollar signs. The strings are then converted into identifiers.

A valid identifier cannot always be generated. If that's the case a SyntaxError is thrown with more details. Similarly multiple matches may result in the same identifier. This also results in a SyntaxError being thrown.

For the ./templates/**/*.handlebars.js example above the matches are:

  • ./templates/main.handlebars.js
  • ./templates/_partial.handlebars.js

The dynamic portions are main and _partial. These are valid identifiers and therefore used as the import members.

A SyntaxError is throw when importing a member that does not correspond to a match:

import { doesNotExist } from './templates/**/*.handlebars.js' // This will throw a SyntaxError

Here's an overview of how the members are determined for additional matches:

Match|Result|Reason :---|:---|:--- ./templates/terms-and-conditions.handlebars.js|termsAndConditions|The - cannot be used in the identifier so it's removed. The following character is uppercased ./templates/blog/footer.handlebars.js|blog$footer|The blog directory is captured by the ** expression in the pattern. It is joined with the footer name using a dollar sign ./templates/-main.handlebars.js|SyntaxError|The - is removed, resulting in the same identifier as for main.handlebars.js ./templates/new.handlebars.js|_new|new is a reserved word so it's prefixed with an underscore ./templates/blog/new.handlebars.js|blog$new|Even though new is a reserved word, it's combined with blog$ so no prefix is necessary ./templates/404.handlebars.js|_404|Identifiers can't start with digits so it's prefixed with an underscore ./templates/error-pages/404.handlebars.js|errorPages$404|Now that 404 is combined with errorPages$ it no longer needs to be prefixed ./templates/🙊.handlebars.js|SyntaxError|No identifier can be generated for 🙊

Brace expansions are not considered to be a dynamic portion of the pattern. Given the pattern ./templates/{blog,error-pages}/*.handlebars.js:

Match|Result :---|:--- ./templates/blog/footer.handlebars.js|footer ./templates/error-pages/404.handlebars.js|_404

Use parentheses patterns instead, e.g. ./templates/{@(blog),@(error-pages)}/*.handlebars.js:

Match|Result :---|:--- ./templates/blog/footer.handlebars.js|blog$footer ./templates/error-pages/404.handlebars.js|errorPages$404