Skip to content

Commit

Permalink
fix: add more tolerance for query match (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
SassNinja authored Sep 10, 2020
1 parent ccd3c6b commit bf53898
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const postcss = require('postcss');
const store = require('./store');
const normalize = require('./utils/normalize');

module.exports = postcss.plugin('MediaQueryPostCSS', options => {

Expand Down Expand Up @@ -35,11 +36,21 @@ module.exports = postcss.plugin('MediaQueryPostCSS', options => {
}
}

function getQueryName(query) {
const queries = Object.keys(options.queries);

for (let i = 0; i < queries.length; i++) {
if (normalize(query) === normalize(queries[i])) {
return options.queries[queries[i]];
}
}
}

return (css, result) => {

css.walkAtRules('media', atRule => {

const queryname = options.queries[atRule.params];
const queryname = getQueryName(atRule.params);

if (queryname) {
const groupName = getGroupName(options.basename);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Utility to normalize a query for more tolerant comparison.
// normalize('@media (min-width: 1000px)') === normalize('@media(min-width:1000px)')

module.exports = function normalize(query) {
return query.toLowerCase().replace(/\s/g, '');
}

0 comments on commit bf53898

Please sign in to comment.