| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = svgoPlugin;
|
|---|
| 5 |
|
|---|
| 6 | var _svgo = _interopRequireDefault(require("svgo"));
|
|---|
| 7 |
|
|---|
| 8 | var _cosmiconfig = require("cosmiconfig");
|
|---|
| 9 |
|
|---|
| 10 | var _config = require("./config");
|
|---|
| 11 |
|
|---|
| 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 13 |
|
|---|
| 14 | /* eslint-disable no-underscore-dangle */
|
|---|
| 15 | const explorer = (0, _cosmiconfig.cosmiconfigSync)('svgo', {
|
|---|
| 16 | searchPlaces: ['package.json', '.svgorc', '.svgorc.js', '.svgorc.json', '.svgorc.yaml', '.svgorc.yml', 'svgo.config.js', '.svgo.yml'],
|
|---|
| 17 | transform: result => result && result.config,
|
|---|
| 18 | cache: true
|
|---|
| 19 | });
|
|---|
| 20 |
|
|---|
| 21 | function encodeSVGDatauri(str, type) {
|
|---|
| 22 | let prefix = 'data:image/svg+xml'; // base64
|
|---|
| 23 |
|
|---|
| 24 | if (!type || type === 'base64') {
|
|---|
| 25 | prefix += ';base64,';
|
|---|
| 26 |
|
|---|
| 27 | if (Buffer.from) {
|
|---|
| 28 | str = prefix + Buffer.from(str).toString('base64');
|
|---|
| 29 | } else {
|
|---|
| 30 | // eslint-disable-next-line
|
|---|
| 31 | str = prefix + new Buffer(str).toString('base64');
|
|---|
| 32 | } // URI encoded
|
|---|
| 33 |
|
|---|
| 34 | } else if (type === 'enc') {
|
|---|
| 35 | str = `${prefix},${encodeURIComponent(str)}`; // unencoded
|
|---|
| 36 | } else if (type === 'unenc') {
|
|---|
| 37 | str = `${prefix},${str}`;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | return str;
|
|---|
| 41 | } // See https://github.com/svg/svgo/blob/master/lib/svgo.js#L24
|
|---|
| 42 | // _optimizeOnce is synchronous internally
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | function optimizeSync(svgstr, info) {
|
|---|
| 46 | const {
|
|---|
| 47 | config
|
|---|
| 48 | } = this;
|
|---|
| 49 |
|
|---|
| 50 | if (config.error) {
|
|---|
| 51 | throw config.error;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | const maxPassCount = config.multipass ? 10 : 1;
|
|---|
| 55 | let counter = 0;
|
|---|
| 56 | let prevResultSize = Number.POSITIVE_INFINITY;
|
|---|
| 57 | let result;
|
|---|
| 58 |
|
|---|
| 59 | const optimizeOnceCallback = svgjs => {
|
|---|
| 60 | if (svgjs.error) {
|
|---|
| 61 | throw svgjs.error;
|
|---|
| 62 | } // eslint-disable-next-line no-plusplus
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
|
|---|
| 66 | prevResultSize = svgjs.data.length;
|
|---|
| 67 |
|
|---|
| 68 | this._optimizeOnce(svgjs.data, info, optimizeOnceCallback);
|
|---|
| 69 | } else {
|
|---|
| 70 | if (config.datauri) {
|
|---|
| 71 | svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (info.path) {
|
|---|
| 75 | svgjs.path = info.path;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | result = svgjs;
|
|---|
| 79 | }
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | this._optimizeOnce(svgstr, info, optimizeOnceCallback);
|
|---|
| 83 |
|
|---|
| 84 | return result;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | function createSvgo(config, rcConfig) {
|
|---|
| 88 | const baseSvgoConfig = (0, _config.getBaseSvgoConfig)(config);
|
|---|
| 89 | const mergedConfig = (0, _config.mergeSvgoConfig)(baseSvgoConfig, rcConfig, config.svgoConfig);
|
|---|
| 90 | return new _svgo.default(mergedConfig);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | function getInfo(state) {
|
|---|
| 94 | return state.filePath ? {
|
|---|
| 95 | input: 'file',
|
|---|
| 96 | path: state.filePath
|
|---|
| 97 | } : {
|
|---|
| 98 | input: 'string'
|
|---|
| 99 | };
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | function svgoPlugin(code, config, state) {
|
|---|
| 103 | if (!config.svgo) return code;
|
|---|
| 104 | const filePath = (0, _config.getFilePath)(state);
|
|---|
| 105 | const svgoRcConfig = config.runtimeConfig ? explorer.search(filePath) : {};
|
|---|
| 106 | const svgo = createSvgo(config, svgoRcConfig);
|
|---|
| 107 | const {
|
|---|
| 108 | data
|
|---|
| 109 | } = optimizeSync.call(svgo, code, getInfo(state));
|
|---|
| 110 | return data;
|
|---|
| 111 | } |
|---|