|
2 | 2 | import postcss from 'postcss' |
3 | 3 | import Tokenizer from 'css-selector-tokenizer' |
4 | 4 |
|
5 | | -const plugin = 'postcss-modules-local-by-default' |
6 | | - |
7 | 5 | function normalizeNodeArray(nodes) { |
8 | 6 | var array = [] |
9 | 7 | nodes.forEach(function(x) { |
@@ -161,44 +159,50 @@ function localizeNode(node, context) { |
161 | 159 | return node |
162 | 160 | } |
163 | 161 |
|
164 | | -module.exports = postcss.plugin(plugin, (options = {}) => css => { |
165 | | - if ( |
166 | | - options.mode && |
167 | | - options.mode !== 'global' && |
168 | | - options.mode !== 'local' && |
169 | | - options.mode !== 'pure' |
170 | | - ) { |
171 | | - throw Error( |
172 | | - 'options.mode must be either "global", "local" or "pure" (default "local")' |
173 | | - ) |
174 | | - } |
175 | | - var pureMode = options.mode === 'pure' |
176 | | - var globalMode = options.mode === 'global' |
177 | | - css.walkRules(function(rule) { |
178 | | - if (rule.parent.type === 'atrule' && /keyframes$/.test(rule.parent.name)) { |
179 | | - // ignore keyframe rules |
180 | | - return |
181 | | - } |
182 | | - var selector = Tokenizer.parse(rule.selector) |
183 | | - var context = { |
184 | | - options: options, |
185 | | - global: globalMode, |
186 | | - hasPureGlobals: false |
187 | | - } |
188 | | - var newSelector |
189 | | - try { |
190 | | - newSelector = localizeNode(selector, context) |
191 | | - } catch (e) { |
192 | | - throw rule.error(e.message) |
193 | | - } |
194 | | - if (pureMode && context.hasPureGlobals) { |
195 | | - throw rule.error( |
196 | | - 'Selector "' + |
197 | | - Tokenizer.stringify(selector) + |
198 | | - '" is not pure ' + |
199 | | - '(pure selectors must contain at least one local class or id)' |
| 162 | +module.exports = postcss.plugin( |
| 163 | + 'postcss-modules-local-by-default', |
| 164 | + (options = {}) => css => { |
| 165 | + if ( |
| 166 | + options.mode && |
| 167 | + options.mode !== 'global' && |
| 168 | + options.mode !== 'local' && |
| 169 | + options.mode !== 'pure' |
| 170 | + ) { |
| 171 | + throw Error( |
| 172 | + 'options.mode must be either "global", "local" or "pure" (default "local")' |
200 | 173 | ) |
201 | 174 | } |
202 | | - rule.selector = Tokenizer.stringify(newSelector) |
203 | | - }) |
204 | | -}) |
| 175 | + var pureMode = options.mode === 'pure' |
| 176 | + var globalMode = options.mode === 'global' |
| 177 | + css.walkRules(function(rule) { |
| 178 | + if ( |
| 179 | + rule.parent.type === 'atrule' && |
| 180 | + /keyframes$/.test(rule.parent.name) |
| 181 | + ) { |
| 182 | + // ignore keyframe rules |
| 183 | + return |
| 184 | + } |
| 185 | + var selector = Tokenizer.parse(rule.selector) |
| 186 | + var context = { |
| 187 | + options: options, |
| 188 | + global: globalMode, |
| 189 | + hasPureGlobals: false |
| 190 | + } |
| 191 | + var newSelector |
| 192 | + try { |
| 193 | + newSelector = localizeNode(selector, context) |
| 194 | + } catch (e) { |
| 195 | + throw rule.error(e.message) |
| 196 | + } |
| 197 | + if (pureMode && context.hasPureGlobals) { |
| 198 | + throw rule.error( |
| 199 | + 'Selector "' + |
| 200 | + Tokenizer.stringify(selector) + |
| 201 | + '" is not pure ' + |
| 202 | + '(pure selectors must contain at least one local class or id)' |
| 203 | + ) |
| 204 | + } |
| 205 | + rule.selector = Tokenizer.stringify(newSelector) |
| 206 | + }) |
| 207 | + } |
| 208 | +) |
0 commit comments