Skip to content

Commit eb8af8d

Browse files
committed
Move source file into seperate folder. Add ESLint and Mocha, set up initial tests, bump to 0.0.7.
1 parent 8b6f222 commit eb8af8d

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "semistandard",
3+
"plugins": [],
4+
"env": {
5+
"mocha": true,
6+
"node": true
7+
}
8+
}

package.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
{
22
"name": "css-object-loader",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"author": "pl12133",
55
"description": "Webpack loader to load CSS into a selector object with camelCased properties",
6-
"main": "index.js",
6+
"main": "src/index.js",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"lint": "./node_modules/.bin/eslint src test",
9+
"lint:fix": "./node_modules/.bin/eslint src test --fix",
10+
"test": "./node_modules/.bin/mocha",
11+
"prepublish": "npm run lint && npm run test"
912
},
1013
"repository": {
11-
"type" : "git",
12-
"url" : "https://github.com/pl12133/css-object-loader"
14+
"type": "git",
15+
"url": "https://github.com/pl12133/css-object-loader"
1316
},
1417
"license": "MIT",
1518
"dependencies": {
1619
"camelcase": "^2.1.1",
1720
"css": "^2.2.1"
21+
},
22+
"devDependencies": {
23+
"chai": "^3.5.0",
24+
"eslint": "^2.9.0",
25+
"eslint-config-semistandard": "^6.0.1",
26+
"eslint-config-standard": "^5.2.0",
27+
"eslint-plugin-promise": "^1.1.0",
28+
"eslint-plugin-standard": "^1.3.2",
29+
"mocha": "^2.4.5"
1830
}
1931
}

index.js renamed to src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict;'
1+
'use strict;';
22
// CSS Parser
33
var css = require('css');
44
// Convert words to camelCase
@@ -7,7 +7,7 @@ var camelCase = require('camelcase');
77
// To accomplish loading CSS to an Object, the process is:
88
// 1. Parse CSS stylesheet to AST
99
// 2. Flatten AST to Object of shape { [rule.selector]: rule.declarations }
10-
module.exports = function cssObjectLoader(source) {
10+
module.exports = function cssObjectLoader (source) {
1111
this.cacheable && this.cacheable();
1212
// Step 1.
1313
var parsedStylesheet = getParsedStylesheet(source);
@@ -17,12 +17,12 @@ module.exports = function cssObjectLoader(source) {
1717
};
1818

1919
// Parse the contents of the CSS file and get the resulting AST
20-
function getParsedStylesheet(source) {
20+
function getParsedStylesheet (source) {
2121
return css.parse(source).stylesheet;
2222
}
2323

2424
// Return `true` for an AST node with { type: 'rule' } and valid selectors
25-
function isValidRule(rule) {
25+
function isValidRule (rule) {
2626
return !!(rule.type === 'rule' && rule.selectors && rule.selectors.length);
2727
}
2828
// Return `true` for an AST node with { type: 'declaration' } and any property
@@ -31,7 +31,7 @@ function isValidDeclaration (declaration) {
3131
}
3232

3333
// Reduce a declaration node from the AST to a style object
34-
function reduceDeclarationsToStyleObject(styleObj, declaration) {
34+
function reduceDeclarationsToStyleObject (styleObj, declaration) {
3535
if (!isValidDeclaration(declaration)) {
3636
return styleObj;
3737
}
@@ -42,15 +42,15 @@ function reduceDeclarationsToStyleObject(styleObj, declaration) {
4242
}
4343

4444
// Reduce a rule to a collection of selectors
45-
function reduceRulesToSelectors(selectors, rule) {
45+
function reduceRulesToSelectors (selectors, rule) {
4646
if (!isValidRule(rule)) {
4747
return selectors;
4848
}
49-
var styleObject = rule.declarations.reduce(reduceDeclarationsToStyleObject, {})
49+
var styleObject = rule.declarations.reduce(reduceDeclarationsToStyleObject, {});
5050
rule.selectors.forEach((selector) => {
5151
selectors[selector] = Object.assign({},
5252
selectors[selector],
53-
styleObject
53+
styleObject
5454
);
5555
});
5656
return selectors;

test/index.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// BEGIN IMPORTS
2+
var fs = require('fs');
3+
var path = require('path');
4+
var cssObjectLoader = require('../');
5+
var expect = require('chai').expect;
6+
// END IMPORTS
7+
8+
// Load test CSS file
9+
var source = fs.readFileSync(path.resolve('test', 'style-spec.css'), 'utf-8');
10+
11+
// Loader returns a string with 'module.exports = ...';
12+
var loaderString = cssObjectLoader(source);
13+
14+
// Parse the JSON that is in the loaded CSS
15+
var loadedJSON = loaderString.replace(/.*?({.*}).*/, (m, $1) => $1);
16+
17+
// The Object the loader actually returns.
18+
var actual = JSON.parse(loadedJSON);
19+
describe('css-object-loader', () => {
20+
it('is a function', () => {
21+
expect(cssObjectLoader).to.be.a('function');
22+
});
23+
24+
it('should be able to transform css', () => {
25+
expect({
26+
p: {
27+
fontSize: '14px',
28+
fontWeight: 'bold'
29+
}
30+
}).to.deep.equal(actual);
31+
});
32+
});

test/style-spec.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p {
2+
font-size: 14px;
3+
font-weight: bold;
4+
}

0 commit comments

Comments
 (0)