Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
peterczg committed May 19, 2024
0 parents commit 87bdfbe
Show file tree
Hide file tree
Showing 20 changed files with 6,159 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: "eslint:recommended",
globals: {
Atomics: "readonly",
SharedArrayBuffer: "readonly"
},
parserOptions: {
ecmaVersion: 2018
},
rules: {
"no-console": 1
}
};
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Node.js CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm test
env:
CI: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
example
.github
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": false
}
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(MIT License)

Copyright (C) 2024 Peter Cheng <peterczg@163.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This project is forked from [postcss-pxtorem](https://github.com/cuth/postcss-pxtorem) under the MIT license.
152 changes: 152 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# postcss-vw2rem

A plugin for [PostCSS](https://github.com/ai/postcss) that generates rem units from pixel units.

This project is forked from [postcss-pxtorem](https://github.com/cuth/postcss-pxtorem) under the MIT license.

## Why

In some tablet or android fold devices, someone may think that arbitrarily scalling the size is inappropriate and that the size needs to be limited. `vw` is directly related to screen width, while `rem` can be restricted.

## Install

```shell
$ npm install postcss postcss-vw2rem --save-dev
```

## Usage

Pixels are the easiest unit to use (*opinion*). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every vw value to a rem from the properties you choose to allow the browser to set the font size.


### Note

`rootValue` below is set to `16`, so you can relate to the examples of `postcss-pxtorem`.

While the default value is set to `10` in real project⚠️.

### Input/Output

*With the default settings, only font related properties are targeted.*

```css
// input
h1 {
margin: 0 0 20vw;
font-size: 32vw;
line-height: 1.2;
letter-spacing: 1vw;
}

// output
h1 {
margin: 0 0 20vw;
font-size: 2rem;
line-height: 1.2;
letter-spacing: 0.0625rem;
}
```

### Example

```js
var fs = require('fs');
var postcss = require('postcss');
var vw2rem = require('postcss-vw2rem');
var css = fs.readFileSync('main.css', 'utf8');
var options = {
replace: false
};
var processedCss = postcss(vw2rem(options)).process(css).css;

fs.writeFile('main-rem.css', processedCss, function (err) {
if (err) {
throw err;
}
console.log('Rem file written.');
});
```

### options

Type: `Object | Null`
Default:
```js
{
rootValue: 16,
unitPrecision: 5,
propList: ['font', 'font-size', 'line-height', 'letter-spacing', 'word-spacing'],
selectorBlackList: [],
replace: true,
mediaQuery: false,
minPixelValue: 0,
exclude: /node_modules/i
}
```

- `rootValue` (Number | Function) Represents the root element font size or returns the root element font size based on the [`input`](https://api.postcss.org/Input.html) parameter
- `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to.
- `propList` (Array) The properties that can change from vw to rem.
- Values need to be exact matches.
- Use wildcard `*` to enable all properties. Example: `['*']`
- Use `*` at the start or end of a word. (`['*position*']` will match `background-position-y`)
- Use `!` to not match a property. Example: `['*', '!letter-spacing']`
- Combine the "not" prefix with the other prefixes. Example: `['*', '!font*']`
- `selectorBlackList` (Array) The selectors to ignore and leave as vw.
- If value is string, it checks to see if selector contains the string.
- `['body']` will match `.body-class`
- If value is regexp, it checks to see if the selector matches the regexp.
- `[/^body$/]` will match `body` but not `.body`
- `replace` (Boolean) Replaces rules containing rems instead of adding fallbacks.
- `mediaQuery` (Boolean) Allow vw to be converted in media queries.
- `minPixelValue` (Number) Set the minimum pixel value to replace.
- `exclude` (String, Regexp, Function) The file path to ignore and leave as vw.
- If value is string, it checks to see if file path contains the string.
- `'exclude'` will match `\project\postcss-vw2rem\exclude\path`
- If value is regexp, it checks to see if file path matches the regexp.
- `/exclude/i` will match `\project\postcss-vw2rem\exclude\path`
- If value is function, you can use exclude function to return a true and the file will be ignored.
- the callback will pass the file path as a parameter, it should returns a Boolean result.
- `function (file) { return file.indexOf('exclude') !== -1; }`
- `unit` (String) Set the default unit to convert, default is `vw`.

### Use with gulp-postcss and autoprefixer

```js
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var vw2rem = require('postcss-vw2rem');

gulp.task('css', function () {

var processors = [
autoprefixer({
browsers: 'last 1 version'
}),
vw2rem({
replace: false
})
];

return gulp.src(['build/css/**/*.css'])
.pipe(postcss(processors))
.pipe(gulp.dest('build/css'));
});
```

### A message about ignoring properties
Currently, the easiest way to have a single property ignored is to use a capital in the pixel unit declaration.

```css
// `vw` is converted to `rem`
.convert {
font-size: 16vw; // converted to 1rem
}

// `Vx` or `VW` is ignored by `postcss-vw2rem` but still accepted by browsers
.ignore {
border: 1vw solid; // ignored
border-width: 2vw; // ignored
}
```
5 changes: 5 additions & 0 deletions example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
"no-console": 0
}
};
18 changes: 18 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

const fs = require("fs");
const postcss = require("postcss");
const pxtorem = require("..");

const css = fs.readFileSync("main.css", "utf8");
const options = {
replace: false
};
const processedCss = postcss(pxtorem(options)).process(css).css;

fs.writeFile("main-rem.css", processedCss, function(err) {
if (err) {
throw err;
}
console.log("Rem file written.");
});
28 changes: 28 additions & 0 deletions example/main-rem.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.class {
margin: -10px .5em;
padding: 5rem .5px;
border: 3px solid black;
font-size: 14px;
font-size: 0.875rem;
line-height: 20px;
line-height: 1.25rem;
}
.class2 {
font-size: 32px;
font-size: 2rem;
line-height: 1em;
}
@media (min-width: 750px) {
.class3 {
font-size: 16px;
font-size: 1rem;
line-height: 22px;
line-height: 1.375rem;
}
}

/*
.class {
font-size: 16px;
}
*/
24 changes: 24 additions & 0 deletions example/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.class {
margin: -10px .5em;
padding: 5rem .5px;
border: 3px solid black;
font-size: 14px;
line-height: 20px;
}
.class2 {
font-size: 32px;
font-size: 2rem;
line-height: 1em;
}
@media (min-width: 750px) {
.class3 {
font-size: 16px;
line-height: 22px;
}
}

/*
.class {
font-size: 16px;
}
*/
Loading

0 comments on commit 87bdfbe

Please sign in to comment.