Skip to content

Commit

Permalink
Merge pull request #1 from localForage-extensions/rollup
Browse files Browse the repository at this point in the history
chore: use es6 & rollup
  • Loading branch information
thgreasi committed May 10, 2016
2 parents 9869191 + c7de7da commit 7406785
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 178 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015-rollup" ]
}
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"parser": "babel-eslint",
"extends": "eslint:recommended",
"env": {
"node": true,
"mocha": true
},
"rules":{
"no-console": 0
},
"globals": {
"localforage": true,
"Promise": true,
"console": true,
"self": true,
"System": true
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist
build
bower_components
node_modules
components
31 changes: 31 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"esnext": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowTrailingComma": true,
"requireBlocksOnNewline": true,
"requireLineFeedAtFileEnd": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"safeContextKeyword": ["globalObject", "self"],
"validateQuoteMarks": {
"escape": true,
"mark": "'"
},
"validateIndentation": 4
}
32 changes: 32 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"asi": false,
"bitwise": true,
"browser": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"nonew": true,
"quotmark": false,
"strict": false,
"trailing": false,
"undef": true,
"unused": true,

"validthis": true,

"globals": {
"console": true,
"define": true,
"localforage": true,
"module": true,
"Promise": true,
"require": true,
"self": true,
"System": true
}
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "4.0"
- "5.0"
31 changes: 0 additions & 31 deletions bower.json

This file was deleted.

4 changes: 2 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<title>Simple localForage-plugin example</title>
</head>
<body>
<script src="../bower_components/localforage/dist/localforage.js"></script>
<script src="../src/localforage-plugin-boilerplate.js"></script>
<script src="../node_modules/localforage/dist/localforage.js"></script>
<script src="../dist/localforage-plugin-boilerplate.js"></script>
<script>
localforage.ready(function() {
localforage.pluginMethod().then(function(result) {
Expand Down
83 changes: 83 additions & 0 deletions lib/localforage-plugin-boilerplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// // you can access the serializer and thedrivers by:
// import { getSerializerPromise, getDriverPromise } from './utils';

// getSerializerPromise();
// getDriverPromise(localforage.WEBSQL);

function handleMethodCall(localforageInstance, methodName, args) {
return localforageInstance.ready()
.then(function () {
console.log('Invoking ' + methodName + ' with arguments: ', args);
var promise = localforageInstance._baseMethods[methodName].apply(localforageInstance, args);
promise.then(function(result) {
console.log('Invoking ' + methodName + ' resolved with: ', result);
}, function(err) {
console.log('Invoking ' + methodName + ' rejected with: ', err);
});
return promise;
});
}

// wraps the localForage methods of the WrappedLibraryMethods array and
// allows you to execute code before & after their invocation
function wireUpMethods(localforageInstance) {
var WrappedLibraryMethods = [
'clear',
'getItem',
'iterate',
'key',
'keys',
'length',
'removeItem',
'setItem'
];

function wireUpMethod(localforageInstance, methodName) {
localforageInstance._baseMethods = localforageInstance._baseMethods || {};
localforageInstance._baseMethods[methodName] = localforageInstance[methodName];
localforageInstance[methodName] = function () {
return handleMethodCall(this, methodName, arguments);
};
}

for (var i = 0, len = WrappedLibraryMethods.length; i < len; i++) {
var methodName = WrappedLibraryMethods[i];
wireUpMethod(localforageInstance, methodName);
}
}

// place your plugin initialization logic here
// useful in case that you need to preserve a state
function setup(localforageInstance) {
if (!localforageInstance._pluginPrivateVariables) {
localforageInstance._pluginPrivateVariables = {
listOfImportantThings: [],
callCount: 0
};

// in case you need to observe the invocation of some methods
wireUpMethods(localforageInstance);
}
}

// this will be available as `localforage.pluginMethod('test');`
export function localforagePluginBoilerplate(/*option*/) {
var localforageInstance = this;

// this will initialize your plugin lazily
// after the first invocation of your method
setup(localforageInstance);

console.log('Hello world from the plugin method!');
return Promise.resolve('Hello world result!');
}

// add your plugin method to every localForage instance
export function extendPrototype(localforage) {
var localforagePrototype = Object.getPrototypeOf(localforage);
if (localforagePrototype) {
localforagePrototype.pluginMethod = localforagePluginBoilerplate;
}
}

export var extendPrototypeResult = extendPrototype(localforage);
26 changes: 26 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function getSerializerPromise(localForageInstance) {
if (getSerializerPromise.result) {
return getSerializerPromise.result;
}
if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') {
Promise.reject(new Error(
'localforage.getSerializer() was not available! ' +
'localforage v1.4+ is required!'));
}
getSerializerPromise.result = localForageInstance.getSerializer();
return getSerializerPromise.result;
}

export function getDriverPromise(localForageInstance, driverName) {
getDriverPromise.result = getDriverPromise.result || {};
if (getDriverPromise.result[driverName]) {
return getDriverPromise.result[driverName];
}
if (!localForageInstance || typeof localForageInstance.getDriver !== 'function') {
Promise.reject(new Error(
'localforage.getDriver() was not available! ' +
'localforage v1.4+ is required!'));
}
getDriverPromise.result[driverName] = localForageInstance.getDriver(driverName);
return getDriverPromise.result[driverName];
}
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
{
"name": "localforage-plugin-boilerplate",
"author": "Thodoris Greasidis",
"description": "A simple plugin boilerplate for localForage.",
"keywords": [
"localforage",
"plugin"
],
"version": "1.0.0",
"description": "A simple plugin boilerplate for localForage.",
"homepage": "https://github.com/localForage-extensions/localForage-plugin-boilerplate",
"main": "src/localforage-plugin-boilerplate.js",
"jsnext:main": "dist/localforage-plugin-boilerplate.es6.js",
"scripts": {
"prebuild": "eslint lib test",
"build": "rollup -c rollup.config.umd.js && rollup -c rollup.config.es6.js",
"pretest": "rollup -c rollup.config.test.js",
"test": "mocha build/test-bundle.js",
"prepublish": "npm run build && npm test"
},
"repository": {
"type": "git",
"url": "git://github.com/localForage-extensions/localForage-plugin-boilerplate.git"
},
"devDependencies": {
"cors": "^2.3.1",
"grunt": "^0.4.2",
"grunt-contrib-concat": "^0.3.0",
"grunt-contrib-connect": "^0.8.0",
"grunt-contrib-jshint": "^0.9.2",
"grunt-contrib-uglify": "^0.4.0",
"grunt-contrib-watch": "^0.5.0",
"grunt-es3-safe-recast": "^0.1.0",
"grunt-jscs": "^0.8.0",
"grunt-mocha": "^0.4.10",
"grunt-saucelabs": "^5.1.2",
"grunt-shell": "^0.6.4",
"load-grunt-tasks": "^0.4.0",
"mocha": "^1.18.2",
"phantomjs": "^1.9.7-12",
"uglify-js": "^2.3.x"
},
"browser": "src/localforage-plugin-boilerplate.js",
"main": "src/localforage-plugin-boilerplate.js",
"keywords": [
"localforage",
"plugin"
],
"author": "Thodoris Greasidis",
"licence": "Apache-2.0",
"bugs": {
"url": "http://github.com/localForage-extensions/localForage-plugin-boilerplate/issues"
},
"devDependencies": {
"babel-eslint": "^6.0.4",
"babel-preset-es2015-rollup": "^1.1.1",
"eslint": "^2.8.0",
"mocha": "^2.4.5",
"rollup": "^0.26.1",
"rollup-plugin-babel": "^2.4.0",
"rollup-plugin-multi-entry": "^1.2.0",
"source-map-support": "^0.4.0"
},
"dependencies": {
"localforage": ">=1.4.0"
}
}
6 changes: 6 additions & 0 deletions rollup.config.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import config from './rollup.config';

config.format = 'es6';
config.dest = 'dist/localforage-plugin-boilerplate.es6.js';

export default config;
7 changes: 7 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import babel from 'rollup-plugin-babel';

export default {
entry: 'lib/localforage-plugin-boilerplate.js',
// sourceMap: true,
plugins: [babel()]
};
11 changes: 11 additions & 0 deletions rollup.config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import babel from 'rollup-plugin-babel';
import multiEntry from 'rollup-plugin-multi-entry';

export default {
entry: 'test/**/*_test.js',
plugins: [babel(), multiEntry()],
format: 'cjs',
intro: 'require("source-map-support").install();',
dest: 'build/test-bundle.js',
sourceMap: true
};
7 changes: 7 additions & 0 deletions rollup.config.umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config from './rollup.config';

config.format = 'umd';
config.dest = 'dist/localforage-plugin-boilerplate.js';
config.moduleName = 'localforagePluginBoilerplate';

export default config;
Loading

0 comments on commit 7406785

Please sign in to comment.