Skip to content

Commit

Permalink
Merge pull request #2 from yoriiis/update-config
Browse files Browse the repository at this point in the history
Update the svgo and svgstore default configuration
  • Loading branch information
yoriiis committed Feb 8, 2021
2 parents f4b990d + f5cd9bd commit 1fd1a3c
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 66 deletions.
1 change: 1 addition & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MD013: false # line-length
MD024: false # Multiple headers with the same content
MD036: false # no-emphasis-as-heading
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 2.0.1

### Updates

- Remove the default `svgo` config to avoid conflicts when merging config (svgo config object array is malformed).
- Remove the default `svgstore` config to allow user to override them (svgAttrs cannot be overriden otherwise).
- Move the configuration of these package into the webpack configuration for example purpose and to keep the TUs up to date.

## 2.0.0

### Updates
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SvgChunkWebpackPlugin

![SvgChunkWebpackPlugin](https://img.shields.io/badge/svg--chunk--webpack--plugin-v2.0.0-29008a.svg?style=for-the-badge) ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/yoriiis/svg-chunk-webpack-plugin/Build/master?style=for-the-badge) [![Coverage Status](https://img.shields.io/coveralls/github/yoriiis/svg-chunk-webpack-plugin?style=for-the-badge)](https://coveralls.io/github/yoriiis/svg-chunk-webpack-plugin?branch=master) ![Node.js](https://img.shields.io/node/v/svg-chunk-webpack-plugin?style=for-the-badge)
![SvgChunkWebpackPlugin](https://img.shields.io/badge/svg--chunk--webpack--plugin-v2.0.1-29008a.svg?style=for-the-badge) ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/yoriiis/svg-chunk-webpack-plugin/Build/master?style=for-the-badge) [![Coverage Status](https://img.shields.io/coveralls/github/yoriiis/svg-chunk-webpack-plugin?style=for-the-badge)](https://coveralls.io/github/yoriiis/svg-chunk-webpack-plugin?branch=master) ![Node.js](https://img.shields.io/node/v/svg-chunk-webpack-plugin?style=for-the-badge)

> Generate SVG sprites according to entrypoint dependencies. Each page only imports its own svgs, wrapped as a sprite and optimized by svgo.
The SvgChunkWebpackPlugin creates optimized SVG sprites, according to Webpack's entrypoints. Each sprite contains only the SVG dependencies listed on its entrypoints to improved code splitting, even on SVG files.

The plugin includes the popular [svgo](https://github.com/svg/svgo) package with the optimized settings, to generates clean and optimized SVG sprites.
The plugin includes the popular [svgo](https://github.com/svg/svgo) package to generates clean and optimized SVG sprites.

Code splitting is the key to deliver files without any content that is unused by the pages. It already exists for CSS, Javascript and now for SVG files with this plugin.

Expand Down Expand Up @@ -104,11 +104,11 @@ Finally, use the SVG with the `<use>` tag, like the following example. Replace `

## Using a configuration

You can pass configuration options to SvgChunkWebpackPlugin to overrides default settings. Allowed values are as follows:
You can pass configuration options to SvgChunkWebpackPlugin to overrides default settings. The transmitted parameters will be merged with the default parameters listed above. Allowed values are as follows:

### `filename`

`String`
`String = '[name].svg'`

Tells the plugin whether to personalize the default sprite filename. The placeholder `[name]` is automatically replaced by entrypoints names.

Expand All @@ -122,11 +122,11 @@ The `filename` parameter is compatible with Webpack caching placeholders, see th

### `svgoConfig`

`Object`
`Object = {}`

Tells the plugin whether to personalize the default settings for svgo. Update the settings according to your needs from the plugins available on the [svgo](https://github.com/svg/svgo) documentation.
Tells the plugin whether to personalize the plugins for svgo. Update the parameters according to your needs from the plugins list available on the [svgo](https://github.com/svg/svgo#what-it-can-do) documentation.

> 💡 The `onlyMatchedOnce` property allows to replace all occurences of CSS classes in HTML attributes, not only selectors that match once.
> 💡 The disabled `onlyMatchedOnce` property allows to replace all occurences of CSS classes in HTML attributes, not only selectors that match once. The disabled `removeViewBox` property allows to keep viewBow attributes.
```javascript
new SvgChunkWebpackPlugin({
Expand All @@ -136,6 +136,9 @@ new SvgChunkWebpackPlugin({
inlineStyles: {
onlyMatchedOnce: false
}
},
{
removeViewBox: false
}
]
}
Expand All @@ -144,18 +147,15 @@ new SvgChunkWebpackPlugin({

### `svgstoreConfig`

`Object`
`Object = { cleanDefs: false, cleanSymbols: false, inline: true }`

SVG sprites are built using the svgstore package. Tells the plugin whether to personalize the default settings for [svgstore](https://github.com/svgstore/svgstore#options).
SVG sprites are built using the svgstore package. Update the parameters according to your needs from the options list available on the [svgstore](https://github.com/svgstore/svgstore#options) documentation.

> 💡 Sprites already contain minimal inline styles to hide the sprite on the page to keep full support with all SVG features. To avoid LinearGradient conflicts, avoid the `display: none` property which break SVG defs.
> 💡 To avoid LinearGradient conflicts, avoid the `display: none` property which breaks SVG definitions.
```javascript
new SvgChunkWebpackPlugin({
svgstoreConfig: {
cleanDefs: false,
cleanSymbols: false,
inline: true,
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
Expand All @@ -172,7 +172,7 @@ Tells the plugin whether to generate the `sprites-manifest.json`. The JSON file

```javascript
new SvgChunkWebpackPlugin({
generateSpritesManifest: false
generateSpritesManifest: true
});
```

Expand All @@ -184,7 +184,7 @@ Tells the plugin whether to generate the `sprites-preview.html`. The HTML previe

```javascript
new SvgChunkWebpackPlugin({
generateSpritesManifest: false
generateSpritesManifest: true
});
```

Expand Down
20 changes: 19 additions & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,25 @@ module.exports = (env, argv) => {
new SvgChunkWebpackPlugin({
filename: `sprites/[name]${isProduction ? '.[contenthash]' : ''}.svg`,
generateSpritesManifest: true,
generateSpritesPreview: true
generateSpritesPreview: true,
svgstoreConfig: {
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
}
},
svgoConfig: {
plugins: [
{
inlineStyles: {
onlyMatchedOnce: false
}
},
{
removeViewBox: false
}
]
}
})
],
stats: {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license MIT
* @name SvgChunkWebpackPlugin
* @version 2.0.0
* @version 2.0.1
* @author: Yoriiis aka Joris DANIEL <joris.daniel@gmail.com>
* @copyright 2021 Joris DANIEL
**/
Expand Down
18 changes: 3 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @license MIT
* @name SvgChunkWebpackPlugin
* @version 2.0.0
* @version 2.0.1
* @author: Yoriiis aka Joris DANIEL <joris.daniel@gmail.com>
* @copyright 2021 Joris DANIEL
**/
Expand Down Expand Up @@ -48,21 +48,9 @@ class SvgSprite {
svgstoreConfig: {
cleanDefs: false,
cleanSymbols: false,
inline: true,
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
}
},
svgoConfig: {
plugins: [
{
inlineStyles: {
onlyMatchedOnce: false
}
}
]
inline: true
},
svgoConfig: {},
generateSpritesManifest: false,
generateSpritesPreview: false
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svg-chunk-webpack-plugin",
"version": "2.0.0",
"version": "2.0.1",
"description": "Generate SVG sprites according to entrypoint dependencies. Each page only imports its own svgs, wrapped as a sprite and optimized by svgo",
"keywords": [
"svg",
Expand Down
44 changes: 27 additions & 17 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,25 @@ const svgsSprite = [
];
const options = {
generateSpritesManifest: true,
generateSpritesPreview: true
generateSpritesPreview: true,
svgstoreConfig: {
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
}
},
svgoConfig: {
plugins: [
{
inlineStyles: {
onlyMatchedOnce: false
}
},
{
removeViewBox: false
}
]
}
};

const getInstance = () => new SvgChunkWebpackPlugin(options);
Expand Down Expand Up @@ -164,7 +182,7 @@ afterEach(() => {

describe('SvgChunkWebpackPlugin constructor', () => {
it('Should initialize the constructor with custom options', () => {
expect(svgChunkWebpackPlugin.options).toMatchObject({
expect(svgChunkWebpackPlugin.options).toStrictEqual({
filename: '[name].svg',
svgstoreConfig: {
cleanDefs: false,
Expand All @@ -181,6 +199,9 @@ describe('SvgChunkWebpackPlugin constructor', () => {
inlineStyles: {
onlyMatchedOnce: false
}
},
{
removeViewBox: false
}
]
},
Expand All @@ -195,25 +216,14 @@ describe('SvgChunkWebpackPlugin constructor', () => {

it('Should initialize the constructor with default options', () => {
const instance = new SvgChunkWebpackPlugin();
expect(instance.options).toMatchObject({
expect(instance.options).toStrictEqual({
filename: '[name].svg',
svgstoreConfig: {
cleanDefs: false,
cleanSymbols: false,
inline: true,
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
}
},
svgoConfig: {
plugins: [
{
inlineStyles: {
onlyMatchedOnce: false
}
}
]
inline: true
},
svgoConfig: {},
generateSpritesManifest: false,
generateSpritesPreview: false
});
Expand Down
18 changes: 3 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license MIT
* @name SvgChunkWebpackPlugin
* @version 2.0.0
* @version 2.0.1
* @author: Yoriiis aka Joris DANIEL <joris.daniel@gmail.com>
* @copyright 2021 Joris DANIEL
**/
Expand Down Expand Up @@ -52,21 +52,9 @@ class SvgSprite {
svgstoreConfig: {
cleanDefs: false,
cleanSymbols: false,
inline: true,
svgAttrs: {
'aria-hidden': true,
style: 'position: absolute; width: 0; height: 0; overflow: hidden;'
}
},
svgoConfig: {
plugins: [
{
inlineStyles: {
onlyMatchedOnce: false
}
}
]
inline: true
},
svgoConfig: {},
generateSpritesManifest: false,
generateSpritesPreview: false
};
Expand Down

0 comments on commit 1fd1a3c

Please sign in to comment.