Git Product home page Git Product logo

dynamic-script-loader's Introduction

Coverage Status Build Status Maintainability

Script Loader

A script loader for modular components in es6 (build process coming soon). This is built so that each components being responsible for what 3rd party dependencies they need.

  • if script has not been loaded, it will load the script, and return a promise
  • onLoadHandler and onErrorHandler will execute on script load and on script error
  • script promise will be resolved when the script is loaded, rejects if it errors
  • if a script has already been requested to load throught dynamic script loader, it will return the existing promise
  • all script proimses are added to window.dynamicScriptLoader global object

Install

npm i -S dynamic-script-loader

Usage

simple

const scriptLoader = require('dynamic-script-loader')

scriptLoader({
    src: ''//path/to/someExternalJS.js'
  },
  onLoadHandler, // optional
  onErrorHandler // optional
  )
  .then(resolverFunction)
  .catch(catchFunction)

Vue

creating a vue wrapper for 3rd party libraries

heres a couple of ways you can use the script loader

using it as a method

import scriptLoader from 'dynamic-script-loader'

Vue.use({
  install: function (Vue, options) {
    Vue.prototype.$scriptLoader = scriptLoader
})

// inside a Vue component
data () {
  return {
    message: '',
    error: ''
  }
},

mounted () {
  // the loader will not load libraries if they have already been loaded
  this.$scriptLoader({
    src: '//some-cdn/src/js/library.js',
    async: true
  }, () => {
    // one time setup
    some3rdPartyLib.credentials('123')
    some3rdPartyLib.partyTime = true
    some3rdPartyLib.breakRandomly = Math.random() % 2 === 1
  })
    .then(() => {
      // something to run everytime this component is mounted
      this.message = some3rdPartyLib.getMessage()
    })
    .catch(() => {
      // script failed to load
      this.error = 'oops'
    })
}

CAVEATS

script loading uses URL's as unique keys so if urls change by for example, query string, then the script will try to load it again. example:

  • urlA = //www.example.com/script.js
  • urlB = https://www.example.com/script.js
  • urlC = https://www.example.com/script.js?hi

the script loader will treat these as separate javascript files

TODO

  • use typescript!

Contributing

  • git clone [email protected]:john-ko/dynamic-script-loader.git
  • cd dynamic-script-loader
  • npm install
  • add changes
  • npm test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.