svg-sprite-generation-loader 1.1.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ vadymshymko@gmail.com.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.0, available at
119
+ https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder](https://github.com/mozilla/diversity).
123
+
124
+ [homepage]: https://www.contributor-covenant.org
125
+
126
+ For answers to common questions about this code of conduct, see the FAQ at
127
+ https://www.contributor-covenant.org/faq. Translations are available at
128
+ https://www.contributor-covenant.org/translations.
package/README.md CHANGED
@@ -5,9 +5,16 @@
5
5
 
6
6
  Webpack loader for generating external svg symbol sprite files
7
7
 
8
+ ## Table of contents
9
+
10
+ - [How it works?](#how-it-works)
11
+ - [Installation](#installation)
12
+ - [Usage](#usage)
13
+ - [Options](#options)
14
+
8
15
  ## How it works?
9
16
 
10
- svg-sprite-generation-loader is a webpack-loader that takes a multiple svg files, transform them (parse and return as an object with `symbolId`, `attributes` and `content` (disabled by default) keys), optimizes and put them back in one file.
17
+ svg-sprite-generation-loader is a webpack-loader that takes a multiple svg files, optimize them (using [svgo.optimize](https://github.com/svg/svgo#optimize)), transform (parse and return as an object with `symbolId`, `attributes` and `content` (disabled by default) keys) and put them back in one file.
11
18
 
12
19
  Input multiple svg files, e.g:
13
20
 
@@ -60,23 +67,6 @@ npm install svg-sprite-generation-loader --save-dev
60
67
  yarn add svg-sprite-generation-loader --dev
61
68
  ```
62
69
 
63
- ## Options
64
-
65
- Plugin options:
66
-
67
- | Name | Type | Default value | Description |
68
- | -------------- | -------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------- |
69
- | spriteFilePath | `string` | `sprite.svg` | Path to sprite file.<br /> `webpack.output.path` <br /> is included |
70
- | svgo | object | default preset with disabled `removeUselessDefs`, `removeViewBox` and `cleanupIDs` plugins | [svgo](https://github.com/svg/svgo) config object |
71
-
72
- Loader options:
73
-
74
- | Name | Type | Default value | Description |
75
- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
76
- | symbolId | `string`&nbsp;\|&nbsp;`function(iconPath)` - function that takes the file path of the original icon as an argument and returns the symbolId value<br /> In both cases you can use [interpolateName](https://github.com/webpack/loader-utils#interpolatename) patterns | `"[name]"` which is equal to icon filename (without extension). For example, by default symbolId for `file1.svg` file will be `file1` | `<symbol>`&nbsp;`id` attribute value. |
77
- | addContent | `boolean` | `false` | Add svg content as property to transformed svg object (may increase bundle size when enabled) |
78
- | attributes | `string[]` | | What attributes of the original svg need to be added to transformed svg object (By default - all) |
79
-
80
70
  ## Usage:
81
71
 
82
72
  In your webpack config:
@@ -85,11 +75,7 @@ In your webpack config:
85
75
  const SvgSpriteGenerationPlugin = require('svg-sprite-generation-loader/plugin.js');
86
76
 
87
77
  module.exports = {
88
- plugins: [
89
- new SvgSpriteGenerationPlugin.Plugin({
90
- spriteFilePath: 'path/to/sprite/filename.svg',
91
- }),
92
- ],
78
+ plugins: [new SvgSpriteGenerationPlugin()],
93
79
  module: {
94
80
  rules: [
95
81
  {
@@ -105,9 +91,20 @@ In some source code:
105
91
 
106
92
  ```jsx
107
93
  import iconData from 'path/to/some/icon-file-name.svg';
108
- // by default iconData will include symbolId and attributes keys. If you enable the addContent loader option, the `content` key will also be added
94
+
95
+ // by default iconData will include symbolId and attributes keys.
96
+ // If you enable the addContent loader option, the `content` key will also be added
109
97
 
110
98
  <svg {...iconData.attributes}>
111
99
  <use href={`path/to/sprite/filename.svg#${iconData.symbolId}`} />
112
100
  </svg>;
113
101
  ```
102
+
103
+ ## Options
104
+
105
+ | Name | Type | Default value | Description |
106
+ | -------------- | -------------------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
107
+ | symbolId | `string`&nbsp;\|&nbsp;`function` | `"[name]"` | Sprite item (single icon) `<symbol>`&nbsp;`id` attribute value.<br />`string` or `function` that takes the file path of the original icon as an argument and returns `string`.<br /><br />You can use [interpolateName](https://github.com/webpack/loader-utils#interpolatename) patterns.<br /> Default value (`"[name]"`) is equal to icon filename (without extension). For example, by default symbolId for `file1.svg` file will be `file1` |
108
+ | spriteFilePath | `string` | `sprite.svg` | Path to sprite file.<br /> `webpack.output.path` is included |
109
+ | svgoOptimize | `boolean` \| `object` | `true` | Enable/Disable/Customize source svg file optimization with [svgo.optimize](https://github.com/svg/svgo#optimize). The following options are used by default: <br /><pre>`{`<br />&nbsp;&nbsp;`plugins: [`<br />&nbsp;&nbsp;&nbsp;&nbsp;`{`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`name: 'preset-default',`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`params: {`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`overrides: {`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`removeViewBox: false,`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`},`<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`},`<br />&nbsp;&nbsp;&nbsp;&nbsp;`},`<br />&nbsp;&nbsp;&nbsp;&nbsp;`'removeXMLNS',`<br />&nbsp;&nbsp;`],`<br />`}`</pre> You can disable it completely (by passing `false`) or use your own configuration (see [svgo docs](https://github.com/svg/svgo#optimize)) |
110
+ | **addContent** | `boolean` | `false` | Add svg content as property to transformed svg object (may increase bundle size when enabled) |
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const loaderUtils = require('loader-utils');
2
+ const { optimize } = require('svgo');
2
3
 
3
4
  const svgSpriteState = require('./utils/spriteState');
4
5
  const transformSvg = require('./utils/transformSvg');
@@ -6,10 +7,35 @@ const transformSvg = require('./utils/transformSvg');
6
7
  function svgSpriteGenerationLoader(source) {
7
8
  const options = {
8
9
  symbolId: '[name]',
10
+ spriteFilePath: 'sprite.svg',
11
+ svgoOptimize: true,
12
+ addContent: false,
9
13
  ...this.getOptions(),
10
14
  };
11
15
 
12
- const { attributes: parsedAttributes, content } = transformSvg(source);
16
+ const isSvgoOptimizeEnabled = !!options.svgoOptimize;
17
+ const svgoOptimizeConfig =
18
+ options.svgoOptimize === true
19
+ ? {
20
+ plugins: [
21
+ {
22
+ name: 'preset-default',
23
+ params: {
24
+ overrides: {
25
+ removeViewBox: false,
26
+ },
27
+ },
28
+ },
29
+ 'removeXMLNS',
30
+ ],
31
+ }
32
+ : options.svgoOptimize;
33
+
34
+ const iconSource = isSvgoOptimizeEnabled
35
+ ? optimize(source, svgoOptimizeConfig).data
36
+ : source;
37
+
38
+ const { attributes, content } = transformSvg(iconSource);
13
39
 
14
40
  const symbolId = loaderUtils.interpolateName(
15
41
  this,
@@ -18,16 +44,11 @@ function svgSpriteGenerationLoader(source) {
18
44
  : options.symbolId(this.resourcePath)
19
45
  );
20
46
 
21
- const attributes = options.attributes
22
- ? options.attributes.reduce((result, attrName) => {
23
- // eslint-disable-next-line no-param-reassign
24
- result[attrName] = parsedAttributes[attrName];
25
- return result;
26
- }, {})
27
- : parsedAttributes;
28
-
29
47
  if (this.target === 'web') {
30
- svgSpriteState.addSpriteIcon({ symbolId, attributes, content });
48
+ svgSpriteState.addSpriteIcon(
49
+ { symbolId, attributes, content },
50
+ options.spriteFilePath
51
+ );
31
52
  }
32
53
 
33
54
  return `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svg-sprite-generation-loader",
3
- "version": "1.1.0",
3
+ "version": "2.0.2",
4
4
  "description": "Webpack loader for generating external svg symbol sprite files",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin.js CHANGED
@@ -1,30 +1,7 @@
1
- const { optimize } = require('svgo');
2
-
3
1
  const svgSpriteState = require('./utils/spriteState');
4
2
 
5
3
  class SvgSpriteGeneratorPlugin {
6
- static defaultOptions = {
7
- spriteFilePath: 'sprite.svg',
8
- svgo: {
9
- plugins: [
10
- {
11
- name: 'preset-default',
12
- params: {
13
- overrides: {
14
- removeUselessDefs: false,
15
- removeViewBox: false,
16
- cleanupIDs: false,
17
- },
18
- },
19
- },
20
- ],
21
- },
22
- };
23
-
24
- constructor(options = {}) {
25
- this.options = { ...SvgSpriteGeneratorPlugin.defaultOptions, ...options };
26
- }
27
-
4
+ // eslint-disable-next-line class-methods-use-this
28
5
  apply(compiler) {
29
6
  // webpack module instance can be accessed from the compiler object,
30
7
  // this ensures that correct version of the module is used
@@ -42,15 +19,12 @@ class SvgSpriteGeneratorPlugin {
42
19
  SvgSpriteGeneratorPlugin.name,
43
20
  () => {
44
21
  if (compilation.options.loader.target === 'web') {
45
- compilation.emitAsset(
46
- `${this.options.spriteFilePath}`,
47
- new RawSource(
48
- optimize(
49
- svgSpriteState.getSpriteContent(),
50
- this.options.svgo
51
- ).data
52
- )
53
- );
22
+ Object.keys(svgSpriteState.sprites).forEach((spriteFilePath) => {
23
+ compilation.emitAsset(
24
+ spriteFilePath,
25
+ new RawSource(svgSpriteState.getSpriteContent(spriteFilePath))
26
+ );
27
+ });
54
28
  }
55
29
  }
56
30
  );
@@ -1,16 +1,17 @@
1
1
  class SvgSpriteGenerationState {
2
2
  constructor() {
3
- this.spriteIcons = {};
3
+ this.sprites = {};
4
4
  }
5
5
 
6
- addSpriteIcon(icon) {
7
- this.spriteIcons[icon.symbolId] = icon;
6
+ addSpriteIcon(icon, spriteFilePath) {
7
+ this.sprites[spriteFilePath] = this.sprites[spriteFilePath] || {};
8
+ this.sprites[spriteFilePath][icon.symbolId] = icon;
8
9
  }
9
10
 
10
- getSpriteContent() {
11
- return `${Object.keys(this.spriteIcons).reduce(
11
+ getSpriteContent(spriteFilePath) {
12
+ return `${Object.keys(this.sprites[spriteFilePath]).reduce(
12
13
  (result, iconId) =>
13
- `${result}<symbol viewBox="${this.spriteIcons[iconId].attributes.viewBox}" fill="none" id="${iconId}">${this.spriteIcons[iconId].content}</symbol>`,
14
+ `${result}<symbol viewBox="${this.sprites[spriteFilePath][iconId].attributes.viewBox}" fill="none" id="${iconId}">${this.sprites[spriteFilePath][iconId].content}</symbol>`,
14
15
  '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">'
15
16
  )}</svg>`;
16
17
  }
@@ -1,7 +1,5 @@
1
1
  function transformSvg(source) {
2
- const [, parsedAttributes, parsedContent] = source.match(
3
- /<svg(.{0,})>([\s\S]+)<\/svg>/i
4
- );
2
+ const [, parsedAttributes, content] = source.match(/<svg(.*?)>(.*?)<\/svg>/i);
5
3
 
6
4
  const attributes = parsedAttributes
7
5
  .match(/([\w-:]+)(=)?("[^<>"]*"|'[^<>']*'|[\w-:]+)/g)
@@ -18,7 +16,7 @@ function transformSvg(source) {
18
16
 
19
17
  return {
20
18
  attributes,
21
- content: parsedContent.replace(/\n/g, ' ').trim(),
19
+ content,
22
20
  };
23
21
  }
24
22