Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 0b6b738

Browse files
authored
feat: upgrade to PostCSS 8 (#11)
1 parent 7f31fda commit 0b6b738

10 files changed

+69
-67
lines changed

.rollup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import pkg from './package.json'
22

33
export default {
44
...pkg.rollup,
5-
plugins: pkg.rollup.plugins.map(plugin => require(plugin)()),
5+
plugins: pkg.rollup.plugins.map(plugin => require(plugin).default()),
66
onwarn(warning, warn) {
77
if (warning.code !== 'UNRESOLVED_IMPORT') warn(warning)
88
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Add [PostCSS Environment Variables] to your project:
3636

3737
```bash
38-
npm install postcss-env-function --save-dev
38+
npm install postcss postcss-env-function --save-dev
3939
```
4040

4141
Use [PostCSS Environment Variables] to process your CSS:

package.json

+17-14
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@
1717
"build:watch": "npx rollup -c .rollup.js --watch",
1818
"lint": "npx eslint --cache src",
1919
"lint:fix": "npx eslint --cache --fix",
20-
"pretest": "npm install && npm run build",
20+
"pretest": "npm run build",
2121
"test": "npm run lint && npm run tape",
22-
"tape": "npx postcss-tape",
22+
"tape": "postcss-tape",
2323
"prepublishOnly": "npm test"
2424
},
2525
"engines": {
26-
"node": ">=8.0.0"
26+
"node": ">=10.0.0"
2727
},
2828
"dependencies": {
29-
"postcss": "^7.0.27",
30-
"postcss-values-parser": "^3.2.0"
29+
"postcss-values-parser": "^5.0.0"
30+
},
31+
"peerDependencies": {
32+
"postcss": "^8.0.0"
3133
},
3234
"devDependencies": {
33-
"@babel/core": "^7.9.0",
34-
"@babel/preset-env": "^7.9.5",
35-
"babel-eslint": "^10.1.0",
36-
"eslint": "^6.8.0",
37-
"postcss-tape": "^5.0.2",
35+
"@babel/core": "^7.13.10",
36+
"@babel/preset-env": "^7.13.10",
37+
"@babel/eslint-parser": "^7.13.10",
38+
"eslint": "^7.22.0",
39+
"postcss": "^8.2.8",
40+
"postcss-tape": "^6.0.0",
3841
"pre-commit": "^1.2.2",
39-
"rollup": "^2.7.2",
40-
"rollup-plugin-babel": "^4.4.0"
42+
"rollup": "^2.41.2",
43+
"@rollup/plugin-babel": "^5.3.0"
4144
},
4245
"babel": {
4346
"presets": [
@@ -55,12 +58,12 @@
5558
"node": true
5659
},
5760
"extends": "eslint:recommended",
58-
"parser": "babel-eslint"
61+
"parser": "@babel/eslint-parser"
5962
},
6063
"rollup": {
6164
"input": "src/index.js",
6265
"plugins": [
63-
"rollup-plugin-babel"
66+
"@rollup/plugin-babel"
6467
],
6568
"output": [
6669
{

src/index.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import postcss from 'postcss';
21
import getReplacedValue from './lib/get-replaced-value';
3-
import getSupportedValue from './lib/get-supported-value';
4-
import setSupportedValue from './lib/set-supported-value';
52
import importEnvironmentVariablesFromSources from './lib/import-from';
63

7-
export default postcss.plugin('postcss-env-fn', opts => {
4+
/**
5+
* @param {{importFrom?: string[]}} opts
6+
* @returns {import('postcss').Plugin}
7+
*/
8+
module.exports = function creator(opts) {
89
// sources to import environment variables from
910
const importFrom = [].concat(Object(opts).importFrom || []);
1011

1112
// promise any environment variables are imported
1213
const environmentVariablesPromise = importEnvironmentVariablesFromSources(importFrom);
1314

14-
return async root => {
15-
const environmentVariables = await environmentVariablesPromise;
15+
return {
16+
postcssPlugin: 'postcss-env-fn',
17+
async AtRule(atRule) {
18+
const replacedValue = getReplacedValue(atRule.params, await environmentVariablesPromise);
1619

17-
root.walk(node => {
18-
const supportedValue = getSupportedValue(node);
19-
20-
if (supportedValue) {
21-
const replacedValue = getReplacedValue(supportedValue, environmentVariables);
20+
if (replacedValue !== atRule.params) {
21+
atRule.params = replacedValue;
22+
}
23+
},
24+
async Declaration(decl) {
25+
const replacedValue = getReplacedValue(decl.value, await environmentVariablesPromise);
2226

23-
if (replacedValue !== supportedValue) {
24-
setSupportedValue(node, replacedValue);
25-
}
27+
if (replacedValue !== decl.value) {
28+
decl.value = replacedValue;
2629
}
27-
});
30+
}
2831
};
29-
});
32+
};
33+
34+
module.exports.postcss = true;

src/lib/get-replaced-value.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { parse } from 'postcss-values-parser';
22
import updateEnvValue from './update-env-value';
33
import walkEnvFuncs from './walk-env-funcs';
44

5-
// returns a value replaced with environment variables
5+
/**
6+
* @param {string} originalValue
7+
* @param variables
8+
* @returns {string} returns a value replaced with environment variables
9+
*/
610
export default (originalValue, variables) => {
711
// get the ast of the original value
812
const ast = parse(originalValue);

src/lib/get-supported-value.js

-5
This file was deleted.

src/lib/import-from.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import fs from 'fs';
22
import path from 'path';
33
import { parse } from 'postcss-values-parser';
44

5-
/* Import Custom Properties from Object
6-
/* ========================================================================== */
7-
5+
/**
6+
* Import Custom Properties from Object
7+
* @param {{environmentVariables: Record<string, string>, 'environment-variables': Record<string, string>}} object
8+
* @returns {Record<string, import('postcss-values-parser').Root>}
9+
*/
810
function importEnvironmentVariablesFromObject(object) {
911
const environmentVariables = Object.assign(
1012
{},
@@ -18,27 +20,33 @@ function importEnvironmentVariablesFromObject(object) {
1820
return environmentVariables;
1921
}
2022

21-
/* Import Custom Properties from JSON file
22-
/* ========================================================================== */
23-
23+
/**
24+
* Import Custom Properties from JSON file
25+
* @param {string} from
26+
* @returns {Promise<Record<string, import('postcss-values-parser').Root>>}
27+
*/
2428
async function importEnvironmentVariablesFromJSONFile(from) {
2529
const object = await readJSON(path.resolve(from));
2630

2731
return importEnvironmentVariablesFromObject(object);
2832
}
2933

30-
/* Import Custom Properties from JS file
31-
/* ========================================================================== */
32-
34+
/**
35+
* Import Custom Properties from JS file
36+
* @param {string} from
37+
* @returns {Promise<Record<string, import('postcss-values-parser').Root>>}
38+
*/
3339
async function importEnvironmentVariablesFromJSFile(from) {
3440
const object = await import(path.resolve(from));
3541

3642
return importEnvironmentVariablesFromObject(object);
3743
}
3844

39-
/* Import Custom Properties from Sources
40-
/* ========================================================================== */
41-
45+
/**
46+
* Import Custom Properties from Sources
47+
* @param {(string|Function|Promise|{type:string,environmentVariables: Record<string, string>, 'environment-variables': Record<string, string>})[]} sources
48+
* @returns {Promise<Record<string, import('postcss-values-parser').Root>>}
49+
*/
4250
export default function importEnvironmentVariablesFromSources(sources) {
4351
return sources.map(source => {
4452
if (source instanceof Promise) {
@@ -80,6 +88,10 @@ export default function importEnvironmentVariablesFromSources(sources) {
8088
/* Helper utilities
8189
/* ========================================================================== */
8290

91+
/**
92+
* @param {string} from
93+
* @returns {Promise<string>}
94+
*/
8395
const readFile = from => new Promise((resolve, reject) => {
8496
fs.readFile(from, 'utf8', (error, result) => {
8597
if (error) {

src/lib/is-atrule.js

-2
This file was deleted.

src/lib/is-decl.js

-2
This file was deleted.

src/lib/set-supported-value.js

-13
This file was deleted.

0 commit comments

Comments
 (0)