UNPKG

5.17 kBJavaScriptView Raw
1/*!
2 * vue-css-modules v1.2.0
3 * (c) 2018-present fjc0k <fjc0kb@gmail.com> (https://github.com/fjc0k)
4 * Released under the MIT License.
5 */
6'use strict';
7
8function includes(arrayLike, element, fromIndex) {
9 if (fromIndex === void 0) {
10 fromIndex = 0;
11 }
12
13 for (var i = fromIndex, len = arrayLike.length; i < len; i++) {
14 if (arrayLike[i] === element) {
15 return true;
16 }
17 }
18
19 return false;
20}
21function isObject(value) {
22 return value !== null && typeof value === 'object';
23}
24function isFunction(value) {
25 return typeof value === 'function';
26}
27function isString(value) {
28 return typeof value === 'string';
29}
30var camelCaseCache = Object.create(null);
31function camelCase(value) {
32 if (camelCaseCache[value]) return camelCaseCache[value];
33 var result = '';
34 var shouldUpperCase = false;
35
36 for (var i = 0, len = value.length; i < len; i++) {
37 var char = value[i];
38
39 if (char === '-') {
40 shouldUpperCase = true;
41 } else {
42 result += result && shouldUpperCase ? char.toUpperCase() : char;
43 shouldUpperCase = false;
44 }
45 }
46
47 camelCaseCache[value] = result;
48 return result;
49}
50
51var cache = Object.create(null);
52var parseClassExpression = (function (expression) {
53 if (cache[expression]) return cache[expression];
54 var className;
55 var binding;
56 var bindingValue;
57 var role;
58
59 if (includes(expression, '=', 1)) {
60 // eg: disabled=isDisabled
61 var _expression$split = expression.split('=');
62
63 className = _expression$split[0];
64 binding = _expression$split[1];
65 } else {
66 var modifier = expression[0];
67
68 if (modifier === '$') {
69 // eg: $type
70 binding = expression.substr(1);
71 bindingValue = true;
72 } else if (modifier === '@') {
73 // eg: @button
74 className = expression.substr(1);
75 role = className;
76 } else if (modifier === ':') {
77 // eg: :disabled
78 className = expression.substr(1);
79 binding = camelCase(className);
80 } else {
81 className = expression;
82 }
83 }
84
85 cache[expression] = {
86 className: className,
87 binding: binding,
88 bindingValue: bindingValue,
89 role: role
90 };
91 return cache[expression];
92});
93
94var INJECT_ATTR = 'styleName';
95
96/* eslint max-depth: 0 guard-for-in: 0 */
97
98function createElement(_) {
99 var args = [].slice.call(arguments, 1); // for functional component
100
101 if (isFunction(_)) {
102 return createElement.bind(_, {
103 functional: true,
104 createElement: _,
105 styles: args[0],
106 context: args[1]
107 });
108 }
109
110 var _$functional = _.functional,
111 functional = _$functional === void 0 ? false : _$functional,
112 h = _.createElement,
113 _$context = _.context,
114 context = _$context === void 0 ? {} : _$context,
115 _$styles = _.styles,
116 styles = _$styles === void 0 ? context.$style || {} : _$styles;
117
118 if (isString(styles)) {
119 styles = (functional ? (context.injections || {})[styles] : context[styles]) || {};
120 }
121
122 if (functional) {
123 context = context.props || {};
124 }
125
126 var data = args[1];
127
128 if (isObject(data)) {
129 if (!data.staticClass) {
130 data.staticClass = '';
131 }
132
133 if (!data.attrs) {
134 data.attrs = {};
135 }
136
137 var modules = data[INJECT_ATTR] || data.attrs[INJECT_ATTR] || '';
138
139 if (modules.length) {
140 var _modules = Array.isArray(modules) ? modules : [modules];
141
142 for (var i in _modules) {
143 var module = _modules[i];
144
145 if (module && typeof module === 'string') {
146 var classExpressions = module.split(/\s+/g);
147
148 for (var _i in classExpressions) {
149 var classExpression = classExpressions[_i];
150
151 var _parseClassExpression = parseClassExpression(classExpression),
152 className = _parseClassExpression.className,
153 binding = _parseClassExpression.binding,
154 bindingValue = _parseClassExpression.bindingValue,
155 role = _parseClassExpression.role;
156
157 if (bindingValue) {
158 className = context[binding];
159 binding = undefined;
160 }
161
162 if ((binding ? context[binding] : true) && styles[className]) {
163 data.staticClass += " " + styles[className];
164 data.staticClass = data.staticClass.trim();
165 }
166
167 if (role) {
168 data.attrs["data-component-" + role] = '';
169 }
170 }
171 }
172 }
173 } // remove styleName attr
174
175
176 delete data[INJECT_ATTR];
177 delete data.attrs[INJECT_ATTR];
178 }
179
180 return h.apply(null, args);
181}
182
183/* eslint camelcase: 0 */
184
185var CSSModules = function CSSModules(styles) {
186 return {
187 beforeCreate: function beforeCreate() {
188 this.original$createElement = this.original$createElement || this.$createElement;
189 this.original_c = this.original_c || this._c;
190 this.$createElement = createElement.bind(this, {
191 createElement: this.original$createElement,
192 context: this,
193 styles: styles
194 });
195 this._c = createElement.bind(this, {
196 createElement: this.original_c,
197 context: this,
198 styles: styles
199 });
200 }
201 };
202};
203
204CSSModules.install = function (Vue) {
205 Vue.mixin(CSSModules());
206};
207
208module.exports = CSSModules;