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-vuex-store

v2.1.1

Published

Utility tools for VUEX

Downloads

11

Readme

Build Status codecov

Babel-Plugin-Vuex-Store (shortly vx for Vuex) packs along a couple of features to simplify your Vuex-related coding.

Installation and usage

Install

npm install babel-plugin-vuex-store

Basic usage

.babelrc (applicable to nuxt as well)

{
  "plugins": [
    "babel-plugin-vuex-store"
  ]
}

Typescript usage

.babelrc (order matters)

{
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "@babel/plugin-transform-typescript",
    "babel-plugin-vuex-store"
  ]
}

The app entry file should also import proper typings:

/// <reference path="node_modules/babel-plugin-vuex-store/vuex.d.ts" />

Examples

Actions, Mutations, Getters

Suppose you have a file defined like this:

export const COUNTER_ACTIONS = {
    INCREMENT: "counter/increment"
};

export const COUNTER_MUTATIONS = {
    INCREMENT: "increment"
};

export const COUNTER_GETTERS = {
  TOTAL: 'counter/total'
}

This tends to be repetitive as you mostly mention the action, mutation or getter names twice, firstly as a key and secondly as a value after the module namespace.

Using this plugin allows you to specify only the module name as an array item. That is all there is to it:

export const VX_COUNTER_ACTIONS = {
  INCREMENT: ['counter']
}

export const VX_COUNTER_MUTATIONS = {
  INCREMENT: ['counter']
}

export const VX_COUNTER_GETTERS = {
  TOTAL: ['counter']
}

By now you have probably guessed it right for how to deal with nested modules. Just extend the array with the nested module:

export const VX_NESTED_ACTIONS = {
  INCREMENT: ['counter', 'nestedModule']
}

export const VX_NESTED_MUTATIONS = {
  INCREMENT: ['counter', 'nestedModule']
}

export const VX_NESTED_GETTERS = {
  TOTAL: ['counter', 'nestedModule']
}

To make it all possible, the only requirements are: 1 - sticking to a prefix naming convention when defining your actions, mutations or getters schema:

  • VX_[...]

i.e.:

  • export const VX_COUNTER_ACTIONS = { ... }
  • export const VX_COUNTER_MUTATIONS = { ... }
  • export const VX_COUNTER_GETTERS = { ... }

2 - sticking to a prefix naming convention when defining the implementation body of your actions, mutations or getters:

  • vx[...]

i.e.:

  • export const vxActions = { ... }
  • export const vxMutations = { ... }
  • export const vxGetters = { ... }

You can then do your imports within your store without a need to do any module name-spacing stuff, just:

import { VX_COUNTER_ACTIONS, VX_COUNTER_MUTATIONS } from '...'

export const vxActions = {
  [VX_COUNTER_ACTIONS.INCREMENT] ({ commit }, payload) {
    commit(VX_COUNTER_MUTATIONS.INCREMENT, payload)
  }
}
import { VX_COUNTER_MUTATIONS } from '...'

export const vxMutations = {
  [VX_COUNTER_MUTATIONS.INCREMENT] (state, payload) {
    state.count += payload
  }
}
import { VX_COUNTER_GETTERS } from '...'

export const vxGetters = {
  [VX_COUNTER_GETTERS.TOTAL] (state) {
    return state.count
  }
}

It is all the same inside a component. just do your imports, no module name-spacing needed:

example_template.vue

<template>
    ...
</template>

<script>
    import { mapActions, mapGetters, mapState } 
        from 'vuex'
    import { VX_COUNTER_GETTERS } 
        from './store/modules/counter/...'
    import { VX_NESTED_ACTIONS } 
        from './store/modules/counter/nested/...'
    
    export default {
        computed: {
          ...mapGetters({
            counter: VX_COUNTER_GETTERS.TOTAL
          }),
        },
        
        methods: {
          ...mapActions({
            incrementCounter: VX_COUNTER_ACTIONS.INCREMENT
          })
        }
    }
</script>

RootActions, RootMutations

Tired of something like this?

dispatch(COUNTER_ACTIONS.INCREMENT, null, { root: true });
dispatch(COUNTER_ACTIONS.INCREMENT, payload, { root: true });

This { root: true } is kind of repetitive and verbose too, especially if there is no payload (i.e. null) to deal with.

Time to meet your new friends, rootDispatch and rootCommit:

import { NESTED_ACTIONS } from './actions';
import { COUNTER_ACTIONS } from '../actions';
import { COUNTER_MUTATIONS } from '../mutations';

export const vxActions = {
    [NESTED_ACTIONS.INCREMENT] ({ rootCommit, rootDispatch }, payload) {
      rootCommit(COUNTER_MUTATIONS.SET_TIME_X);
      rootCommit(COUNTER_MUTATIONS.SET_TIME_Y, Date.now());
    
      rootDispatch(COUNTER_ACTIONS.INCREMENT_X);
      rootDispatch(COUNTER_ACTIONS.INCREMENT_Y, payload);
    }
};

Is it not a lot nicer and less verbose?

Guess what, let's simplify it even more. Looking at the example_template.vue component above, you have to import mapState, mapActions and mapGetters to get Vuex things going. But the plugin makes it possible to do without these imports.

Just meet your new prefix friends:

  • vxs to represent Vuex state
  • vxa to represent Vuex actions
  • vxg to represent Vuex getters

Take a look at the same component without the imports:

<template>
    <a href="javascript://" @click="vxa_incrementCounter(1)">
      Increment Counter
    </a>

    <p>
        {{ vxgCounter }}
        {{ vxgCounterMultiplied(2) }}
    </p>

    <p>
        {{ vxsAlias }} 
        {{ vxsCountBasic }} 
        {{ vxsCountNested }}</p>
    </p>
  </center>
</template>

<script>
  import { COUNTER_ACTIONS, COUNTER_GETTERS } 
    from './store/modules/counter/...'
  import { NESTED_ACTIONS, NESTED_GETTERS } 
    from './store/modules/counter/nested/...'

  export default {
    computed: {
      vxsAlias: { otherCount: 'counter' },
      vxsCountBasic: 'counter',
      vxsCountNested: 'counter/nested',

      vxgCounter: COUNTER_GETTERS.TOTAL,
      vxgCounterMultiplied: COUNTER_GETTERS.TOTAL_MULTIPLIED
    },

    methods: {
      vxaIncrementCounter: () => COUNTER_ACTIONS.INCREMENT
    }
  }
</script>

The resultant component contains less code and is more straightforward in what it intends to do.

Now, let's code efficiently and do not hesitate to contribute.