Skip to content

Commit f71bb2b

Browse files
authored
Merge pull request #110 from vadymshymko/add-interpolate-to-sprite-filename
Allow use interpolate name patterns in spriteFilePath
2 parents 06bed18 + d01979c commit f71bb2b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ import iconData from 'path/to/some/icon-file-name.svg';
105105
| Name | Type | Default value | Description |
106106
| -------------- | -------------------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
107107
| 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. |
109109
| 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)) |
110110
| **addContent** | `boolean` | `false` | Add svg content as property to transformed svg object (may increase bundle size when enabled) |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svg-sprite-generation-loader",
3-
"version": "2.1.3",
3+
"version": "2.2.0",
44
"description": "Webpack loader for generating external svg symbol sprite files",
55
"main": "index.js",
66
"scripts": {

plugin.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const loaderUtils = require('loader-utils');
12
const svgSpriteState = require('./utils/spriteState');
23

34
class SvgSpriteGeneratorPlugin {
@@ -24,9 +25,23 @@ class SvgSpriteGeneratorPlugin {
2425
() => {
2526
if (compilation.options.loader.target === 'web') {
2627
Object.keys(svgSpriteState.sprites).forEach((spriteFilePath) => {
27-
compilation.emitAsset(
28+
const spriteContent = svgSpriteState.getSpriteContent(
29+
spriteFilePath,
30+
this.params
31+
);
32+
33+
const interpolatedPath = loaderUtils.interpolateName(
34+
{ resourcePath: spriteFilePath },
2835
spriteFilePath,
29-
new RawSource(svgSpriteState.getSpriteContent(spriteFilePath, this.params))
36+
{
37+
context: compilation.options.context,
38+
content: spriteContent,
39+
}
40+
);
41+
42+
compilation.emitAsset(
43+
interpolatedPath,
44+
new RawSource(spriteContent)
3045
);
3146
});
3247
}

0 commit comments

Comments
 (0)