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