-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Declaration } from 'postcss'; | ||
const unitsConverters = require('./converters'); | ||
|
||
const rem = unitsConverters.rem; | ||
|
||
module.exports = () => { | ||
return { | ||
postcssPlugin: 'postcss-auto-rem', | ||
Declaration: (decl: Declaration) => { | ||
if (!decl.value.includes('px')) { | ||
return; | ||
} | ||
|
||
decl.value = rem(decl.value); | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports.postcss = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`auto-rem it transforms input with rem function correctly 1`] = ` | ||
" | ||
.demo { | ||
font-size: calc(1rem * var(--mantine-scale)); | ||
border: calc(0.0625rem * var(--mantine-scale)) solid red; | ||
padding: calc(1rem * var(--mantine-scale)); | ||
} | ||
" | ||
`; | ||
|
||
exports[`auto-rem it transforms media query correctly 1`] = ` | ||
" | ||
@media (min-width: 320px) { | ||
.demo { | ||
font-size: calc(2rem * var(--mantine-scale)) | ||
} | ||
} | ||
" | ||
`; | ||
|
||
exports[`auto-rem it transforms px to rem values correctly 1`] = ` | ||
" | ||
.demo { | ||
font-size: calc(1rem * var(--mantine-scale)); | ||
height: calc(2.5rem * var(--mantine-scale)); | ||
padding: calc(0.625rem * var(--mantine-scale)) calc(3.125rem * var(--mantine-scale)) calc(1.25rem * var(--mantine-scale)) calc(1.875rem * var(--mantine-scale)); | ||
background: red; | ||
border: calc(0.0625rem * var(--mantine-scale)) solid black; | ||
box-shadow: 0 0 calc(0.625rem * var(--mantine-scale)) calc(0.3125rem * var(--mantine-scale)) rgba(0, 0, 0, 0.5); | ||
} | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { testTransform } from './utils'; | ||
|
||
const baseInput = ` | ||
.demo { | ||
font-size: 16px; | ||
height: 40px; | ||
padding: 10px 50px 20px 30px; | ||
background: red; | ||
border: 1px solid black; | ||
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.5); | ||
} | ||
`; | ||
|
||
const remFunctionInput = ` | ||
.demo { | ||
font-size: rem(16px); | ||
border: rem(1px) solid red; | ||
padding: 16px; | ||
} | ||
`; | ||
|
||
const mediaInput = ` | ||
.demo { | ||
@media (min-width: 320px) { | ||
font-size: 32px; | ||
} | ||
} | ||
`; | ||
|
||
describe('auto-rem', () => { | ||
it('it transforms px to rem values correctly', async () => { | ||
const res = await testTransform(baseInput, { autoRem: true }); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
|
||
it('it transforms input with rem function correctly', async () => { | ||
const res = await testTransform(remFunctionInput, { autoRem: true }); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
|
||
it('it transforms media query correctly', async () => { | ||
const res = await testTransform(mediaInput, { autoRem: true }); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import postcss from 'postcss'; | ||
const preset = require('../preset'); | ||
|
||
export function testTransform(input: string) { | ||
return postcss([preset]).process(input, { from: undefined }); | ||
export function testTransform(input: string, options?: Record<string, any>) { | ||
return postcss([preset(options)]).process(input, { from: undefined }); | ||
} |