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

awilix-vite

v2.0.3

Published

Use Awilix in Vite projects

Downloads

529

Readme

awilix-vite

The awilix-vite plugin is designed for the Awilix dependency injection library. It provides a loadModules method that allows Vite users to achieve almost the same behavior as the loadModules method in the original Awilix library by using Vite's import.meta.glob. The available loadModules method will take the result from import.meta.glob and load the modules correctly with Awilix.

Installation

To install the awilix-vite plugin, you need to have Awilix and Vite set up in your project. You can then install the plugin via your desired package manager:

npm install awilix-vite

Usage

The loadModules method provided by this plugin allows you to load and register modules into an Awilix container using the result of Vite's import.meta.glob function.

Importing the Plugin

import { createContainer } from 'awilix/browser';
import { loadModules } from 'awilix-vite';

Setting Up the Container

Create an Awilix container where the modules will be registered.

const container = createContainer();

Loading Modules

Use the import.meta.glob with eager set to true to dynamically import your modules.

const modules = import.meta.glob('./path/to/modules/*.js', { eager: true });

loadModules(container, modules, {
  resolverOptions: {
    // Optional: Awilix resolver options
  },
  formatName: (name) => name // Optional: Custom function to format module names, defaults to camelCase
});

Why use { eager: true }?

When using import.meta.glob Vite will transform the code

const modules = import.meta.glob('./dir/*.js')

into the following output:

// code produced by vite
const modules = {
  './dir/foo.js': () => import('./dir/foo.js'),
}

while using eager: true will result in the following output:

// code produced by vite:
import { setup as __glob__0_0 } from './dir/foo.js'
import { setup as __glob__0_1 } from './dir/bar.js'
const modules = {
  './dir/foo.js': __glob__0_0,
  './dir/bar.js': __glob__0_1,
}

When using loadModules all the modules are supposed to be loaded immediately, so there is no benefit in importing the the modules lazily. Therefore we recommend setting eager: true to increase performance.

Example

Here's a complete example of how to use the awilix-vite plugin to load and register modules.

import { createContainer } from 'awilix/browser';
import { loadModules } from 'awilix-vite';

const container = createContainer();

const modules = import.meta.glob('./services/*.js', { eager: true });

loadModules(container, modules, {
  resolverOptions: {
    lifetime: 'SINGLETON' // Example: Set lifetime to SINGLETON
  },
  formatName: (name) => `myPrefix${name.charAt(0).toUpperCase() + name.slice(1)}` // Example: Prefix and camelCase module names
});

API

loadModules(container, globResult, options)

Parameters

  • container (required): The Awilix container where the modules should be registered.
  • globResult (required): The result of either import.meta.glob('/.js') or import.meta.glob('/.js', { eager: true }).
  • options (optional): An object containing the following properties:
    • resolverOptions: Optional Awilix resolver options.
    • formatName: Optional function to format module names.

Why do i have to use import.meta.glob?

The import.meta.glob method will be transformed by Vite from

const modules = import.meta.glob('./dir/*.js', { eager: true })

into

// code produced by vite:
import { setup as __glob__0_0 } from './dir/foo.js'
import { setup as __glob__0_1 } from './dir/bar.js'
const modules = {
  './dir/foo.js': __glob__0_0,
  './dir/bar.js': __glob__0_1,
}

by using static code analysis. Because of this behaviour, it's not possible to create a function like this:

function loadModules(glob) {
    return import.meta.glob(glob, { eager: true })
}

Vite will not know what files should be imported since the code is transformed during build. This is why you have to seperately use the import.meta.glob yourself, and use the loadModules method from this library to correctly load the modules automatically with Awilix.