Coder Social home page Coder Social logo

webpack-localforage's Introduction

webpack-localForage

Webpack friendly https://github.com/mozilla/localForage (Offline storage, improved.)


API

http://mozilla.github.io/localForage/

Changes w.r.t. mozilla/localForage :

  • Removed promise module import.
  • Removed all moduleType specific code.
  • Lazy load localforage drivers using webpack bundle loader.

Installation :

  • Download the project or install via bower bower install webpack-localforage or npm npm install webpack-localforage
  • Install webpack npm install webpack with following loaders
    • bundle loader npm install bundle-loader
    • imports loader npm install imports-loader
    • exports loader npm install exports-loader
  • Install es6-promise polyfill (If you are targeting browsers with no es6 promise support). bower install es6-promise

Configuration :

var path = require('path');
/* Adjust the bower path according to your project structure */
var bowerComponentsPath = path.join(__dirname, '/bower_components');

var config = {
    module: {
            {
                test: /[\/]promise\.js$/,
                loaders: ['exports?Promise']
            },
            {
                test: /[\/](localforage|indexeddb|localstorage|websql)\.js$/,
                loaders: ['imports?this=>window']
            }
    },
    resolve: {
        alias: {
            localforage: path.join(bowerComponentsPath, 'webpack-localForage/src/localforage.js'),
            // A polyfill for ES6-style Promises
            promise: path.join(bowerComponentsPath, 'es6-promise/promise.js'),
        }
    }
};
module.exports = config;

Usage

If you are targeting browsers with no promise support (http://caniuse.com/#feat=promises), lazy load polyfill.

var es6PromiseSupport =
        'Promise' in window &&
        /*
         * Some of these methods are missing from
         * Firefox/Chrome experimental implementations
         */
        'resolve' in window.Promise &&
        'reject' in window.Promise &&
        'all' in window.Promise &&
        'race' in window.Promise &&
        (function () {
            /*
             * Older version of the spec had a resolver object
             * as the arg rather than a function
             */
            var resolve;
            new window.Promise(function (r) {
                resolve = r;
            });
            return (typeof resolve === 'function');
        }());

if (!es6PromiseSupport) {
    var load = require('bundle?lazy!promise');
    load(function (promise) {
        callback();
    });
} else {
    callback();
}
var callback = function(){
    require('localforage');
}

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.