1 | "use strict";
|
2 |
|
3 | var _pluginError = _interopRequireDefault(require("plugin-error"));
|
4 |
|
5 | var _stream = require("stream");
|
6 |
|
7 | var _svgo = require("svgo");
|
8 |
|
9 | var _getSvgoConfig = require("./get-svgo-config.js");
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | const PLUGIN_NAME = 'gulp-svgmin';
|
14 |
|
15 | module.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);
|
29 |
|
30 | if (typeof result.data === 'string') {
|
31 | file.contents = Buffer.from(result.data);
|
32 | return cb(null, file);
|
33 | }
|
34 |
|
35 |
|
36 | throw result.error;
|
37 | }).catch(error => cb(new _pluginError.default(PLUGIN_NAME, error)));
|
38 | return;
|
39 | }
|
40 |
|
41 |
|
42 | return cb(null, file);
|
43 | };
|
44 |
|
45 | return stream;
|
46 | }; |
\ | No newline at end of file |