Skip to content

Commit a770436

Browse files
authored
Merge pull request #7 from css-modules/jest
Jest
2 parents 3e60a7f + 63a4745 commit a770436

File tree

11 files changed

+3680
-145
lines changed

11 files changed

+3680
-145
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
lib/
2-
3-
# Logs
4-
logs
5-
*.log
6-
7-
# Runtime data
8-
pids
9-
*.pid
10-
*.seed
11-
12-
# Directory for instrumented libs generated by jscoverage/JSCover
13-
lib-cov
14-
15-
# Coverage directory used by tools like istanbul
16-
coverage
17-
18-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19-
.grunt
20-
21-
# node-waf configuration
22-
.lock-wscript
23-
24-
# Compiled binary addons (http://nodejs.org/api/addons.html)
25-
build/Release
26-
27-
# Dependency directory
28-
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
291
node_modules
2+
coverage
3+
lib

.npmignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "4"
4+
- "6"
5+
- "node"

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Alexey Litvinov
3+
Copyright (c) 2015 Alexey Litvinov and Bogdan Chadkin
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,69 @@
11
{
2-
"name": "postcss-modules-parser",
2+
"name": "postcss-icss",
33
"version": "1.1.0",
4-
"description": "A CSS Modules parser to extract tokens from the css file",
5-
"main": "index.js",
4+
"description": "Postcss plugin to process css modules and extract tokens",
5+
"main": "lib/index.js",
6+
"files": [
7+
"lib"
8+
],
69
"scripts": {
7-
"compile": "babel src --out-dir lib",
8-
"prepublish": "in-publish && npm run -s compile || in-install",
9-
"test": "mocha --compilers js:babel-core/register"
10+
"build": "babel --out-dir lib src",
11+
"test": "jest --coverage",
12+
"precommit": "lint-staged",
13+
"prepublish": "yarn test && yarn run build"
14+
},
15+
"lint-staged": {
16+
"*.js": [
17+
"eslint",
18+
"prettier --write",
19+
"git add"
20+
]
1021
},
11-
"engines": {
12-
"node": ">=0.12"
22+
"eslintConfig": {
23+
"parserOptions": {
24+
"ecmaVersion": 6,
25+
"sourceType": "module"
26+
},
27+
"env": {
28+
"es6": true
29+
},
30+
"extends": "eslint:recommended"
1331
},
14-
"repository": {
15-
"type": "git",
16-
"url": "git+https://github.com/css-modules/postcss-modules-parser.git"
32+
"babel": {
33+
"presets": [
34+
[
35+
"env",
36+
{
37+
"targets": {
38+
"node": 4
39+
}
40+
}
41+
]
42+
]
1743
},
44+
"repository": "css-modules/postcss-icss",
1845
"keywords": [
1946
"css-modules",
2047
"postcss",
2148
"css",
49+
"icss",
2250
"postcss-plugin"
2351
],
2452
"author": "Alexey Litvinov",
2553
"license": "MIT",
26-
"bugs": {
27-
"url": "https://github.com/css-modules/postcss-modules-parser/issues"
28-
},
29-
"homepage": "https://github.com/css-modules/postcss-modules-parser#readme",
3054
"dependencies": {
3155
"icss-replace-symbols": "^1.0.2",
3256
"lodash.foreach": "^3.0.3",
3357
"postcss": "^5.0.10"
3458
},
3559
"devDependencies": {
36-
"babel-cli": "^6.0.15",
37-
"babel-core": "^6.0.17",
38-
"babel-preset-es2015": "^6.0.15",
39-
"in-publish": "^2.0.0",
40-
"mocha": "^2.3.3"
60+
"babel-cli": "^6.24.1",
61+
"babel-jest": "^20.0.3",
62+
"babel-preset-env": "^1.5.1",
63+
"eslint": "^3.19.0",
64+
"husky": "^0.13.3",
65+
"jest": "^20.0.4",
66+
"lint-staged": "^3.5.0",
67+
"prettier": "^1.3.1"
4168
}
4269
}

src/index.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { plugin } from 'postcss';
2-
import forEach from 'lodash.foreach';
3-
import replaceSymbols from 'icss-replace-symbols';
1+
/* eslint-env node */
2+
import postcss from "postcss";
3+
import forEach from "lodash.foreach";
4+
import replaceSymbols from "icss-replace-symbols";
45
const importRegexp = /^:import\((.+)\)$/;
56
const exportRegexp = /^:export$/;
67

@@ -9,7 +10,7 @@ const exportRegexp = /^:export$/;
910
* @return {boolean}
1011
*/
1112
function isPromise(promise) {
12-
return typeof promise === 'object' && typeof promise.then === 'function';
13+
return typeof promise === "object" && typeof promise.then === "function";
1314
}
1415

1516
/**
@@ -23,7 +24,10 @@ function proceed(css, translations) {
2324

2425
css.walkRules(exportRegexp, rule => {
2526
rule.walkDecls(decl => {
26-
forEach(translations, (value, key) => decl.value = decl.value.replace(key, value));
27+
forEach(
28+
translations,
29+
(value, key) => (decl.value = decl.value.replace(key, value))
30+
);
2731
exportTokens[decl.prop] = decl.value;
2832
});
2933

@@ -37,38 +41,35 @@ function proceed(css, translations) {
3741
* @param {function} options.fetch
3842
* @return {function}
3943
*/
40-
export default plugin('parser', function parser({ fetch } = {}) {
41-
return css => {
42-
// https://github.com/postcss/postcss/blob/master/docs/api.md#inputfile
43-
const file = css.source.input.file;
44+
module.exports = postcss.plugin("postcss-icss", ({ fetch } = {}) => css => {
45+
// https://github.com/postcss/postcss/blob/master/docs/api.md#inputfile
46+
const file = css.source.input.file;
4447

45-
const translations = {};
46-
const promises = [];
48+
const translations = {};
49+
const promises = [];
4750

48-
let iteration = 0;
51+
let iteration = 0;
4952

50-
css.walkRules(importRegexp, rule => {
51-
const dependency = RegExp.$1.replace(/^["']|["']$/g, '');
52-
const result = fetch(dependency, file, iteration++);
53+
css.walkRules(importRegexp, rule => {
54+
const dependency = RegExp.$1.replace(/^["']|["']$/g, "");
55+
const result = fetch(dependency, file, iteration++);
5356

54-
if (isPromise(result)) {
55-
result.then(exports => {
56-
rule.walkDecls(decl => translations[decl.prop] = exports[decl.value]);
57-
rule.remove();
58-
});
59-
60-
promises.push(result);
61-
} else {
62-
rule.walkDecls(decl => translations[decl.prop] = result[decl.value]);
57+
if (isPromise(result)) {
58+
result.then(exports => {
59+
rule.walkDecls(decl => (translations[decl.prop] = exports[decl.value]));
6360
rule.remove();
64-
}
65-
});
61+
});
6662

67-
if (promises.length === 0) {
68-
return void proceed(css, translations);
63+
promises.push(result);
64+
} else {
65+
rule.walkDecls(decl => (translations[decl.prop] = result[decl.value]));
66+
rule.remove();
6967
}
68+
});
69+
70+
if (promises.length === 0) {
71+
return void proceed(css, translations);
72+
}
7073

71-
return Promise.all(promises)
72-
.then(() => proceed(css, translations));
73-
};
74+
return Promise.all(promises).then(() => proceed(css, translations));
7475
});

test/index.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

test/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-env jest */
2+
import { readFileSync } from "fs";
3+
import { resolve } from "path";
4+
import asyncLoader from "./helper/async-loader";
5+
import syncLoader from "./helper/sync-loader";
6+
7+
let fixture;
8+
let expected;
9+
let filename;
10+
11+
describe("single", () => {
12+
beforeEach(() => {
13+
fixture = "test/fixture/single";
14+
filename = resolve(fixture, "source.css");
15+
expected = JSON.parse(
16+
readFileSync(resolve(fixture, "expected.json"), "utf8")
17+
);
18+
});
19+
20+
it("asynchronous", () => {
21+
return asyncLoader(filename, filename).then(result => {
22+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expected));
23+
});
24+
});
25+
26+
it("synchronous", () => {
27+
const result = syncLoader(filename, filename);
28+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expected));
29+
});
30+
});
31+
32+
describe("multiple", () => {
33+
beforeEach(() => {
34+
fixture = "test/fixture/multiple";
35+
filename = resolve(fixture, "source.css");
36+
expected = JSON.parse(
37+
readFileSync(resolve(fixture, "expected.json"), "utf8")
38+
);
39+
});
40+
41+
it("asynchronous", () => {
42+
return asyncLoader(filename, filename).then(result => {
43+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expected));
44+
});
45+
});
46+
47+
it("synchronous", () => {
48+
const result = syncLoader(filename, filename);
49+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expected));
50+
});
51+
});

0 commit comments

Comments
 (0)