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

vue-diod

v0.1.3

Published

Dependency injection in Vue 3 with DIOD.

Downloads

20

Readme


Documentation & Examples | DIOD on GitHub

About

As stated by its author on GitHub, DIOD is:

A very opinionated and lightweight (under 2kB minified and gzipped) inversion of control container and dependency injector for Node.js or browser apps. It is available for vanilla Javascript usage but its true power will be shown by building Typescript apps.

Vue DIOD is made on top of DIOD and helps avoiding SOLID principles violations in Vue 3 applications. It allows to configure dependencies at the app level and / or at component level, without children knowing abour the actual implementations.

Basically, it wraps DIOD functionalities in a plugin which uses Vue's provide method to make dependencies available to subtree components, by calling inject with the registered abstract class as key.

It also provides its builder for bootstrapping dependencies at component level, and a composable which main purpose is helping with injection key typings.

Quick example (with plugin)

Define abstraction

export abstract class AbstractCounter {
  increment(value: number): number;
  decrement(value: number): number;
}

Create implementation

export class Counter implements AbstractCounter {
  public increment(value: number): number {
    return value + 1;
  }

  public decrement(value: number): number {
    return value - 1;
  }
}

Bootstrap dependency container

In your main.ts file, for example.

import('reflect-metadata');

// ...

app.use(VueDiod, {
  injectables: [{ register: AbstractCounter, use: Counter }],
});

// ...

Inject in component

Without helper

NB: Keeping your application decoupled from Vue DIOD.

In the component's <script setup>.

// ...

import type { InjectionKey } from 'vue';
import { inject } from 'vue';

const Key = AbstractCounter as unknown;
const counter = inject<() => AbstractCounter>(
  Key as InjectionKey<AbstractCounter>
  /* Optional fallback */
);

const increment = counter.increment.bind(counter);
const decrement = counter.decrement.bind(counter);

// ...
<template>
  <!-- Use in your template -->
</template>

With Vue DIOD helper

// ...

import { useVueDiod } from 'vue-diod';
const { injectServiceInstance } = useVueDiod();

const counter = injectServiceInstance<AbstractCounter>(
  AbstractCounter
  /* Optional fallback */
);

const increment = counter.increment.bind(counter);
const decrement = counter.decrement.bind(counter);

// ...

For other examples, please check the documentation that provides an example for bootstrapping the container at component level, and demonstrate DIOD's powerful autowire for constructor's dependencies.

Install

Install dependencies

Considering you already have a working Vue 3 project, you'll have to install Vue DIOD with its dependencies: reflect-metadata and obviously diod.

npm

npm install -s vue-diod diod reflect-metadata

yarn

yarn add vue-diod diod reflect-metadata

Typescript configuration

Modify your tsconfig.jsonto include the following settings:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Vite configuration

As stated in the Vite GitHub issues (here):

The emitDecoratorMetadata flag is intentionally not supported.

To enable this support, we have to disable esbuild and use SWC instead, thanks to the rollup-plugin-swc3 package.

npm install --save-dev @swc/core rollup-plugin-swc3

# OR

yarn add -D @swc/core rollup-plugin-swc3

Then, in our vite.config.js or vite.config.ts file, we simply have to add:

import { defineConfig } from 'vite';
import swc from 'rollup-plugin-swc3';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
  // ...

  plugins: [
    swc({
      jsc: {
        parser: {
          syntax: 'typescript',
          dynamicImport: true,
          decorators: true,
        },
        target: 'es2021',
        transform: {
          decoratorMetadata: true,
        },
      },
    }),
    vue(),
  ],
  esbuild: false,
});

TODO

  • [ ] Tests
  • [ ] Improve documentation
  • [ ] Complete examples in the monorepo packages