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
9 changed files
with
166 additions
and
1 deletion.
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
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
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,53 @@ | ||
# @unocss/preset-rem-to-px | ||
|
||
Coverts rem to px for utils. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i -D @unocss/preset-rem-to-px | ||
``` | ||
|
||
```ts | ||
import presetUno from '@unocss/preset-uno' | ||
import presetRemToPx from '@unocss/preset-rem-to-px' | ||
|
||
Unocss({ | ||
presets: [ | ||
presetUno(), | ||
presetRemToPx() | ||
], | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
```html | ||
<div class="m-2"></div> | ||
``` | ||
|
||
<table><tr><td width="500px" valign="top"> | ||
|
||
### without | ||
|
||
```css | ||
.m-2 { | ||
margin: 0.5rem; | ||
} | ||
``` | ||
|
||
</td><td width="500px" valign="top"> | ||
|
||
### with | ||
|
||
```css | ||
.m-2 { | ||
margin: 8px; | ||
} | ||
``` | ||
|
||
</td></tr></table> | ||
|
||
## License | ||
|
||
MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu) |
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,12 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
rollup: { | ||
emitCJS: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@unocss/preset-rem-to-px", | ||
"version": "0.36.0", | ||
"description": "Convert all rem to px in utils", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"license": "MIT", | ||
"funding": "https://github.com/sponsors/antfu", | ||
"homepage": "https://github.com/unocss/unocss/tree/main/packages/preset-rem-to-px#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/unocss/unocss.git", | ||
"directory": "packages/preset-rem-to-px" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/unocss/unocss/issues" | ||
}, | ||
"keywords": [ | ||
"unocss", | ||
"unocss-preset" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "workspace:*" | ||
} | ||
} |
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,28 @@ | ||
import type { Preset } from '@unocss/core' | ||
|
||
const remRE = /^-?[\.\d]+rem$/ | ||
|
||
export interface RemToPxOptions { | ||
/** | ||
* 1rem = n px | ||
* @default 16 | ||
*/ | ||
baseFontSize?: number | ||
} | ||
|
||
export default (options: RemToPxOptions = {}): Preset => { | ||
const { | ||
baseFontSize = 16, | ||
} = options | ||
|
||
return { | ||
name: '@unocss/preset-rem-to-px', | ||
postprocess: (util) => { | ||
util.entries.forEach((i) => { | ||
const value = i[1] | ||
if (value && typeof value === 'string' && remRE.test(value)) | ||
i[1] = `${+value.slice(0, -3) * baseFontSize}px` | ||
}) | ||
}, | ||
} | ||
} |
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,26 @@ | ||
import { createGenerator } from '@unocss/core' | ||
import { describe, expect, test } from 'vitest' | ||
import presetRemToPx from '@unocss/preset-rem-to-px' | ||
import presetMini from '@unocss/preset-mini' | ||
|
||
describe('rem-to-px', () => { | ||
const uno = createGenerator({ | ||
presets: [ | ||
presetMini(), | ||
presetRemToPx(), | ||
], | ||
}) | ||
|
||
test('should works', async () => { | ||
expect((await uno.generate( | ||
new Set(['m4', 'mx2', '-p2', 'gap2']), | ||
{ preflights: false })).css) | ||
.toMatchInlineSnapshot(` | ||
"/* layer: default */ | ||
.-p2{padding:-8px;} | ||
.m4{margin:16px;} | ||
.mx2{margin-left:8px;margin-right:8px;} | ||
.gap2{grid-gap:8px;gap:8px;}" | ||
`) | ||
}) | ||
}) |
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