svgo-loader
Advanced tools
Comparing version 2.2.2 to 3.0.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 @@ ] |
@@ -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 @@ ] |
80
index.js
@@ -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" | ||
} | ||
} |
130
README.md
@@ -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). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
2
3
0
24770
115
128
+ Addedsvgo@^2.2.0
+ Added@trysound/sax@0.2.0(transitive)
+ Addedcommander@7.2.0(transitive)
+ Addedcss-select@4.3.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddom-serializer@1.4.1(transitive)
+ Addeddomhandler@4.3.1(transitive)
+ Addeddomutils@2.8.0(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedpicocolors@1.1.0(transitive)
+ Addedsvgo@2.8.0(transitive)
- Removedjs-yaml@^3.13.1
- Removed@types/q@1.5.8(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarray.prototype.reduce@1.0.7(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcoa@2.0.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcss-select@2.1.0(transitive)
- Removedcss-select-base-adapter@0.1.1(transitive)
- Removedcss-tree@1.0.0-alpha.37(transitive)
- Removedcss-what@3.4.2(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddom-serializer@0.2.2(transitive)
- Removeddomelementtype@1.3.1(transitive)
- Removeddomutils@1.7.0(transitive)
- Removedes-abstract@1.23.3(transitive)
- Removedes-array-method-boxes-properly@1.0.0(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedmdn-data@2.0.4(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednth-check@1.0.2(transitive)
- Removedobject-inspect@1.13.2(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedobject.getownpropertydescriptors@2.1.8(transitive)
- Removedobject.values@1.2.0(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedq@1.5.1(transitive)
- Removedregexp.prototype.flags@1.5.2(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedsax@1.2.4(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedsvgo@1.3.2(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedunquote@1.1.1(transitive)
- Removedutil.promisify@1.0.1(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)