diff --git a/src/index.js b/src/index.js index dae72bd..f1c2758 100644 --- a/src/index.js +++ b/src/index.js @@ -4,13 +4,13 @@ const METHOD = 'map-get'; const valResolve = value => { const map = value.substring( - value.indexOf('((') + 2, - value.indexOf('),') - ) + value.indexOf('((') + 2, + value.indexOf('),') + ) .split(',') .reduce((map, string) => { const [key, value] = string.split(':'); - return Object.assign(map, {[key]: value}) + return Object.assign(map, {[key]: value}); }, {}); const key = value.substring( @@ -19,18 +19,25 @@ const valResolve = value => { ); return map[key]; -} +}; const normalize = value => value.replace(/(\s|!default)/g, ''); export default postcss.plugin('postcss-map-get', () => { - return nodes => { - nodes.walkDecls(decl => { - let {value} = decl; - - if (value.startsWith(METHOD)) { - decl.value = valResolve(normalize(decl.value)); - } - }); - }; + return nodes => { + nodes.walkDecls(decl => { + let {value} = decl; + + if (value.startsWith(METHOD)) { + decl.value = valResolve(normalize(decl.value)); + } + }); + + nodes.walkAtRules(rules => { + const {params} = rules; + const start = params.indexOf(METHOD); + const end = params.lastIndexOf('))'); + rules.params = `${params.substr(0, start)}${valResolve(normalize(params.substring(start, end)))}${params.substr(end + 1)}`; + }); + }; }); diff --git a/test/test.js b/test/test.js index 0809653..ca8af6c 100644 --- a/test/test.js +++ b/test/test.js @@ -3,18 +3,24 @@ import test from 'ava'; import plugin from '../src'; const processing = (input, opts) => { - return postcss([plugin(opts)]).process(input).css; + return postcss([plugin(opts)]).process(input).css; }; test('it should return body color for background', t => { - const expected = 'body{background: #fff;}'; - const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);}'; - t.is(processing(value), expected); + const expected = 'body{background: #fff;}'; + const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);}'; + t.is(processing(value), expected); }); test('it should return body color and min-width width fro decl', t => { - const expected = 'body{background: #fff;min-width: 1280px;}'; - const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg);}'; - t.is(processing(value), expected); + const expected = 'body{background: #fff;min-width: 1280px;}'; + const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg);}'; + t.is(processing(value), expected); +}); + +test('it should retunr width for at rule @media', t => { + const expected = '@media (min-width: 1280px) {body {overflow-x: hidden}}'; + const value = '@media (min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg))) {body {overflow-x: hidden}}'; + t.is(processing(value), expected); });