Skip to content

Commit

Permalink
refactor: code (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 21, 2020
1 parent f3ae4d5 commit 73000e3
Show file tree
Hide file tree
Showing 18 changed files with 2,223 additions and 2,993 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"parserOptions": {
"ecmaVersion": 2018
},
"env": {
"node": true,
"es6": true,
"jest": true
},
"extends": "eslint:recommended"
}
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
.idea
*.iml
.nyc_output
coverage
lib
node_modules

41 changes: 9 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
sudo: false

git:
depth: 10

branches:
only:
- master
- next

language: node_js

cache:
yarn: true
directories:
- node_modules

matrix:
include:
- node_js: "12"
env: JOB_PART=pretest
- node_js: "6"
env: JOB_PART=test
- node_js: "8"
env: JOB_PART=test
- node_js: "10"
env: JOB_PART=test
- node_js: "12"
env: JOB_PART=test
node_js:
- "10"
- "12"
- "14"

before_script:
- node --version
- yarn --version
script: yarn ci

script:
- yarn $JOB_PART
after_success:
- cat ./coverage/lcov.info | node_modules/.bin/coveralls --verbose
- cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js
- rm -rf ./coverage
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ Converts icss imports and exports definitions to postcss ast
createICSSRules(
{
colors: {
a: "b"
}
a: "b",
},
},
{
c: "d"
}
c: "d",
},
// Need pass `rule` and `decl` from postcss
// Please look at `Step 4` https://evilmartians.com/chronicles/postcss-8-plugin-migration
postcss
);
```

Expand Down
5 changes: 5 additions & 0 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
hooks: {
"pre-commit": "lint-staged",
},
};
4 changes: 4 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"*.js": ["eslint --fix", "prettier --write"],
"*.{json,md,yml,css,ts}": ["prettier --write"],
};
77 changes: 21 additions & 56 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,24 @@
"name": "icss-utils",
"version": "4.1.1",
"description": "ICSS utils for postcss ast",
"main": "lib/index.js",
"main": "src/index.js",
"engines": {
"node": ">= 6"
"node": ">= 10.13.0 || >= 12.13.0 || >= 14"
},
"files": [
"lib"
"src"
],
"scripts": {
"lint": "eslint . --ignore-path .gitignore",
"build": "babel --out-dir lib src",
"pretest": "npm run lint",
"test": "npm run test:only",
"prettier": "prettier -l --ignore-path .gitignore . \"!test/test-cases\"",
"eslint": "eslint --ignore-path .gitignore .",
"lint": "yarn eslint && yarn prettier",
"pretest": "yarn lint",
"test": "jest --coverage",
"test:only": "jest",
"prepublish": "yarn test && yarn run build"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": 6
}
}
]
]
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
},
"env": {
"es6": true,
"jest": true
},
"extends": "eslint:recommended"
},
"lint-staged": {
"*.js": [
"prettier --write",
"eslint",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
"autotest": "jest --coverage --watch",
"cover": "jest --coverage",
"ci": "yarn pretest && yarn cover",
"prepublishOnly": "yarn test"
},
"repository": {
"type": "git",
Expand All @@ -68,19 +37,15 @@
"url": "https://github.com/css-modules/icss-utils/issues"
},
"homepage": "https://github.com/css-modules/icss-utils#readme",
"dependencies": {
"postcss": "^7.0.14"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",
"eslint": "^5.14.1",
"husky": "^1.3.1",
"jest": "^24.1.0",
"lint-staged": "^8.1.4",
"prettier": "^1.16.4"
"eslint": "^7.9.0",
"husky": "^4.3.0",
"jest": "^26.4.2",
"lint-staged": "^10.4.0",
"postcss": "^8.0.6",
"prettier": "^2.1.2"
},
"peerDependencies": {
"postcss": "^8.0.0"
}
}
28 changes: 13 additions & 15 deletions src/createICSSRules.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import postcss from "postcss";

const createImports = imports => {
return Object.keys(imports).map(path => {
const createImports = (imports, postcss) => {
return Object.keys(imports).map((path) => {
const aliases = imports[path];
const declarations = Object.keys(aliases).map(key =>
const declarations = Object.keys(aliases).map((key) =>
postcss.decl({
prop: key,
value: aliases[key],
raws: { before: "\n " }
raws: { before: "\n " },
})
);

const hasDeclarations = declarations.length > 0;

const rule = postcss.rule({
selector: `:import('${path}')`,
raws: { after: hasDeclarations ? "\n" : "" }
raws: { after: hasDeclarations ? "\n" : "" },
});

if (hasDeclarations) {
Expand All @@ -26,12 +24,12 @@ const createImports = imports => {
});
};

const createExports = exports => {
const declarations = Object.keys(exports).map(key =>
const createExports = (exports, postcss) => {
const declarations = Object.keys(exports).map((key) =>
postcss.decl({
prop: key,
value: exports[key],
raws: { before: "\n " }
raws: { before: "\n " },
})
);

Expand All @@ -42,16 +40,16 @@ const createExports = exports => {
const rule = postcss
.rule({
selector: `:export`,
raws: { after: "\n" }
raws: { after: "\n" },
})
.append(declarations);

return [rule];
};

const createICSSRules = (imports, exports) => [
...createImports(imports),
...createExports(exports)
const createICSSRules = (imports, exports, postcss) => [
...createImports(imports, postcss),
...createExports(exports, postcss),
];

export default createICSSRules;
module.exports = createICSSRules;
8 changes: 4 additions & 4 deletions src/extractICSS.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;

const getDeclsObject = rule => {
const getDeclsObject = (rule) => {
const object = {};

rule.walkDecls(decl => {
rule.walkDecls((decl) => {
const before = decl.raws.before ? decl.raws.before.trim() : "";

object[before + decl.prop] = decl.value;
Expand All @@ -16,7 +16,7 @@ const extractICSS = (css, removeRules = true) => {
const icssImports = {};
const icssExports = {};

css.each(node => {
css.each((node) => {
if (node.type === "rule") {
if (node.selector.slice(0, 7) === ":import") {
const matches = importPattern.exec(node.selector);
Expand Down Expand Up @@ -48,4 +48,4 @@ const extractICSS = (css, removeRules = true) => {
return { icssImports, icssExports };
};

export default extractICSS;
module.exports = extractICSS;
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export { default as replaceValueSymbols } from "./replaceValueSymbols.js";
export { default as replaceSymbols } from "./replaceSymbols.js";
export { default as extractICSS } from "./extractICSS.js";
export { default as createICSSRules } from "./createICSSRules.js";
const replaceValueSymbols = require("./replaceValueSymbols.js");
const replaceSymbols = require("./replaceSymbols.js");
const extractICSS = require("./extractICSS.js");
const createICSSRules = require("./createICSSRules.js");

module.exports = {
replaceValueSymbols,
replaceSymbols,
extractICSS,
createICSSRules,
};
6 changes: 3 additions & 3 deletions src/replaceSymbols.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import replaceValueSymbols from "./replaceValueSymbols.js";
const replaceValueSymbols = require("./replaceValueSymbols.js");

const replaceSymbols = (css, replacements) => {
css.walk(node => {
css.walk((node) => {
if (node.type === "decl" && node.value) {
node.value = replaceValueSymbols(node.value.toString(), replacements);
} else if (node.type === "rule" && node.selector) {
Expand All @@ -15,4 +15,4 @@ const replaceSymbols = (css, replacements) => {
});
};

export default replaceSymbols;
module.exports = replaceSymbols;
2 changes: 1 addition & 1 deletion src/replaceValueSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ const replaceValueSymbols = (value, replacements) => {
return value;
};

export default replaceValueSymbols;
module.exports = replaceValueSymbols;
20 changes: 10 additions & 10 deletions test/createICSSRules.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import postcss from "postcss";
import { createICSSRules } from "../src";
const postcss = require("postcss");
const { createICSSRules } = require("../src");

const run = (imports, exports) => {
return postcss
.root()
.append(createICSSRules(imports, exports))
.append(createICSSRules(imports, exports, postcss))
.toString();
};

test("create empty :import statement", () => {
expect(
run(
{
"path/file": {}
"path/file": {},
},
{}
)
Expand All @@ -24,8 +24,8 @@ test("create :import statement", () => {
run(
{
"path/file": {
e: "f"
}
e: "f",
},
},
{}
)
Expand All @@ -38,7 +38,7 @@ test("create :export statement", () => {
{},
{
a: "b",
c: "d"
c: "d",
}
)
).toEqual(":export {\n a: b;\n c: d\n}");
Expand All @@ -49,11 +49,11 @@ test("create :import and :export", () => {
run(
{
colors: {
a: "b"
}
a: "b",
},
},
{
c: "d"
c: "d",
}
)
).toEqual(":import('colors') {\n a: b\n}\n:export {\n c: d\n}");
Expand Down
Loading

0 comments on commit 73000e3

Please sign in to comment.