Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat: upgrade to PostCSS 8
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Nov 7, 2021
1 parent ad1746f commit 3bd85bc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
package-lock.json
yarn.lock
.*
!.appveyor.yml
!.editorconfig
Expand All @@ -8,3 +9,4 @@ package-lock.json
!.travis.yml
*.log*
*.result.css
.eslintcache
6 changes: 2 additions & 4 deletions .tape.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
'postcss-media-fn': {
'basic': {
message: 'supports basic usage'
}
'basic': {
message: 'supports basic usage'
}
};
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
language: node_js

node_js:
- 4
- 10
- 12

install:
- npm install --ignore-scripts
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ h1 {
Add [Media()] to your build tool:

```bash
npm install postcss-media-fn --save-dev
npm install postcss postcss-media-fn --save-dev
```

#### Node
Expand Down
57 changes: 35 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
// tooling
const postcss = require('postcss');
const parser = require('postcss-value-parser');
const postcss = require("postcss");
const {parse} = require('postcss-values-parser');

// local tooling
const isLikelyMediaFn = (node) => node.type === 'decl' && (/media\([^\)]+\)/).test(node.value);
const isMediaFn = (node) => node.type === 'function' && node.value === 'media';
const mediaFnRegExp = /media\([^)]+\)/i;
const isMediaFn = (node) => node.name === 'media';
const isResponsiveValue = (value) => value.length > 1;
const isNonResponsiveValue = (value) => value.length === 1;

// plugin
module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
css.walkRules(
(rule) => {
/**
* Use `media()` to assign responsive values.
* @returns {import('postcss').Plugin}
*/
module.exports = function creator() {
return {
postcssPlugin: 'postcss-media-fn',
Rule(rule) {
const newAtRules = [];

Array.prototype.filter.call(rule.nodes, isLikelyMediaFn).forEach(
rule.walkDecls(
(decl) => {
const newValue = parser(decl.value).walk(
if (!mediaFnRegExp.test(decl.value)) {
return;
}

const valueAST = parse(decl.value);
valueAST.walkFuncs(
(node) => {
if (isMediaFn(node)) {
// all values
Expand All @@ -28,9 +36,8 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
values.push([]);
} else {
// otherwise, assign the stringified node to the last values sub-group
values[values.length - 1].push(parser.stringify(childNode));
values[values.length - 1].push(childNode.raws.before + childNode.toString() + childNode.raws.after);
}

return values;
},
[[]]
Expand All @@ -45,11 +52,14 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
// for each responsive value
responsiveValues.forEach(
(value) => {
const prop = value.pop().trim()
const media = value.join('').trim()

// add new @media at-rule, rule, and declaration to list
newAtRules.push(
postcss.atRule({
name: 'media',
params: value.shift().trim(),
params: media,
source: decl.source
}).append(
rule.clone({
Expand All @@ -58,7 +68,7 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
}
}).removeAll().append(
decl.clone({
value: value.join('').trim(),
value: prop,
raws: {}
})
)
Expand All @@ -71,15 +81,16 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
if (nonResponsiveValues.length) {
// re-assign the first non-responsive value to the declaration
node.type = 'word';
node.value = nonResponsiveValues.shift().join('');
node.value = nonResponsiveValues.join('');
} else {
// otherwise, remove the node
node.type = 'word';
node.value = '';
node.remove()
}
}
}
).toString();
);

const newValue = valueAST.toString()

// if the value has changed
if (decl.value !== newValue) {
Expand All @@ -89,7 +100,7 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
decl.remove();
} else {
// otherwise, update the declaration value
decl.value = newValue;
decl.value = newValue.trim();
}
}
}
Expand All @@ -103,5 +114,7 @@ module.exports = postcss.plugin('postcss-media-fn', () => (css) => {
}
}
}
);
});
}
}

module.exports.postcss = true;
38 changes: 21 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,42 @@
"description": "Use `media()` to assign responsive values",
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
"repository": "jonathantneal/postcss-media-fn",
"homepage": "https://github.com/jonathantneal/postcss-media-fn#readme",
"bugs": "https://github.com/jonathantneal/postcss-media-fn/issues",
"repository": "csstools/postcss-media-fn",
"homepage": "https://github.com/csstools/postcss-media-fn#readme",
"bugs": "https://github.com/csstools/postcss-media-fn/issues",
"main": "index.js",
"files": [
"example.js",
"index.js"
],
"scripts": {
"clean": "git clean -X -d -f",
"prepublish": "npm test",
"test": "echo 'Running tests...'; npm run test:js && npm run test:tape",
"test:js": "eslint *.js --cache --ignore-pattern .gitignore",
"test:tape": "postcss-tape"
"lint:js": "eslint . --cache",
"lint": "npm lint:js",
"pretest": "npm lint",
"test": "postcss-tape",
"prepublishOnly": "npm test"
},
"engines": {
"node": ">=4.0.0"
"node": ">=12"
},
"peerDependencies": {
"postcss": "^8.3"
},
"dependencies": {
"postcss": "^6.0.11",
"postcss-value-parser": "^3.3.0"
"postcss-values-parser": "^6.0.1"
},
"devDependencies": {
"eslint": "^4.6.1",
"eslint-config-dev": "2.0.0",
"postcss-tape": "2.0.1",
"pre-commit": "^1.2.2"
"eslint": "7.32.0",
"postcss": "8.3.11",
"postcss-tape": "6.0.1"
},
"eslintConfig": {
"extends": "dev"
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended"
},
"runkitExampleFilename": "example.js",
"keywords": [
"postcss",
"css",
Expand Down

0 comments on commit 3bd85bc

Please sign in to comment.