UNPKG

1.39 kBJavaScriptView Raw
1"use strict";
2
3var _pluginError = _interopRequireDefault(require("plugin-error"));
4
5var _stream = require("stream");
6
7var _svgo = require("svgo");
8
9var _getSvgoConfig = require("./get-svgo-config.js");
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13const PLUGIN_NAME = 'gulp-svgmin';
14
15module.exports = function (options) {
16 const optionsFunction = typeof options === 'function';
17 const stream = new _stream.Transform({
18 objectMode: true
19 });
20
21 stream._transform = function (file, encoding, cb) {
22 if (file.isStream()) {
23 return cb(new _pluginError.default(PLUGIN_NAME, 'Streaming not supported'));
24 }
25
26 if (file.isBuffer()) {
27 (0, _getSvgoConfig.getSvgoConfig)(optionsFunction ? options(file) : options, optionsFunction).then(config => {
28 const result = (0, _svgo.optimize)(String(file.contents), config); // Ignore svgo meta data and return the SVG string.
29
30 if (typeof result.data === 'string') {
31 file.contents = Buffer.from(result.data);
32 return cb(null, file);
33 } // Otherwise, throw an error, even if it is undefined.
34
35
36 throw result.error;
37 }).catch(error => cb(new _pluginError.default(PLUGIN_NAME, error)));
38 return;
39 } // Handle all other cases, like file.isNull(), file.isDirectory().
40
41
42 return cb(null, file);
43 };
44
45 return stream;
46};
\No newline at end of file