Skip to content

Commit 2c7eceb

Browse files
committed
feat(outputPath): add possibility to define output path
1 parent 0fbf2bd commit 2c7eceb

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Webpack loader for creating SVG sprites.
2626
- [`extract`](#extract)
2727
- [`spriteFilename`](#sprite-filename)
2828
- [`publicPath`](#public-path)
29+
- [`outputPath`](#output-path)
2930
- [`plainSprite`](#plain-sprite)
3031
- [`spriteAttrs`](#sprite-attrs)
3132
- [Examples](#examples)
@@ -241,6 +242,25 @@ Custom public path for sprite file.
241242
}
242243
```
243244

245+
<a id="output-path"></a>
246+
### `outputPath` (type: `string`, default: null`)
247+
248+
Custom output path for sprite file.
249+
By default it will use `publicPath`.
250+
This param is useful if you want to store sprite is a directory with a custom http access.
251+
252+
```js
253+
{
254+
test: /\.svg$/,
255+
loader: 'svg-sprite-loader',
256+
options: {
257+
extract: true,
258+
outputPath: 'custom-dir/sprites/'
259+
publicPath: 'sprites/'
260+
}
261+
}
262+
```
263+
244264
<a id="plain-sprite"></a>
245265
### Plain sprite
246266

lib/plugin.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class SVGSpritePlugin {
6363
apply(compiler) {
6464
this.rules = getMatchedRule(compiler);
6565

66+
const path = this.rules.outputPath ? this.rules.outputPath : this.rules.publicPath;
67+
this.filenamePrefix = path
68+
? path.replace(/^\//, '')
69+
: '';
70+
6671
if (compiler.hooks) {
6772
compiler.hooks
6873
.thisCompilation
@@ -162,11 +167,8 @@ class SVGSpritePlugin {
162167
})
163168
.then((sprite) => {
164169
const content = sprite.render();
165-
const filenamePrefix = this.rules.publicPath
166-
? this.rules.publicPath.replace(/^\//, '')
167-
: '';
168170

169-
compilation.assets[`${filenamePrefix}${filename}`] = {
171+
compilation.assets[`${this.filenamePrefix}${filename}`] = {
170172
source() { return content; },
171173
size() { return content.length; }
172174
};
@@ -209,11 +211,9 @@ class SVGSpritePlugin {
209211

210212
beforeHtmlGeneration(compilation) {
211213
const itemsBySprite = this.map.groupItemsBySpriteFilename();
212-
const filenamePrefix = this.rules.publicPath
213-
? this.rules.publicPath.replace(/^\//, '')
214-
: '';
214+
215215
const sprites = Object.keys(itemsBySprite).reduce((acc, filename) => {
216-
acc[filenamePrefix + filename] = compilation.assets[filenamePrefix + filename].source();
216+
acc[this.filenamePrefix + filename] = compilation.assets[this.filenamePrefix + filename].source();
217217
return acc;
218218
}, {});
219219

0 commit comments

Comments
 (0)