broccoli-svgstore
Advanced tools
Comparing version 0.2.1 to 0.3.1
@@ -0,7 +1,11 @@ | ||
# 0.3.1 -- September 13, 2016 | ||
- Update project licensing | ||
# 0.3.0 -- September 13, 2016 | ||
- Add ability to set `svgstore` options on a per-file basis ([#16](https://github.com/svgstore/broccoli-svgstore/pull/16)) | ||
# 0.2.1 -- August 28, 2016 | ||
- Use `svgstore` implementation of svg-to-symbol conversion [#13](https://github.com/svgstore/broccoli-svgstore/pull/13) | ||
# 0.2.0 -- August 03, 2016 | ||
- Replace `broccoli-writer` with `broccoli-caching-writer` and begin conforming to its API. | ||
@@ -12,3 +16,2 @@ - Update miscellaneous deps from two-year old project. | ||
# 0.1.0 | ||
- Initial release |
37
index.js
@@ -10,3 +10,2 @@ 'use strict'; | ||
var svgstore = require('svgstore'); | ||
// var svgToSymbol = require('./utils/svg-to-symbol'); | ||
@@ -50,29 +49,31 @@ var defaultSettings = { | ||
SvgProcessor.prototype.build = function () { | ||
/** | ||
* Overrides broccoli-plugin's `build' function. | ||
* @see: https://github.com/broccolijs/broccoli-plugin#pluginprototypebuild | ||
*/ | ||
SvgProcessor.prototype.build = function() { | ||
var svgOutput = svgstore(this._options.svgstoreOpts); | ||
var fileSettings = this._options.fileSettings || {}; | ||
try { | ||
var srcDir; | ||
var inputFiles; | ||
var inputFileName; | ||
var inputFilePath; | ||
var stat; | ||
var fileContents; | ||
var svgId; | ||
// iterate through `inputPaths` of our `inputNodes` (`inputPaths` is an array of | ||
// paths on disk corresponding to each node in `inputNodes`) | ||
for (var i = 0, l = this.inputPaths.length; i < l; i++) { | ||
srcDir = this.inputPaths[i]; | ||
inputFiles = helpers.multiGlob(["**/*.svg"], { cwd: srcDir }); | ||
var srcDir = this.inputPaths[i]; | ||
var inputFiles = helpers.multiGlob(["**/*.svg"], { cwd: srcDir }); | ||
for (var j = 0, ll = inputFiles.length; j < ll; j++) { | ||
inputFileName = inputFiles[j]; | ||
inputFilePath = path.join(srcDir, inputFileName); | ||
stat = fs.statSync(inputFilePath); | ||
var inputFileName = inputFiles[j]; | ||
var inputFilePath = path.join(srcDir, inputFileName); | ||
var stat = fs.statSync(inputFilePath); | ||
if (stat && stat.isFile()) { | ||
fileContents = fs.readFileSync(inputFilePath, { encoding: 'utf8' }); | ||
svgId = inputFileName.replace(/\.[^\.]+$/, ''); | ||
var fileNameWithoutExtension = inputFileName.replace(/\.[^\.]+$/, ''); | ||
var fileContents = fs.readFileSync(inputFilePath, { encoding: 'utf8' }); | ||
var inputFileSettings = fileSettings[fileNameWithoutExtension] || {}; | ||
var svgId = inputFileSettings.id || fileNameWithoutExtension; | ||
var fileSVGStoreOpts = inputFileSettings.svgstoreOpts || {}; | ||
svgOutput.add(svgId, fileContents); | ||
svgOutput.add(svgId, fileContents, fileSVGStoreOpts); | ||
} | ||
@@ -79,0 +80,0 @@ } |
{ | ||
"name": "broccoli-svgstore", | ||
"version": "0.2.1", | ||
"version": "0.3.1", | ||
"description": "Combine all your SVGs into one using <symbol>", | ||
@@ -29,3 +29,2 @@ "main": "index.js", | ||
"broccoli-kitchen-sink-helpers": "0.3.1", | ||
"cheerio": "^0.20.0", | ||
"mkdirp": "^0.5.1", | ||
@@ -38,4 +37,5 @@ "object-assign": "4.1.0", | ||
"chai": "3.5.0", | ||
"mocha": "2.5.3" | ||
"cheerio": "^0.22.0", | ||
"mocha": "^3.0.2" | ||
} | ||
} |
@@ -1,14 +0,21 @@ | ||
#broccoli-svgstore | ||
# broccoli-svgstore | ||
A [Broccoli](https://github.com/broccolijs/broccoli) plugin built on top of | ||
[`broccoli-caching-writer`](https://github.com/ember-cli/broccoli-caching-writer) that processes | ||
SVGs with [`svgstore`](https://github.com/svgstore/svgstore) | ||
[![Latest NPM release][npm-badge]][npm-badge-url] | ||
[![TravisCI Build Status][travis-badge]][travis-badge-url] | ||
[![License][license-badge]][license-badge-url] | ||
[![Dependencies][dependencies-badge]][dependencies-badge-url] | ||
[![Dev Dependencies][devDependencies-badge]][devDependencies-badge-url] | ||
_A [Broccoli](https://github.com/broccolijs/broccoli) plugin built on top of | ||
[`broccoli-caching-writer`](https://github.com/ember-cli/broccoli-caching-writer) that processes | ||
SVGs with [`svgstore`](https://github.com/svgstore/svgstore)_ | ||
## Installation | ||
```shell | ||
npm install --save broccoli-svgstore | ||
``` | ||
npm install --save broccoli-svgstore | ||
``` | ||
@@ -18,7 +25,7 @@ ## Usage | ||
`broccoli-svgstore` accepts an [`inputNode`](https://github.com/broccolijs/broccoli/blob/master/docs/node-api.md#part-2-node-api-specification) -- | ||
or a list of `inputNodes` -- and converts the contents of SVG files found within each node's directory root into | ||
SVG `<symbol/>`s (processing them with [`svgstore`](https://github.com/svgstore/svgstore)). | ||
or a list of `inputNodes` -- and converts the contents of SVG files found within each node's directory root into | ||
SVG `<symbol/>`s (processing them with [`svgstore`](https://github.com/svgstore/svgstore)). | ||
The transformed content is then written into a single file (see: the [`outputFile` option](#option-outputFile)), | ||
and returned as an output node of the Broccoli build processes. | ||
and returned as an output node of the Broccoli build processes. | ||
@@ -42,9 +49,9 @@ ```javascript | ||
## API | ||
- `inputNode|inputNodes` {inputNode or Array of inputNodes}: A standalone [Broccoli Node](https://github.com/broccolijs/broccoli/blob/master/docs/node-api.md), or a list of them. | ||
- `inputNode|inputNodes` {inputNode or Array of inputNodes}: A standalone [Broccoli Node](https://github.com/broccolijs/broccoli/blob/master/docs/node-api.md), or a list of them. | ||
The root of each node's source directory will form the starting point for a recursive search of `.svg` files. | ||
- `options`: {Object}: [Options for `broccoli-svgstore`](#broccoli-svgstore-options) | ||
- `options` {Object}: [Options for `broccoli-svgstore`](#options) | ||
### <a name="broccoli-svgstore-options"></a>Options | ||
- <a name="option-outputFile"></a>`outputFile` {string}: The name of the file -- including any directory | ||
### Options | ||
- <a name="option-outputFile"></a>`outputFile` {string}: The name of the file -- including any directory | ||
path -- [to which output will be written](https://github.com/broccolijs/broccoli-plugin#pluginprototypebuild) | ||
@@ -62,3 +69,3 @@ (starting from the root directory of your build destination). | ||
- `svgstoreOpts` {Object}: Options to be passed on to `svgstore` during the processing step. | ||
- `svgstoreOpts` {Object}: Options to be passed on to `svgstore` during the processing step. | ||
- See: `svgstore`'s options [documentation](https://github.com/svgstore/svgstore#options) | ||
@@ -69,1 +76,30 @@ | ||
- `fileSettings` {Object}: a hash of per-file settings. | ||
That is, each root key should correspond to a file name of an SVG that | ||
will be found in this node. It's value should then be an Object with any of the following settings: | ||
+ `id` {string}: A custom id to be used for this SVG's final `<symbol>`. | ||
+ `svgstoreOpts` {Object}: same as `options.svgstoreOpts`, but scoped to the file | ||
Example usage: | ||
```javascript | ||
var outputNode = svgstore(inputNodes, { | ||
outputFile: "/assets/icons.svg", | ||
fileSettings: { | ||
twitter: { id: 'tweet' }, | ||
menu: { id: 'hamburger', svgstoreOpts: { customSymbolAttrs: ['preserveAspectRatio'] } } | ||
} | ||
}); | ||
``` | ||
[npm-badge]: https://img.shields.io/npm/v/broccoli-svgstore.svg | ||
[npm-badge-url]: https://www.npmjs.com/package/broccoli-svgstore | ||
[travis-badge]: https://img.shields.io/travis/svgstore/broccoli-svgstore/master.svg?label=TravisCI | ||
[travis-badge-url]: https://travis-ci.org/svgstore/broccoli-svgstore | ||
[license-badge]: https://img.shields.io/npm/l/broccoli-svgstore.svg | ||
[license-badge-url]: LICENSE.md | ||
[dependencies-badge]: https://david-dm.org/svgstore/broccoli-svgstore/status.svg | ||
[dependencies-badge-url]: https://david-dm.org/svgstore/broccoli-svgstore | ||
[devDependencies-badge]: https://david-dm.org/svgstore/broccoli-svgstore/dev-status.svg | ||
[devDependencies-badge-url]: https://david-dm.org/svgstore/broccoli-svgstore#info=devDependencies |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var path = require('path'); | ||
@@ -26,8 +28,11 @@ var fs = require('fs'); | ||
function makeBuilderFromInputNodes(inputNodes, options = {}) { | ||
function makeBuilderFromInputNodes(inputNodes, options) { | ||
var options = options || {}; | ||
var svgProcessor = new SvgProcessor(inputNodes, { | ||
outputFile: options.outputFile || OUTPUT_FILE, | ||
annotation: options.annotation || 'SVGStore Processor -- Tests', | ||
svgstoreOpts: options.svgstoreOpts || {} | ||
svgstoreOpts: options.svgstoreOpts || {}, | ||
fileSettings: options.fileSettings || {} | ||
}); | ||
return new broccoli.Builder(svgProcessor); | ||
@@ -41,6 +46,3 @@ } | ||
function testForSymbols($loadedSVG, expectedSymbolIds, opts = {}) { | ||
// var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
// var $ = cheerio.load(fileContent, { xmlMode: true }); | ||
function testForSymbols($loadedSVG, expectedSymbolIds) { | ||
// test proper structure | ||
@@ -60,5 +62,4 @@ var $svg = $loadedSVG('svg'); | ||
var $symbol; | ||
expectedSymbolIds.forEach(function (id, idx) { | ||
$symbol = $loadedSVG('svg #' + id).first(); | ||
var $symbol = $loadedSVG('svg #' + id).first(); | ||
expect($symbol[0].tagName).to.equal('symbol'); | ||
@@ -119,3 +120,3 @@ expect($symbol.attr('id')).to.equal(id); | ||
it('writes all SVGs in a single directory to the target outputFile', function(done) { | ||
it('writes all SVGs in a single directory to the target outputFile', function() { | ||
var inputNodes = [SOURCE_DIR_GROUP_1]; | ||
@@ -128,7 +129,6 @@ builder = makeBuilderFromInputNodes(inputNodes); | ||
testForSymbols(loadSVG(outputDestination), ID_MANIFEST[SOURCE_DIR_GROUP_1]); | ||
done(); | ||
}); | ||
}); | ||
it('writes all SVGs from a list of directories to the target outputFile', function(done) { | ||
it('writes all SVGs from a list of directories to the target outputFile', function() { | ||
var inputNodes = [SOURCE_DIR_GROUP_1, SOURCE_DIR_GROUP_2]; | ||
@@ -142,3 +142,2 @@ builder = makeBuilderFromInputNodes(inputNodes); | ||
testForSymbols(loadSVG(outputDestination), symbolIds); | ||
done(); | ||
}); | ||
@@ -148,3 +147,3 @@ }); | ||
// TODO: Implement test | ||
it('passes options to SVGStore', function(done) { | ||
it('passes options to SVGStore', function() { | ||
var inputNode = SOURCE_DIR_SVGSTORE_OPTS; | ||
@@ -167,7 +166,26 @@ var CUSTOM_ATTR_VALUES = { | ||
expect($('symbol').attr('x-custom-attr')).to.equal(CUSTOM_ATTR_VALUES['x-custom-attr']); | ||
done(); | ||
}); | ||
}); | ||
it('enables per-file configuration via a `fileSettings` hash', function() { | ||
var inputNodes = [SOURCE_DIR_GROUP_1]; | ||
var customIDs = ['customID-1', 'customID-2', 'customID-3']; | ||
var fileSettings = { | ||
[ID_MANIFEST[SOURCE_DIR_GROUP_1][0]]: { id: customIDs[0] }, | ||
[ID_MANIFEST[SOURCE_DIR_GROUP_1][1]]: { id: customIDs[1] }, | ||
[ID_MANIFEST[SOURCE_DIR_GROUP_1][2]]: { id: customIDs[2] } | ||
}; | ||
builder = makeBuilderFromInputNodes(inputNodes, { fileSettings }); | ||
return builder.build().then(function (results) { | ||
var outputDestination = path.join(results.directory, path.normalize(OUTPUT_FILE)); | ||
var symbolIds = customIDs.concat(ID_MANIFEST[SOURCE_DIR_GROUP_1][3]); | ||
var $ = loadSVG(outputDestination); | ||
testForSymbols($, symbolIds); | ||
}); | ||
}); | ||
}); | ||
}); | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
506360
5
19
282
102
4
- Removedcheerio@^0.20.0