Comparing version 2.0.1 to 2.0.2
@@ -13,15 +13,22 @@ 'use strict'; | ||
try { | ||
const config = require(configFile); | ||
if (config == null || typeof config !== 'object' || Array.isArray(config)) { | ||
throw Error(`Invalid config file "${configFile}"`); | ||
} | ||
return config; | ||
} catch (error) { | ||
if (error.code === 'MODULE_NOT_FOUND') { | ||
return null; | ||
} | ||
throw error; | ||
await fs.promises.access(configFile); | ||
} catch { | ||
return null; | ||
} | ||
const config = require(configFile); | ||
if (config == null || typeof config !== 'object' || Array.isArray(config)) { | ||
throw Error(`Invalid config file "${configFile}"`); | ||
} | ||
return config; | ||
}; | ||
const isFile = async (file) => { | ||
try { | ||
const stats = await fs.promises.stat(file); | ||
return stats.isFile(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
const loadConfig = async (configFile, cwd = process.cwd()) => { | ||
@@ -37,9 +44,6 @@ if (configFile != null) { | ||
while (true) { | ||
try { | ||
const file = path.join(dir, "svgo.config.js"); | ||
const stats = await fs.promises.stat(file); | ||
if (stats.isFile()) { | ||
return await importConfig(file); | ||
} | ||
} catch {} | ||
const file = path.join(dir, "svgo.config.js"); | ||
if (await isFile(file)) { | ||
return await importConfig(file); | ||
} | ||
const parent = path.dirname(dir); | ||
@@ -46,0 +50,0 @@ if (dir === parent) { |
@@ -12,3 +12,2 @@ /* jshint quotmark: false */ | ||
const regSVGFile = /\.svg$/i; | ||
const noop = () => {}; | ||
@@ -101,3 +100,3 @@ /** | ||
if (parseFloat(process.versions.node) < parseFloat(nodeVersion)) { | ||
return printErrorAndExit(`Error: ${PKG.name} requires Node.js version ${nodeVersion} or higher.`); | ||
throw Error(`${PKG.name} requires Node.js version ${nodeVersion} or higher.`); | ||
} | ||
@@ -107,9 +106,5 @@ } | ||
// --config | ||
try { | ||
const loadedConfig = await loadConfig(opts.config); | ||
if (loadedConfig != null) { | ||
config = loadedConfig; | ||
} | ||
} catch (error) { | ||
return printErrorAndExit(error.message); | ||
const loadedConfig = await loadConfig(opts.config); | ||
if (loadedConfig != null) { | ||
config = loadedConfig; | ||
} | ||
@@ -172,3 +167,3 @@ | ||
var ouputFolder = output && output[0] || opts.folder; | ||
return optimizeFolder(config, opts.folder, ouputFolder).then(noop, printErrorAndExit); | ||
await optimizeFolder(config, opts.folder, ouputFolder); | ||
} | ||
@@ -190,4 +185,5 @@ | ||
} else { | ||
return Promise.all(input.map((file, n) => optimizeFile(config, file, output[n]))) | ||
.then(noop, printErrorAndExit); | ||
await Promise.all( | ||
input.map((file, n) => optimizeFile(config, file, output[n])) | ||
); | ||
} | ||
@@ -398,13 +394,2 @@ | ||
/** | ||
* Write an error and exit. | ||
* @param {Error} error | ||
* @return {Promise} a promise for running tests | ||
*/ | ||
function printErrorAndExit(error) { | ||
console.error(chalk.red(error)); | ||
process.exit(1); | ||
return Promise.reject(error); // for tests | ||
} | ||
module.exports.checkIsDir = checkIsDir; |
@@ -15,7 +15,3 @@ 'use strict'; | ||
prefix += ';base64,'; | ||
if (Buffer.from) { | ||
str = prefix + Buffer.from(str).toString('base64'); | ||
} else { | ||
str = prefix + new Buffer(str).toString('base64'); | ||
} | ||
str = prefix + Buffer.from(str).toString('base64'); | ||
} else if (type === 'enc') { | ||
@@ -48,3 +44,3 @@ // URI encoded | ||
// base64 | ||
str = new Buffer(data, 'base64').toString('utf8'); | ||
str = Buffer.from(data, 'base64').toString('utf8'); | ||
} else if (data.charAt(0) === '%') { | ||
@@ -51,0 +47,0 @@ // URI encoded |
{ | ||
"name": "svgo", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Nodejs-based tool for optimizing SVG vector graphics files", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -32,3 +32,3 @@ /* jshint quotmark: false */ | ||
// Important rule | ||
rImportant = '(\\s*!important(?![-(\w]))?', | ||
rImportant = '(\\s*!important(?![-(\\w]))?', | ||
@@ -35,0 +35,0 @@ // Final RegExp to parse CSS declarations. |
307
README.md
@@ -1,7 +0,4 @@ | ||
**english** | [русский](https://github.com/svg/svgo/blob/master/README.ru.md) | ||
- - - | ||
<img src="https://svg.github.io/svgo-logo.svg" width="200" height="200" alt="logo"/> | ||
## SVGO [](https://npmjs.org/package/svgo) [](https://travis-ci.org/svg/svgo) [](https://coveralls.io/r/svg/svgo?branch=master) | ||
## SVGO [](https://npmjs.org/package/svgo) | ||
@@ -15,8 +12,193 @@ **SVG O**ptimizer is a Nodejs-based tool for optimizing SVG vector graphics files. | ||
## What it can do | ||
## Installation | ||
```sh | ||
npm -g svgo | ||
``` | ||
or | ||
```sh | ||
yarn global add svgo | ||
``` | ||
## CLI usage | ||
```sh | ||
svgo one.svg two.svg -p one.min.svg two.min.svg | ||
``` | ||
Or use --folder, -f flag to optimize whole folder of svg icons | ||
```sh | ||
svgo -f ./path/to/folder/with/svg/files -o ./path/to/folder/with/svg/output | ||
``` | ||
See help for advanced usage | ||
```sh | ||
svgo --help | ||
``` | ||
## Configuration | ||
Some options can be configured with CLI though it may be easier to have configuration in separate file. | ||
SVGO automatically loads configuration from `svgo.config.js` or module specified with `--config` flag. | ||
```js | ||
module.exports = { | ||
multipass: true, // boolean. false by default | ||
datauri: 'enc', // 'base64', 'enc' or 'unenc'. 'base64' by default | ||
js2svg: { | ||
indent: 2, // string with spaces or number of spaces. 4 by default | ||
pretty: true, // boolean, false by default | ||
} | ||
} | ||
``` | ||
SVGO has a plugin-based architecture, so almost every optimization is a separate plugin. | ||
There is a set of [builtin plugins](#builtin-plugins). See how to configure them: | ||
Today we have: | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
// enable builtin plugin by name | ||
'builtinPluginName', | ||
// or by expanded version | ||
{ | ||
name: 'builtinPluginName' | ||
}, | ||
// some plugins allow/require to pass options | ||
{ | ||
name: 'builtinPluginName', | ||
params: { | ||
optionName: 'optionValue' | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
If `plugins` field is specified default list is fully overrided. To extend default | ||
list use `extendDefaultPlugins` utility: | ||
```js | ||
const { extendDefaultPlugins } = require('svgo'); | ||
module.exports = { | ||
plugins: extendDefaultPlugins([ | ||
{ | ||
name: 'builtinPluginName', | ||
params: { | ||
optionName: 'optionValue' | ||
} | ||
} | ||
]) | ||
} | ||
``` | ||
To disable one of default plugins use `active` field: | ||
```js | ||
const { extendDefaultPlugins } = require('svgo'); | ||
module.exports = { | ||
plugins: extendDefaultPlugins([ | ||
{ | ||
name: 'builtinPluginName', | ||
active: false | ||
} | ||
]) | ||
} | ||
``` | ||
See the list of default plugins: | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
'removeDoctype', | ||
'removeXMLProcInst', | ||
'removeComments', | ||
'removeMetadata', | ||
'removeEditorsNSData', | ||
'cleanupAttrs', | ||
'inlineStyles', | ||
'minifyStyles', | ||
'convertStyleToAttrs', | ||
'cleanupIDs', | ||
'removeUselessDefs', | ||
'cleanupNumericValues', | ||
'convertColors', | ||
'removeUnknownsAndDefaults', | ||
'removeNonInheritableGroupAttrs', | ||
'removeUselessStrokeAndFill', | ||
'removeViewBox', | ||
'cleanupEnableBackground', | ||
'removeHiddenElems', | ||
'removeEmptyText', | ||
'convertShapeToPath', | ||
'convertEllipseToCircle', | ||
'moveElemsAttrsToGroup', | ||
'moveGroupAttrsToElems', | ||
'collapseGroups', | ||
'convertPathData', | ||
'convertTransform', | ||
'removeEmptyAttrs', | ||
'removeEmptyContainers', | ||
'mergePaths', | ||
'removeUnusedNS', | ||
'sortDefsChildren', | ||
'removeTitle', | ||
'removeDesc' | ||
] | ||
} | ||
``` | ||
It's also possible to specify custom plugin: | ||
```js | ||
const anotherCustomPlugin = require('./another-custom-plugin.js') | ||
module.exports = { | ||
plugins: [ | ||
{ | ||
name: 'customPluginName', | ||
type: 'perItem', // 'perItem', 'perItemReverse' or 'full' | ||
params: { | ||
optionName: 'optionValue', | ||
}, | ||
fn: (ast, params, info) => {} | ||
}, | ||
anotherCustomPlugin | ||
] | ||
} | ||
``` | ||
## API usage | ||
SVGO provides a few low level utilities. `extendDefaultPlugins` is described above. | ||
### optimize | ||
The core of SVGO is `optimize` function. | ||
```js | ||
const { optimize } = require('svgo'); | ||
const result = optimize(svgString, { | ||
// optional but recommended field | ||
path: 'path-to.svg', | ||
// all config fields are also available here | ||
multipass: true | ||
}) | ||
const optimizedSvgString = result.data | ||
``` | ||
### loadConfig | ||
If you write a tool on top of svgo you might need a way to load svgo config. | ||
```js | ||
const { loadConfig } = require('svgo'); | ||
const config = await loadConfig() | ||
// you can also specify relative or absolute path and customize current working directory | ||
const config = await loadConfig(configFile, cwd) | ||
``` | ||
## Builtin plugins | ||
| Plugin | Description | Default | | ||
@@ -74,115 +256,4 @@ | ------ | ----------- | ------- | | ||
Want to know how it works and how to write your own plugin? [Of course you want to](https://github.com/svg/svgo/blob/master/docs/how-it-works/en.md). ([동작방법](https://github.com/svg/svgo/blob/master/docs/how-it-works/ko.md)) | ||
## Other Ways to Use SVGO | ||
## Installation | ||
```sh | ||
$ [sudo] npm install -g svgo | ||
``` | ||
## Usage | ||
### <abbr title="Command Line Interface">CLI</abbr> | ||
``` | ||
Usage: | ||
svgo [OPTIONS] [ARGS] | ||
Options: | ||
-h, --help : Help | ||
-v, --version : Version | ||
-i INPUT, --input=INPUT : Input file, "-" for STDIN | ||
-s STRING, --string=STRING : Input SVG data string | ||
-f FOLDER, --folder=FOLDER : Input folder, optimize and rewrite all *.svg files | ||
-o OUTPUT, --output=OUTPUT : Output file or folder (by default the same as the input), "-" for STDOUT | ||
-p PRECISION, --precision=PRECISION : Set number of digits in the fractional part, overrides plugins params | ||
--config=CONFIG : Config file to customize default behavior | ||
--datauri=DATAURI : Output as Data URI string (base64, URI encoded or unencoded) | ||
--multipass : Pass over SVGs multiple times to ensure all optimizations are applied | ||
--pretty : Make SVG pretty printed | ||
--indent=INDENT : Indent number when pretty printing SVGs | ||
-r, --recursive : Use with '-f'. Optimizes *.svg files in folders recursively. | ||
-q, --quiet : Only output error messages, not regular status messages | ||
--show-plugins : Show available plugins and exit | ||
Arguments: | ||
INPUT : Alias to --input | ||
``` | ||
* with files: | ||
```sh | ||
$ svgo test.svg | ||
``` | ||
or: | ||
```sh | ||
$ svgo *.svg | ||
``` | ||
Windows does not support glob expansion. The command above will not work on Windows. | ||
```sh | ||
$ svgo test.svg -o test.min.svg | ||
``` | ||
```sh | ||
$ svgo test.svg other.svg third.svg | ||
``` | ||
```sh | ||
$ svgo test.svg other.svg third.svg -o test.min.svg -o other.min.svg -o third.min.svg | ||
``` | ||
* with STDIN / STDOUT: | ||
```sh | ||
$ cat test.svg | svgo -i - -o - > test.min.svg | ||
``` | ||
* with folder | ||
```sh | ||
$ svgo -f ../path/to/folder/with/svg/files | ||
``` | ||
or: | ||
```sh | ||
$ svgo -f ../path/to/folder/with/svg/files -o ../path/to/folder/with/svg/output | ||
``` | ||
```sh | ||
$ svgo *.svg -o ../path/to/folder/with/svg/output | ||
``` | ||
* with strings: | ||
```sh | ||
$ svgo -s '<svg version="1.1">test</svg>' -o test.min.svg | ||
``` | ||
or even with Data URI base64: | ||
```sh | ||
$ svgo -s 'data:image/svg+xml;base64,...' -o test.min.svg | ||
``` | ||
* with SVGZ: | ||
from `.svgz` to `.svg`: | ||
```sh | ||
$ gunzip -c test.svgz | svgo -i - -o test.min.svg | ||
``` | ||
from `.svg` to `.svgz`: | ||
```sh | ||
$ svgo test.svg -o - | gzip -cfq9 > test.svgz | ||
``` | ||
### Other Ways to Use SVGO | ||
* as a web app – [SVGOMG](https://jakearchibald.github.io/svgomg/) | ||
@@ -189,0 +260,0 @@ * as a GitHub Action – [SVGO Action](https://github.com/marketplace/actions/svgo-action) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
291
1801761
71
54267