svg-sprite-generation-loader 2.2.0 → 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.
Files changed (3) hide show
  1. package/index.js +77 -0
  2. package/package.json +2 -1
  3. package/plugin.js +36 -19
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.2.0",
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
@@ -21,29 +21,46 @@ class SvgSpriteGeneratorPlugin {
21
21
  SvgSpriteGeneratorPlugin.name,
22
22
  (compilation) => {
23
23
  compilation.hooks.processAssets.tap(
24
- SvgSpriteGeneratorPlugin.name,
25
- () => {
24
+ {
25
+ name: SvgSpriteGeneratorPlugin.name,
26
+ stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
27
+ },
28
+ (assets, callback) => {
26
29
  if (compilation.options.loader.target === 'web') {
27
- Object.keys(svgSpriteState.sprites).forEach((spriteFilePath) => {
28
- const spriteContent = svgSpriteState.getSpriteContent(
29
- spriteFilePath,
30
- this.params
31
- );
30
+ try {
31
+ Object.keys(svgSpriteState.sprites).forEach(
32
+ (spriteFilePath) => {
33
+ const spriteContent = svgSpriteState.getSpriteContent(
34
+ spriteFilePath,
35
+ this.params
36
+ );
32
37
 
33
- const interpolatedPath = loaderUtils.interpolateName(
34
- { resourcePath: spriteFilePath },
35
- spriteFilePath,
36
- {
37
- context: compilation.options.context,
38
- content: spriteContent,
39
- }
40
- );
38
+ const interpolatedPath = loaderUtils.interpolateName(
39
+ { resourcePath: spriteFilePath },
40
+ spriteFilePath,
41
+ {
42
+ context: compilation.options.context,
43
+ content: spriteContent,
44
+ }
45
+ );
41
46
 
42
- compilation.emitAsset(
43
- interpolatedPath,
44
- new RawSource(spriteContent)
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
+ }
45
59
  );
46
- });
60
+ } catch (error) {
61
+ console.error(error);
62
+ callback(error);
63
+ }
47
64
  }
48
65
  }
49
66
  );