Skip to content

Commit 3807c8c

Browse files
author
Eduardo García Sanz
committed
tested and documented...
1 parent 5d0c8e0 commit 3807c8c

5 files changed

Lines changed: 157 additions & 11 deletions

File tree

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: node_js
2+
node_js :
3+
- "0.11"
4+
- "iojs"
5+
deploy:
6+
provider: npm
7+
email: eduardo@comakai.com
8+
api_key:
9+
secure: JX1zgdfsP3rwISjl2mn6LNFOwNYcy2XIuL1mHXhAllDE5utvvaum8/zt/PWuBY7qlocGN8NZfjDsWc3PlDiezs9o0JZ0y0ipBgcPLPJJqbXc6U5EIVAUwnOMs7HorF1yz4S5T4GfBWwWfhFPr3setSHmRDTKyjmUBUqnvUj9Pq8=
10+
on:
11+
tags: true
12+
repo: coma/svg-reactify

README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,72 @@
1-
# svg-reactify
2-
transform SVG files into React elements
1+
svg-reactify
2+
============
3+
4+
[![Build Status](https://travis-ci.org/coma/svg-reactify.png?branch=master)](https://travis-ci.org/coma/svg-reactify)
5+
[![Dependency Status](https://david-dm.org/coma/svg-reactify.png)](http://david-dm.org/coma/svg-reactify)
6+
[![NPM version](https://badge.fury.io/js/svg-reactify.png)](http://badge.fury.io/js/svg-reactify)
7+
![io.js supported](https://img.shields.io/badge/io.js-supported-green.svg?style=flat)
8+
9+
Transform SVG files into React elements.
10+
11+
Setup
12+
-----
13+
14+
Without configuration...
15+
16+
```javascript
17+
var browserify = require('browserify'),
18+
svgrt = require('svg-reactify');
19+
20+
browserify({
21+
transform: [svgrt]
22+
})
23+
.bundle()
24+
```
25+
26+
and with some configuration...
27+
28+
```javascript
29+
var browserify = require('browserify'),
30+
svgrt = require('svg-reactify');
31+
32+
browserify({
33+
transform: [svgrt({
34+
svgo : {}, // options passed to svgo
35+
react: {} // options passed to react-tools
36+
})]
37+
})
38+
.bundle()
39+
```
40+
41+
Requiring SVG files
42+
-------------------
43+
44+
Now you can do things like...
45+
46+
```javascript
47+
var React = require('react'),
48+
SVG = {
49+
Dog : require('images/dog.svg'),
50+
Parrot: require('images/parrot.svg'),
51+
Horse : require('images/horse.svg')
52+
};
53+
54+
module.exports = React.createClass({
55+
render: function () {
56+
return (
57+
<h2>Animals</h2>
58+
<ul>
59+
<li>
60+
<SVG.Dog/>
61+
</li>
62+
<li>
63+
<SVG.Parrot/>
64+
</li>
65+
<li>
66+
<SVG.Horse/>
67+
</li>
68+
</ul>
69+
);
70+
}
71+
});
72+
```

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
var through = require('through'),
22
extend = require('extend'),
33
SVGO = require('svgo'),
4-
react = require('react-tools');
5-
6-
var svgo = new SVGO();
4+
react = require('react-tools'),
5+
svgo = new SVGO();
76

87
var settings = {
98
react: {
@@ -12,7 +11,7 @@ var settings = {
1211
stripTypes: false,
1312
harmony : false
1413
},
15-
svgo: {}
14+
svgo : {}
1615
};
1716

1817
var isSVG = function (filename) {
@@ -34,7 +33,7 @@ var transform = function (filename) {
3433

3534
var out = function (svg) {
3635

37-
var source = "var React = require('react');module.exports = React.createClass({render: function () { return (" + svg.data + "); }});",
36+
var source = 'module.exports = require("react").createClass({render: function () { return (' + svg.data + '); }});',
3837
output = react.transform(source, settings.react);
3938

4039
stream.queue(output);

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"homepage" : "https://github.com/coma/svg-reactify",
55
"repository" : "https://github.com/coma/svg-reactify",
66
"main" : "./index.js",
7-
"version" : "0.3.0",
7+
"version" : "0.4.0",
88
"license" : "MIT",
99
"private" : false,
1010
"authors" : [
@@ -22,7 +22,18 @@
2222
"svgo" : "^0.5",
2323
"extend" : "^2"
2424
},
25-
"devDependencies": {},
26-
"scripts" : {},
27-
"engines" : {}
25+
"devDependencies": {
26+
"jshint" : "^2",
27+
"jshint-stylish": "^1",
28+
"mocha" : "^2",
29+
"rewire" : "^2"
30+
},
31+
"scripts" : {
32+
"lint": "jshint --verbose --reporter node_modules/jshint-stylish/stylish.js index.js",
33+
"test": "npm run lint && mocha test"
34+
},
35+
"engines" : {
36+
"iojs" : ">= 1.0.0",
37+
"node" : ">= 0.11.0"
38+
}
2839
}

test/main.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var rewire = require('rewire'),
2+
assert = require('assert'),
3+
svgrt = rewire('..');
4+
5+
var svg = [
6+
'<?xml version="1.0" encoding="utf-8"?>',
7+
'<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">',
8+
' <rect x="0" y="0" width="100" height="100"/>',
9+
'</svg>'
10+
];
11+
12+
describe('when passing a string', function () {
13+
14+
it('won\'t do anything if it isn\'t a SVG file', function (done) {
15+
16+
svgrt.__set__('through', function () {
17+
18+
assert.strictEqual(arguments.length, 0);
19+
done();
20+
});
21+
22+
svgrt('foo.png');
23+
});
24+
25+
it('will work for a SVG file', function (done) {
26+
27+
var write, end;
28+
29+
svgrt.__set__('through', function () {
30+
31+
assert.strictEqual(arguments.length, 2);
32+
33+
write = arguments[0];
34+
end = arguments[1];
35+
36+
return {
37+
queue: function (output) {
38+
39+
if (!output) {
40+
41+
return;
42+
}
43+
44+
assert.strictEqual(output, 'module.exports = require(\"react\").createClass({render: function () { return (React.createElement(\"svg\", {viewBox: \"0 0 100 100\", xmlns: \"http://www.w3.org/2000/svg\"}, React.createElement(\"path\", {d: \"M0 0h100v100H0z\"}))); }});');
45+
done();
46+
}
47+
};
48+
});
49+
50+
svgrt('foo.svg');
51+
svg.forEach(write);
52+
end();
53+
});
54+
});

0 commit comments

Comments
 (0)