Socket
Socket
Sign inDemoInstall

svgo-loader

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svgo-loader - npm Package Compare versions

Comparing version 2.2.2 to 3.0.0

example/external-config/svgo.config.js

11

example/basic/webpack.config.js

@@ -14,12 +14,7 @@ module.exports = {

use: [
{loader: 'file-loader'},
{
loader: 'file-loader'
},
{
loader: '../../index.js',
options: {
plugins: [
{removeTitle: true},
{convertColors: {shorthex: false}},
{convertPathData: false}
]
}
}

@@ -26,0 +21,0 @@ ]

@@ -14,8 +14,7 @@ module.exports = {

use: [
{loader: "file-loader"},
{
loader: "file-loader"
},
{
loader: "../../index.js",
options: {
externalConfig: "svgo-config.yml"
}
}

@@ -22,0 +21,0 @@ ]

@@ -14,12 +14,7 @@ module.exports = {

use: [
{loader: 'file-loader'},
{
loader: 'file-loader'
},
{
loader: '../../index.js',
options: {
plugins: [
{removeTitle: true},
{convertColors: {shorthex: false}},
{convertPathData: false}
]
}
}

@@ -26,0 +21,0 @@ ]

@@ -1,66 +0,24 @@

var Svgo = require('svgo');
var loaderUtils = require('loader-utils');
var yaml = require('js-yaml');
var fs = require('fs');
var path = require('path');
const { optimize, loadConfig } = require('svgo');
const loaderUtils = require('loader-utils');
module.exports = function(source) {
this.cacheable(true);
var callback = this.async();
var config = loaderUtils.getOptions(this) || {};
// This piece of code exists in order to support webpack 1.x.x top level configurations.
// In webpack 2 this options does not exists anymore.
// Please use the `options` field of the loader to pass your configuration
if (config.useConfig) {
var configName = config.useConfig;
var config = this.options[configName];
if (config === undefined) {
callback(new Error(
'You specified "useConfig=' + configName +
'" for svgo-loader, but there is no property named "' + configName +
'" in your main webpack configuration.'
));
return;
}
async function loader(source) {
const { configFile, ...options } = loaderUtils.getOptions(this) || {};
let config;
if (typeof configFile === 'string') {
config = await loadConfig(configFile, this.context);
} else if (configFile !== false) {
config = await loadConfig(null, this.context);
}
// Ported from:
// https://github.com/svg/svgo/blob/174c37208017e5909d5f7db2e8faba49663a945a/lib/svgo/coa.js#L175-L192
if (typeof config.externalConfig === 'string') {
var configPath = config.externalConfig;
var configText;
try {
configText = fs.readFileSync(path.resolve(configPath), 'utf8');
config = JSON.parse(configText);
} catch (err) {
if (err.code === 'ENOENT') {
callback(new Error("Couldn't find file with external svgo config '" + configPath + "'."));
return;
} else if (err.code === 'EISDIR') {
callback(new Error("Directory '" + configPath + "' is not a config file."));
return;
}
config = yaml.safeLoad(configText);
if (!config || Array.isArray(config)) {
callback(new Error("Invalid external svgo config file '" + configPath + "'."));
return;
}
}
const result = optimize(source, { path: this.resourcePath, ...config, ...options });
if (result.error) {
throw Error(result.error);
}
return result.data;
}
var svgo = new Svgo({ ...config });
svgo.optimize(source, { path: this.resourcePath })
.then(function(result) {
callback(null, result.data);
return;
}, function(error) {
if (error instanceof Error) {
callback(error);
return;
}
callback(new Error(error));
return;
});
module.exports = function (source) {
const callback = this.async();
loader.call(this, source)
.then(result => callback(null, result))
.catch(error => callback(error));
};
{
"name": "svgo-loader",
"version": "2.2.2",
"version": "3.0.0",
"description": "svgo loader for webpack",

@@ -25,13 +25,9 @@ "main": "index.js",

"file-loader": "^2.0.0",
"svgo": "^1.0.5",
"webpack": "^4.17.2",
"webpack-cli": "^3.1.0"
},
"peerDependencies": {
"svgo": "^1.0.0"
},
"dependencies": {
"js-yaml": "^3.13.1",
"loader-utils": "^1.0.3"
"loader-utils": "^1.0.3",
"svgo": "^2.2.0"
}
}

@@ -6,3 +6,3 @@ # [svgo](https://github.com/svg/svgo) loader for [webpack](https://github.com/webpack/webpack)

```
$ npm install svgo svgo-loader --save-dev
$ npm install svgo-loader --save-dev
```

@@ -13,16 +13,10 @@

```
$ yarn add svgo svgo-loader -D
$ yarn add svgo-loader -D
```
DON'T FORGET TO INSTALL / UPDATE THE `svgo` PACKAGE after you update `svg-loader` (see [#20](https://github.com/rpominov/svgo-loader/issues/20))
## Usage
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
[Documentation: Using loaders](https://webpack.js.org/concepts/loaders/#using-loaders)
Svgo-loader just passes config to the [svgo](https://github.com/svg/svgo) library.
### Put the SVGO config into loader's `options`
``` javascript
```js
module.exports = {

@@ -35,12 +29,7 @@ ...,

use: [
{loader: 'file-loader'},
{
loader: 'file-loader'
},
{
loader: 'svgo-loader',
options: {
plugins: [
{removeTitle: true},
{convertColors: {shorthex: false}},
{convertPathData: false}
]
}
}

@@ -54,5 +43,8 @@ ]

### Or use an external config like you would with SVGO CLI
By default svgo-loader uses config from `svgo.config.js` similar to svgo cli.
See [how to configure svgo](https://github.com/svg/svgo#configuration).
``` javascript
Specify configFile option to load custom config module:
```js
module.exports = {

@@ -65,7 +57,9 @@ ...,

use: [
{loader: 'file-loader'},
{
loader: 'file-loader'
},
{
loader: 'svgo-loader',
options: {
externalConfig: "svgo-config.yml"
configFile: './scripts/svgo.config.js'
}

@@ -80,39 +74,21 @@ }

In `svgo-config.yml`:
or to disable loading config:
```yml
plugins:
- removeTitle: true
- convertPathData: false
- convertColors:
shorthex: false
```
You can use `YML` or `JSON` files as external configs.
### Legacy Webpack v1 config
There are two ways of loading svgo configuration.
You can pass it as a JSON string after loader name, like this:
``` javascript
// webpack.config.js
var svgoConfig = JSON.stringify({
plugins: [
{removeTitle: true},
{convertColors: {shorthex: false}},
{convertPathData: false}
]
});
```js
module.exports = {
...
...,
module: {
loaders: [
rules: [
{
test: /.*\.svg$/,
loaders: [
'file-loader',
'svgo-loader?' + svgoConfig
test: /\.svg$/,
use: [
{
loader: 'file-loader'
},
{
loader: 'svgo-loader',
options: {
configFile: false
}
}
]

@@ -125,32 +101,34 @@ }

Or you can save svgo config in your main webpack config object,
and then specify name of the property in the loader query string.
You can also specify options which override loaded from config
However, this option will not work in Webpack 2.<br>
This is only shown here in the documentation for backwards compatibility.
``` javascript
// webpack.config.js
```js
module.exports = {
...
...,
module: {
loaders: [
rules: [
{
test: /.*\.svg$/,
loaders: [
'file-loader',
'svgo-loader?useConfig=svgoConfig1'
test: /\.svg$/,
use: [
{
loader: 'file-loader'
},
{
loader: 'svgo-loader',
options: {
multipass: true,
js2svg: {
indent: 2,
pretty: true,
}
}
}
]
}
]
},
svgoConfig1: {
plugins: [
{removeTitle: true},
{convertColors: {shorthex: false}},
{convertPathData: false}
]
}
}
```
## License and Copyright
This software is released under the terms of the [MIT license](https://github.com/rpominov/svgo-loader/blob/master/LICENSE).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc