Skip to content

Commit

Permalink
Add export to javascript (#53)
Browse files Browse the repository at this point in the history
- create test cases
- add export to javascript
- update docs
- fix syntax and formatting
- change javascript output formatting

* - fix tests and add missing fixtures
  • Loading branch information
siemiatj authored and selaux committed Feb 22, 2017
1 parent 018a65d commit d0447d5
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -114,6 +114,7 @@ Built-in formats:
- `'sass'`, `'scss'`: http://sass-lang.com/
- `'css'`: http://www.w3.org/Style/CSS/
- `'prefixed-css'`: A version of `'css'` that generates a smaller stylesheet but requires the use of the `'prefix'` stylesheet option
- `'javascript'`: Creates a file that exports javascrip object with sprites data.

#### options.stylesheetOptions
Type: `Object`
Expand Down
6 changes: 6 additions & 0 deletions lib/stylesheet/javascript.js
@@ -0,0 +1,6 @@
'use strict';

var getTemplatedStylesheet = require('./templatedStylesheet'),
path = require('path');

module.exports = getTemplatedStylesheet(path.join(__dirname, '/templates/javascript.tpl'));
8 changes: 8 additions & 0 deletions lib/stylesheet/templates/javascript.tpl
@@ -0,0 +1,8 @@
'use strict';

var SPRITES = {
<% layout.images.forEach(function (image, idx) { if (image.className.indexOf('-') >= 0) { %>'<%= image.className %>'<% } else { %><%= image.className %><% } %>: { x: <%= image.x %>, y: <%= image.y %>, width: <%= image.width %>, height: <%= image.height %> }<% if (idx !== layout.images.length - 1) { %>,<% } %>
<% }); %>
};

module.exports = SPRITES;
10 changes: 10 additions & 0 deletions test/fixtures/stylesheets/javascript/no-options.js
@@ -0,0 +1,10 @@
'use strict';

var SPRITES = {
foo: { x: 0, y: 0, width: 150, height: 12 },
bar: { x: 0, y: 12, width: 150, height: 24 },
test: { x: 0, y: 36, width: 150, height: 12 }

};

module.exports = SPRITES;
10 changes: 10 additions & 0 deletions test/fixtures/stylesheets/javascript/with-nameMapping.js
@@ -0,0 +1,10 @@
'use strict';

var SPRITES = {
oof: { x: 0, y: 0, width: 150, height: 12 },
rab: { x: 0, y: 12, width: 150, height: 24 },
tset: { x: 0, y: 36, width: 150, height: 12 }

};

module.exports = SPRITES;
10 changes: 10 additions & 0 deletions test/fixtures/stylesheets/javascript/with-pixelRatio.js
@@ -0,0 +1,10 @@
'use strict';

var SPRITES = {
foo: { x: 0, y: 0, width: 75, height: 6 },
bar: { x: 0, y: 6, width: 75, height: 12 },
test: { x: 0, y: 18, width: 75, height: 6 }

};

module.exports = SPRITES;
10 changes: 10 additions & 0 deletions test/fixtures/stylesheets/javascript/with-prefix.js
@@ -0,0 +1,10 @@
'use strict';

var SPRITES = {
'prefix-foo': { x: 0, y: 0, width: 150, height: 12 },
'prefix-bar': { x: 0, y: 12, width: 150, height: 24 },
'prefix-test': { x: 0, y: 36, width: 150, height: 12 }

};

module.exports = SPRITES;
10 changes: 10 additions & 0 deletions test/fixtures/stylesheets/javascript/with-spritePath.js
@@ -0,0 +1,10 @@
'use strict';

var SPRITES = {
foo: { x: 0, y: 0, width: 150, height: 12 },
bar: { x: 0, y: 12, width: 150, height: 24 },
test: { x: 0, y: 36, width: 150, height: 12 }

};

module.exports = SPRITES;
5 changes: 5 additions & 0 deletions test/specs/stylesheet/javascript.js
@@ -0,0 +1,5 @@
'use strict';

var testTemplatedStylesheet = require('./testTemplatedStylesheet');

testTemplatedStylesheet('javascript', 'js');

0 comments on commit d0447d5

Please sign in to comment.