-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (78 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const fs = require('fs');
const path = require('path');
const objectAssign = require('object-assign');
const mkdirp = require('mkdirp');
const Plugin = require('broccoli-caching-writer');
const helpers = require('broccoli-kitchen-sink-helpers');
const svgstore = require('misvg');
const babel = require('babel-core');
const es2015 = require('babel-preset-es2015');
const defaultSettings = {
outputFile: '/svg-sprites.js',
annotation: 'misvg plugin',
misvgOptions: {}
};
// TOOD: Perhaps be a bit more robust (and thus, more explicit about the proper API) with validation
const validationErrorPrefix = 'Expected a non-falsey argument for `_inputNode`, got ';
function MisvgPlugin(_inputNode, _options) {
if (!(this instanceof MisvgPlugin)) {
return new MisvgPlugin(_inputNode, _options);
}
const options = objectAssign({}, defaultSettings, _options);
if (options.name) {
this._name = options.name;
} else {
this._name = (this.constructor && this.constructor.name) ? this.constructor.name : 'MisvgSprites';
}
this._annotation = options.annotation;
this._options = options;
const label = this._name + ' (' + this._annotation + ')';
if (!_inputNode) {
throw new TypeError(label + ': ' + validationErrorPrefix + _inputNode);
}
const inputNodes = Array.isArray(_inputNode) ? _inputNode : [_inputNode];
Plugin.call(this, inputNodes, this._options);
}
MisvgPlugin.prototype = Object.create(Plugin.prototype);
MisvgPlugin.prototype.constructor = MisvgPlugin;
MisvgPlugin.prototype.description = 'misvg';
/**
* Overrides broccoli-plugin's `build' function.
* @see: https://github.com/broccolijs/broccoli-plugin#pluginprototypebuild
* @returns {*}
*/
MisvgPlugin.prototype.build = function () {
const svgOutput = svgstore(this._options.misvgOptions);
try {
// Iterate through `inputPaths` of our `inputNodes` (`inputPaths` is an array of
// paths on disk corresponding to each node in `inputNodes`)
for (let i = 0, l = this.inputPaths.length; i < l; i++) {
const srcDir = this.inputPaths[i];
const inputFiles = helpers.multiGlob(['**/*.svg'], {cwd: srcDir});
for (let j = 0, ll = inputFiles.length; j < ll; j++) {
const inputFileName = inputFiles[j];
const inputFilePath = path.join(srcDir, inputFileName);
const stat = fs.statSync(inputFilePath);
if (stat && stat.isFile()) {
const fileNameWithoutExtension = inputFileName.replace(/\.[^.]+$/, '');
const fileContent = fs.readFileSync(inputFilePath, {encoding: 'utf8'});
svgOutput.add(fileNameWithoutExtension, fileContent);
}
}
}
} catch (err) {
if (!err.message.match('did not match any files')) {
throw err;
}
}
helpers.assertAbsolutePaths([this.outputPath]); // ❓❓ QUESTION: Necessary?
const outputDestination = path.join(this.outputPath, this._options.outputFile);
mkdirp.sync(path.dirname(outputDestination));
const sprites = 'let MISVG_STORE = ' + svgOutput.getObjectString() + ';';
const result = babel.transform(sprites, {
presets: [es2015]
});
return fs.writeFileSync(outputDestination, result.code);
};
module.exports = MisvgPlugin;