Skip to content

Commit bfa303f

Browse files
committed
Now use babel to compile library before publishing to npm
1 parent a0b6e98 commit bfa303f

File tree

5 files changed

+388
-133
lines changed

5 files changed

+388
-133
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
lib/

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/

lib/vue-plugin.js

Lines changed: 167 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,198 @@
1-
const omit = require('lodash.omit');
1+
'use strict';
22

3-
let apolloClient = null;
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
44

5-
class DollarApollo {
6-
constructor(vm) {
7-
this.vm = vm;
8-
this.querySubscriptions = {};
9-
}
5+
var _lodash = require('lodash.omit');
106

11-
get client() {
12-
return apolloClient;
13-
}
7+
var _lodash2 = _interopRequireDefault(_lodash);
148

15-
get query() {
16-
return this.client.query;
17-
}
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1810

19-
watchQuery(options) {
20-
const vm = this.vm;
21-
const observable = this.client.watchQuery(options);
22-
const _subscribe = observable.subscribe.bind(observable);
23-
observable.subscribe = (function(options) {
24-
let sub = _subscribe(options);
25-
vm._apolloSubscriptions.push(sub);
26-
return sub;
27-
}).bind(observable);
28-
return observable;
29-
}
11+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3012

31-
get mutate() {
32-
return this.client.mutate;
33-
}
13+
var apolloClient = null;
3414

35-
option(key, options) {
36-
const vm = this.vm;
37-
const $apollo = this;
15+
var DollarApollo = function () {
16+
function DollarApollo(vm) {
17+
_classCallCheck(this, DollarApollo);
3818

39-
let query, observer, sub;
19+
this.vm = vm;
20+
this.querySubscriptions = {};
21+
}
4022

41-
let firstLoadingDone = false;
23+
_createClass(DollarApollo, [{
24+
key: 'watchQuery',
25+
value: function watchQuery(options) {
26+
var vm = this.vm;
27+
var observable = this.client.watchQuery(options);
28+
var _subscribe = observable.subscribe.bind(observable);
29+
observable.subscribe = function (options) {
30+
var sub = _subscribe(options);
31+
vm._apolloSubscriptions.push(sub);
32+
return sub;
33+
}.bind(observable);
34+
return observable;
35+
}
36+
}, {
37+
key: 'option',
38+
value: function option(key, options) {
39+
var vm = this.vm;
40+
var $apollo = this;
4241

43-
let loadingKey = options.loadingKey;
44-
let loadingChangeCb = options.watchLoading;
42+
var query = void 0,
43+
observer = void 0,
44+
sub = void 0;
4545

46-
if (typeof loadingChangeCb === 'function') {
47-
loadingChangeCb = loadingChangeCb.bind(vm);
48-
}
46+
var firstLoadingDone = false;
4947

50-
// Simple query
51-
if (!options.query) {
52-
query = options;
53-
}
48+
var loadingKey = options.loadingKey;
49+
var loadingChangeCb = options.watchLoading;
5450

55-
function generateApolloOptions(variables) {
56-
const apolloOptions = omit(options, [
57-
'variables',
58-
'watch',
59-
'update',
60-
'result',
61-
'error',
62-
'loadingKey',
63-
'watchLoading',
64-
]);
65-
apolloOptions.variables = variables;
66-
return apolloOptions;
67-
}
51+
if (typeof loadingChangeCb === 'function') {
52+
loadingChangeCb = loadingChangeCb.bind(vm);
53+
}
6854

69-
function q(variables) {
70-
applyLoadingModifier(1);
55+
// Simple query
56+
if (!options.query) {
57+
query = options;
58+
}
7159

72-
if (options.forceFetch && observer) {
73-
// Refresh query
74-
observer.refetch(variables, {
75-
forceFetch: !!options.forceFetch
76-
});
77-
} else {
78-
if (sub) {
79-
sub.unsubscribe();
80-
}
60+
function generateApolloOptions(variables) {
61+
var apolloOptions = (0, _lodash2.default)(options, ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading']);
62+
apolloOptions.variables = variables;
63+
return apolloOptions;
64+
}
8165

82-
// Create observer
83-
observer = $apollo.watchQuery(generateApolloOptions(variables));
66+
function q(variables) {
67+
applyLoadingModifier(1);
68+
69+
if (options.forceFetch && observer) {
70+
// Refresh query
71+
observer.refetch(variables, {
72+
forceFetch: !!options.forceFetch
73+
});
74+
} else {
75+
if (sub) {
76+
sub.unsubscribe();
77+
}
78+
79+
// Create observer
80+
observer = $apollo.watchQuery(generateApolloOptions(variables));
81+
82+
// Create subscription
83+
sub = observer.subscribe({
84+
next: nextResult,
85+
error: catchError
86+
});
87+
}
88+
}
8489

85-
// Create subscription
86-
sub = observer.subscribe({
87-
next: nextResult,
88-
error: catchError
90+
if (typeof options.variables === 'function') {
91+
vm.$watch(options.variables.bind(vm), q, {
92+
immediate: true
8993
});
94+
} else {
95+
q(options.variables);
9096
}
91-
}
9297

93-
if (typeof options.variables === 'function') {
94-
vm.$watch(options.variables.bind(vm), q, {
95-
immediate: true
96-
});
97-
} else {
98-
q(options.variables);
99-
}
98+
function nextResult(_ref) {
99+
var data = _ref.data;
100100

101-
function nextResult({ data }) {
102-
applyData(data);
103-
}
101+
applyData(data);
102+
}
104103

105-
function applyData(data) {
106-
loadingDone();
104+
function applyData(data) {
105+
loadingDone();
107106

108-
if (typeof options.update === 'function') {
109-
vm[key] = options.update.call(vm, data);
110-
} else if (data[key] === undefined) {
111-
console.error(`Missing ${key} attribute on result`, data);
112-
} else {
113-
vm[key] = data[key];
114-
}
107+
if (typeof options.update === 'function') {
108+
vm[key] = options.update.call(vm, data);
109+
} else if (data[key] === undefined) {
110+
console.error('Missing ' + key + ' attribute on result', data);
111+
} else {
112+
vm[key] = data[key];
113+
}
115114

116-
if (typeof options.result === 'function') {
117-
options.result.call(vm, data);
115+
if (typeof options.result === 'function') {
116+
options.result.call(vm, data);
117+
}
118118
}
119-
}
120119

121-
function applyLoadingModifier(value) {
122-
if (loadingKey) {
123-
vm[loadingKey] += value;
124-
}
120+
function applyLoadingModifier(value) {
121+
if (loadingKey) {
122+
vm[loadingKey] += value;
123+
}
125124

126-
if (loadingChangeCb) {
127-
loadingChangeCb(value === 1, value);
125+
if (loadingChangeCb) {
126+
loadingChangeCb(value === 1, value);
127+
}
128128
}
129-
}
130129

131-
function loadingDone() {
132-
if (!firstLoadingDone) {
133-
applyLoadingModifier(-1);
134-
firstLoadingDone = true;
130+
function loadingDone() {
131+
if (!firstLoadingDone) {
132+
applyLoadingModifier(-1);
133+
firstLoadingDone = true;
134+
}
135135
}
136-
}
137-
138-
function catchError(error) {
139-
loadingDone();
140136

141-
if (error.graphQLErrors && error.graphQLErrors.length !== 0) {
142-
console.error(`GraphQL execution errors for query ${query}`);
143-
for (let e of error.graphQLErrors) {
144-
console.error(e);
137+
function catchError(error) {
138+
loadingDone();
139+
140+
if (error.graphQLErrors && error.graphQLErrors.length !== 0) {
141+
console.error('GraphQL execution errors for query ' + query);
142+
var _iteratorNormalCompletion = true;
143+
var _didIteratorError = false;
144+
var _iteratorError = undefined;
145+
146+
try {
147+
for (var _iterator = error.graphQLErrors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
148+
var e = _step.value;
149+
150+
console.error(e);
151+
}
152+
} catch (err) {
153+
_didIteratorError = true;
154+
_iteratorError = err;
155+
} finally {
156+
try {
157+
if (!_iteratorNormalCompletion && _iterator.return) {
158+
_iterator.return();
159+
}
160+
} finally {
161+
if (_didIteratorError) {
162+
throw _iteratorError;
163+
}
164+
}
165+
}
166+
} else if (error.networkError) {
167+
console.error('Error sending the query ' + query, error.networkError);
168+
} else {
169+
console.error(error);
145170
}
146-
} else if (error.networkError) {
147-
console.error(`Error sending the query ${query}`, error.networkError);
148-
} else {
149-
console.error(error);
150-
}
151171

152-
if (typeof options.error === 'function') {
153-
options.error(error);
172+
if (typeof options.error === 'function') {
173+
options.error(error);
174+
}
154175
}
155176
}
156-
}
157-
}
177+
}, {
178+
key: 'client',
179+
get: function get() {
180+
return apolloClient;
181+
}
182+
}, {
183+
key: 'query',
184+
get: function get() {
185+
return this.client.query;
186+
}
187+
}, {
188+
key: 'mutate',
189+
get: function get() {
190+
return this.client.mutate;
191+
}
192+
}]);
193+
194+
return DollarApollo;
195+
}();
158196

159197
function prepare() {
160198
this._apolloSubscriptions = [];
@@ -163,15 +201,13 @@ function prepare() {
163201
}
164202

165203
function launch() {
166-
let apollo = this.$options.apollo;
204+
var apollo = this.$options.apollo;
167205

168206
if (apollo) {
169-
const queries = omit(apollo, [
170-
'subscribe',
171-
]);
207+
var queries = (0, _lodash2.default)(apollo, ['subscribe']);
172208

173209
// watchQuery
174-
for (let key in queries) {
210+
for (var key in queries) {
175211
this.$apollo.option(key, queries[key]);
176212
}
177213

@@ -183,7 +219,7 @@ function launch() {
183219
}
184220

185221
module.exports = {
186-
install(Vue, options) {
222+
install: function install(Vue, options) {
187223

188224
apolloClient = options.apolloClient;
189225

@@ -196,8 +232,8 @@ module.exports = {
196232

197233
created: launch,
198234

199-
destroyed: function() {
200-
this._apolloSubscriptions.forEach((sub) => {
235+
destroyed: function destroyed() {
236+
this._apolloSubscriptions.forEach(function (sub) {
201237
sub.unsubscribe();
202238
});
203239
this._apolloSubscriptions = null;
@@ -207,6 +243,5 @@ module.exports = {
207243
}
208244

209245
});
210-
211246
}
212-
};
247+
};

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"name": "vue-apollo",
3-
"version": "1.0.0-beta10",
3+
"version": "1.0.0-beta11",
44
"description": "Vue apollo integration",
55
"main": "index.js",
66
"scripts": {
7+
"compile": "babel --presets es2015 -d lib/ src/",
8+
"prepublish": "npm run compile",
79
"test": "echo \"Error: no test specified\" && exit 1"
810
},
911
"repository": {
@@ -26,5 +28,9 @@
2628
},
2729
"dependencies": {
2830
"lodash.omit": "^4.5.0"
31+
},
32+
"devDependencies": {
33+
"babel-cli": "^6.14.0",
34+
"babel-preset-es2015": "^6.14.0"
2935
}
3036
}

0 commit comments

Comments
 (0)