svg-sprite-generation-loader 2.1.3 → 2.2.1

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.
package/README.md CHANGED
@@ -105,6 +105,6 @@ import iconData from 'path/to/some/icon-file-name.svg';
105
105
  | Name | Type | Default value | Description |
106
106
  | -------------- | -------------------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
107
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 |
108
+ | spriteFilePath | `string` | `sprite.svg` | Path to sprite file.<br /> `webpack.output.path` is included. You can use [interpolateName](https://github.com/webpack/loader-utils#interpolatename) patterns. |
109
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
110
  | **addContent** | `boolean` | `false` | Add svg content as property to transformed svg object (may increase bundle size when enabled) |
package/index.js CHANGED
@@ -1,9 +1,79 @@
1
1
  const loaderUtils = require('loader-utils');
2
2
  const { optimize } = require('svgo');
3
+ const { validate } = require('schema-utils');
3
4
 
4
5
  const svgSpriteState = require('./utils/spriteState');
5
6
  const transformSvg = require('./utils/transformSvg');
6
7
 
8
+ const optionsSchema = {
9
+ $schema: 'http://json-schema.org/draft-07/schema#',
10
+ title: 'SvgSpriteGeneratorPlugin Options',
11
+ type: 'object',
12
+ properties: {
13
+ symbolId: {
14
+ description:
15
+ 'Sprite item (single icon) `<symbol>` id attribute value. Can be a string or a function that takes the file path of the original icon and returns a string.',
16
+ oneOf: [
17
+ {
18
+ type: 'string',
19
+ description:
20
+ "String value for symbol id, e.g. '[name]' which defaults to the icon filename without extension.",
21
+ },
22
+ {
23
+ instanceof: 'Function',
24
+ description:
25
+ 'Function that takes the file path of the original icon and returns a string.',
26
+ },
27
+ ],
28
+ },
29
+ spriteFilePath: {
30
+ type: 'string',
31
+ description:
32
+ 'Path to sprite file. This path is relative to `webpack.output.path` and can include [interpolateName](https://github.com/webpack/loader-utils#interpolatename) patterns.',
33
+ },
34
+ svgoOptimize: {
35
+ description: 'Enable or disable SVG optimization',
36
+ oneOf: [
37
+ {
38
+ type: 'boolean',
39
+ description:
40
+ 'Enable or disable SVG optimization with default settings.',
41
+ },
42
+ {
43
+ type: 'object',
44
+ description: 'Customize SVG optimization using svgo options.',
45
+ properties: {
46
+ plugins: {
47
+ type: 'array',
48
+ items: {
49
+ type: 'object',
50
+ properties: {
51
+ name: {
52
+ type: 'string',
53
+ description: 'Name of the svgo plugin',
54
+ },
55
+ params: {
56
+ type: 'object',
57
+ description: 'Parameters for the svgo plugin',
58
+ },
59
+ },
60
+ required: ['name'],
61
+ },
62
+ },
63
+ },
64
+ },
65
+ ],
66
+ },
67
+ addContent: {
68
+ type: 'boolean',
69
+ description:
70
+ 'Add SVG content as property to transformed SVG object, which may increase bundle size.',
71
+ },
72
+ },
73
+ required: [],
74
+ additionalProperties: false,
75
+ };
76
+
7
77
  function svgSpriteGenerationLoader(source) {
8
78
  const options = {
9
79
  symbolId: '[name]',
@@ -13,6 +83,13 @@ function svgSpriteGenerationLoader(source) {
13
83
  ...this.getOptions(),
14
84
  };
15
85
 
86
+ validate(optionsSchema, options, {
87
+ name: 'svgSpriteGenerationLoader',
88
+ baseDataPath: 'options',
89
+ });
90
+
91
+ this.cacheable(false);
92
+
16
93
  const isSvgoOptimizeEnabled = !!options.svgoOptimize;
17
94
  const svgoOptimizeConfig =
18
95
  options.svgoOptimize === true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svg-sprite-generation-loader",
3
- "version": "2.1.3",
3
+ "version": "2.2.1",
4
4
  "description": "Webpack loader for generating external svg symbol sprite files",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -36,6 +36,7 @@
36
36
  "homepage": "https://github.com/vadymshymko/svg-sprite-generation-loader#readme",
37
37
  "dependencies": {
38
38
  "loader-utils": "^3.2.1",
39
+ "schema-utils": "^4.2.0",
39
40
  "svgo": "^3.0.2"
40
41
  },
41
42
  "devDependencies": {
package/plugin.js CHANGED
@@ -1,3 +1,4 @@
1
+ const loaderUtils = require('loader-utils');
1
2
  const svgSpriteState = require('./utils/spriteState');
2
3
 
3
4
  class SvgSpriteGeneratorPlugin {
@@ -20,15 +21,46 @@ class SvgSpriteGeneratorPlugin {
20
21
  SvgSpriteGeneratorPlugin.name,
21
22
  (compilation) => {
22
23
  compilation.hooks.processAssets.tap(
23
- SvgSpriteGeneratorPlugin.name,
24
- () => {
24
+ {
25
+ name: SvgSpriteGeneratorPlugin.name,
26
+ stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
27
+ },
28
+ (assets, callback) => {
25
29
  if (compilation.options.loader.target === 'web') {
26
- Object.keys(svgSpriteState.sprites).forEach((spriteFilePath) => {
27
- compilation.emitAsset(
28
- spriteFilePath,
29
- new RawSource(svgSpriteState.getSpriteContent(spriteFilePath, this.params))
30
+ try {
31
+ Object.keys(svgSpriteState.sprites).forEach(
32
+ (spriteFilePath) => {
33
+ const spriteContent = svgSpriteState.getSpriteContent(
34
+ spriteFilePath,
35
+ this.params
36
+ );
37
+
38
+ const interpolatedPath = loaderUtils.interpolateName(
39
+ { resourcePath: spriteFilePath },
40
+ spriteFilePath,
41
+ {
42
+ context: compilation.options.context,
43
+ content: spriteContent,
44
+ }
45
+ );
46
+
47
+ if (compilation.getAsset(interpolatedPath)) {
48
+ compilation.updateAsset(
49
+ interpolatedPath,
50
+ new RawSource(spriteContent)
51
+ );
52
+ } else {
53
+ compilation.emitAsset(
54
+ interpolatedPath,
55
+ new RawSource(spriteContent)
56
+ );
57
+ }
58
+ }
30
59
  );
31
- });
60
+ } catch (error) {
61
+ console.error(error);
62
+ callback(error);
63
+ }
32
64
  }
33
65
  }
34
66
  );