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-define

v1.3.4

Published

Manage environment variables & configurations in Vue application.

Downloads

121

Readme

vue-define

NPM version Travis (.org) Coveralls github NPM

Manage environment variables & configurations in Vue application.

Powered by dotenv-conversion.

Install

npm install vue-define

Usage

Create Vue application:

import Vue from 'vue'
import VueDefine from 'vue-define'

...

// Without options
Vue.use(VueDefine)

// With options
Vue.use(VueDefine, options)

...

new Vue({...}).$mount('#app')

Options

  • options

    (optional) Must be an object.

    Has attributes of defines and dotenvConversionConfigOptions.

  • options.defines

    (optional) Must be an object.

    To define some pre-configurations if wanted.

    Eg: {APP_TITLE: 'My Website', APP_DESCRIPTION': 'A sample page of mine'}.

  • options.dotenvConversionConfigOptions

    (optional) Must be an object.

    To use with features of dotenv-conversion.

    Eg: {specs: { ... }, override: { ... }, prevents: { ... }}.

    See dotenv-conversion.

Features

Get or Set Configuration

Example of .env file in Vue application:

# .env file
VUE_APP_TITLE='My Website'
VUE_APP_DEBUG=true
VUE_APP_DATA='{"foo":"bar"}'

Those environment variables above will be automatically set in configuration after creating Vue application with vue-define.

Used in Vue components

// .vue file

<template>
   ...
</template>

<script>
export default {
    name: 'MyComponent',
    created() {

        // **Configuration**

        // Getting

        console.log(this.$defineManager.get('VUE_APP_TITLE')) // (string) 'My Website'
        console.log(this.$define.VUE_APP_TITLE) // (string) 'My Website'

        console.log(this.$defineManager.get('VUE_APP_DEBUG')) // (boolean) true
        console.log(this.$define.VUE_APP_DEBUG) // (boolean) true

        console.log(this.$defineManager.get('VUE_APP_DATA')) // (object) {foo: 'bar'}
        console.log(this.$define.VUE_APP_DATA) // (object) {foo: 'bar'}
        console.log(this.$defineManager.get('VUE_APP_DEBUG.foo')) // (string) 'bar'
        console.log(this.$define.VUE_APP_DATA.foo) // (string) 'bar'
        
        console.log(this.$defineManager.get('VUE_APP_NOT_SET')) // null
        console.log(this.$defineManager.get('VUE_APP_NOT_SET', 'default_value')) // (string) 'default_value'
        console.log(this.$define.VUE_APP_NOT_SET) // undefined

        // Setting

        this.$defineManager.set('VUE_APP_DATA.animals.dog', 'dog')
        this.$defineManager.set('VUE_APP_DATA.animals.cat', 'cat')
        console.log(this.$defineManager.get('VUE_APP_DATA.animals')) // (object) {dog: 'dog', cat: 'cat'}
        console.log(this.$define.VUE_APP_DATA.animals) // (object) {dog: 'dog', cat: 'cat'}
    
        this.$define.VUE_APP_DATA.vehices = {
            car: 'car',
            bike: 'bike',
        }
        console.log(this.$defineManager.get('VUE_APP_DATA.vehicles')) // (object) {car: 'car', bike: 'bike'}
        console.log(this.$defineManager.get('VUE_APP_DATA.vehicles.car')) // (string) 'car'
        console.log(this.$defineManager.get('VUE_APP_DATA.vehicles.bike')) // (string) 'bike'
        
        this.$defineManager.set('custom.value', 'value')
        console.log(this.$define.custom) // (object) {value: 'value'}

    }
}
</script>

Used in anywhere

import {Define, DefineManager, getenv} from 'vue-define'

...

// **Environment variables**

const env = getenv()

console.log(env)
// output:
// {
//    VUE_APP_TITLE: 'My Website',
//    VUE_APP_DEBUG: true,
//    VUE_APP_DATA: {
//       foo: 'bar',
//    },
// }

// **Configuration**

// Getting

console.log(DefineManager.get('VUE_APP_TITLE')) // (string) 'My Website'
console.log(Define.VUE_APP_TITLE) // (string) 'My Website'

console.log(DefineManager.get('VUE_APP_DEBUG')) // (boolean) true
console.log(Define.VUE_APP_DEBUG) // (boolean) true

console.log(DefineManager.get('VUE_APP_DATA')) // (object) {foo: 'bar'}
console.log(Define.VUE_APP_DATA) // (object) {foo: 'bar'}
console.log(DefineManager.get('VUE_APP_DEBUG.foo')) // (string) 'bar'
console.log(Define.VUE_APP_DATA.foo) // (string) 'bar'

console.log(DefineManager.get('VUE_APP_NOT_SET')) // null
console.log(DefineManager.get('VUE_APP_NOT_SET', 'default_value')) // (string) 'default_value'
console.log(Define.VUE_APP_NOT_SET) // undefined

// Setting

DefineManager.set('VUE_APP_DATA.animals.dog', 'dog')
DefineManager.set('VUE_APP_DATA.animals.cat', 'cat')
console.log(DefineManager.get('VUE_APP_DATA.animals')) // (object) {dog: 'dog', cat: 'cat'}
console.log(Define.VUE_APP_DATA.animals) // (object) {dog: 'dog', cat: 'cat'}

Define.VUE_APP_DATA.vehices = {
    car: 'car',
    bike: 'bike',
}
console.log(DefineManager.get('VUE_APP_DATA.vehicles')) // (object) {car: 'car', bike: 'bike'}
console.log(DefineManager.get('VUE_APP_DATA.vehicles.car')) // (string) 'car'
console.log(DefineManager.get('VUE_APP_DATA.vehicles.bike')) // (string) 'bike'

DefineManager.set('custom.value', 'value')
console.log(Define.custom) // (object) {value: 'value'}

Custom Configuration from Files

Example of configuration files:

// config.app.js file
export const LOG_ENABLED = true
export const LOG_METHOD = 'console'
// config.locale.js file
export default {
    LOCALES: {
        en: 'English',
        vi: 'Tiếng Việt',
    },
    DEFAULT_LOCALE: 'en',
    FALLBACK_LOCALE: 'en',
}
// config.lang.en.json file
{
    "LANG": {
        "en": {
            "hello": "Hello"
        }
    }
}
// config.lang.vi.json file
{
    "LANG": {
        "vi": {
            "hello": "Xin chào"
        }
    }
}

Load from those configuration files above:

import {Define, DefineManager} from 'vue-define'

DefineManager.import(
    import('./config.app'),
    import('./config.locale'),
    import('./config.lang.en.json'),
    import('./config.lang.vi.json')
).then(() => {
    console.log(Define.LOG_ENABLED) // (boolean) true
    console.log(Define.LOG_METHOD) // (string) 'console'
    console.log(Define.LOCALES) // (object) {en: 'English', vi: 'Tiếng Việt'}
    console.log(Define.DEFAULT_LOCALE) // (string) 'en'
    console.log(Define.FALLBACK_LOCALE) // (string) 'en'
    console.log(Define.LANG) // (object) {en: {hello: 'Hello'}, vi: {hello: 'Xin chào'}
})

Custom Configuration from Inline

import {Define, DefineManager} from 'vue-define'

DefineManager.append({
    LOG_ENABLED: true,
    LOG_METHOD: 'console',
}, {
    LOCALES: {
        en: 'English',
        vi: 'Tiếng Việt',
    },
    DEFAULT_LOCALE: 'en',
    FALLBACK_LOCALE: 'en',
}, {
    LANG: {
        en: {
            hello: 'Hello',
        }   
    }
}, {
    LANG: {
        vi: {
            hello: 'Xin chào',
        }   
    }
})

console.log(Define.LOG_ENABLED) // (boolean) true
console.log(Define.LOG_METHOD) // (string) 'console'
console.log(Define.LOCALES) // (object) {en: 'English', vi: 'Tiếng Việt'}
console.log(Define.DEFAULT_LOCALE) // (string) 'en'
console.log(Define.FALLBACK_LOCALE) // (string) 'en'
console.log(Define.LANG) // (object) {en: {hello: 'Hello'}, vi: {hello: 'Xin chào'}