-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvue-script2.js
180 lines (142 loc) · 5.84 KB
/
vue-script2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*!
* vue-script2 v2.1.0
* (c) 2016-2019 Greg Slepak
* @license MIT License
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.VueScript2 = factory());
}(this, function () { 'use strict';
var Script2 = {
installed: false,
p: Promise.resolve(),
version: '2.1.0',
// grunt will overwrite to match package.json
loaded: {},
// keys are the scripts that is loading or loaded, values are promises
install(Vue) {
if (Script2.installed) return;
var customAttrs = ['unload']; // from: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
// 'async' and 'defer' don't allow document.write according to:
// http://www.html5rocks.com/en/tutorials/speed/script-loading/
// we ignore 'defer' and handle 'async' specially.
var props = customAttrs.concat(['src', 'type', 'async', 'integrity', 'text', 'crossorigin']);
Vue.component('script2', {
props: props,
// <slot> is important, see: http://vuejs.org/guide/components.html#Named-Slots
// template: '<div style="display:none"><slot></slot></div>',
// NOTE: Instead of using `template` we can use the `render` function like so:
render(h) {
return h('div', {
style: 'display:none'
}, this.$slots.default);
},
mounted() {
var parent = this.$el.parentElement;
if (!this.src) {
Script2.p = Script2.p.then(() => {
var s = document.createElement('script');
var h = this.$el.innerHTML;
h = h.replace(/</gi, '<').replace(/>/gi, '>').replace(/&/gi, '&');
s.type = 'text/javascript';
s.appendChild(document.createTextNode(h));
parent.appendChild(s);
this.$emit('loaded'); // any other proper way to do this or emit error?
});
} else {
var opts = _.omitBy(_.pick(this, props), _.isUndefined);
opts.parent = parent; // this syntax results in an implicit return
var load = () => Script2.load(this.src, opts).then(() => this.$emit('loaded'), err => this.$emit('error', err));
_.isUndefined(this.async) || this.async === 'false' ? Script2.p = Script2.p.then(load) // serialize execution
: load(); // inject immediately
} // see: https://vuejs.org/v2/guide/migration.html#ready-replaced
this.$nextTick(() => {
// code that assumes this.$el is in-document
// NOTE: we could've done this.$el.remove(), but IE sucks, see:
// https://github.com/taoeffect/vue-script2/pull/17
this.$el.parentElement.removeChild(this.$el); // remove dummy template <div>
});
},
destroyed() {
if (this.unload) {
new Function(this.unload)(); // eslint-disable-line
delete Script2.loaded[this.src];
}
}
});
Script2.installed = true;
},
load(src) {
let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
parent: document.head,
attributes: []
};
if (!Script2.loaded[src]) {
Script2.loaded[src] = new Promise((resolve, reject) => {
var s = document.createElement('script'); // omit the special options that Script2 supports
_.defaults2(s, _.omit(opts, ['unload', 'parent']), {
type: 'text/javascript'
}); // according to: http://www.html5rocks.com/en/tutorials/speed/script-loading/
// async does not like 'document.write' usage, which we & vue.js make
// heavy use of based on the SPA style. Also, async can result
// in code getting executed out of order from how it is inlined on the page.
s.async = false; // therefore set this to false
s.src = src; // crossorigin in HTML and crossOrigin in the DOM per HTML spec
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-crossorigin
if(opts.attributes.length > 0)
opts.attributes.forEach(element => {
s.setAttribute(element.key,element.value)
});
if (opts.crossorigin) {
s.crossOrigin = opts.crossorigin;
} // inspiration from: https://github.com/eldargab/load-script/blob/master/index.js
// and: https://github.com/ded/script.js/blob/master/src/script.js#L70-L82
s.onload = () => resolve(src); // IE should now support onerror and onload. If necessary, take a look
// at this to add older IE support: http://stackoverflow.com/a/4845802/1781435
s.onerror = () => reject(new Error(src));
opts.parent.appendChild(s);
});
}
return Script2.loaded[src];
}
};
var _ = {
isUndefined(x) {
return x === undefined;
},
pick(o, props) {
var x = {};
props.forEach(k => {
x[k] = o[k];
});
return x;
},
omit(o, props) {
var x = {};
Object.keys(o).forEach(k => {
if (props.indexOf(k) === -1) x[k] = o[k];
});
return x;
},
omitBy(o, pred) {
var x = {};
Object.keys(o).forEach(k => {
if (!pred(o[k])) x[k] = o[k];
});
return x;
},
// custom defaults function suited to our specific purpose
defaults2(o) {
for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
sources[_key - 1] = arguments[_key];
}
sources.forEach(s => {
Object.keys(s).forEach(k => {
if (_.isUndefined(o[k]) || o[k] === '') o[k] = s[k];
});
});
}
};
return Script2;
}));