Skip to content

Commit

Permalink
feat(outputPath): add possibility to define output path
Browse files Browse the repository at this point in the history
  • Loading branch information
Slashgear committed Apr 24, 2020
1 parent 0fbf2bd commit 2c7eceb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Webpack loader for creating SVG sprites.
- [`extract`](#extract)
- [`spriteFilename`](#sprite-filename)
- [`publicPath`](#public-path)
- [`outputPath`](#output-path)
- [`plainSprite`](#plain-sprite)
- [`spriteAttrs`](#sprite-attrs)
- [Examples](#examples)
Expand Down Expand Up @@ -241,6 +242,25 @@ Custom public path for sprite file.
}
```

<a id="output-path"></a>
### `outputPath` (type: `string`, default: null`)

Custom output path for sprite file.
By default it will use `publicPath`.
This param is useful if you want to store sprite is a directory with a custom http access.

```js
{
test: /\.svg$/,
loader: 'svg-sprite-loader',
options: {
extract: true,
outputPath: 'custom-dir/sprites/'
publicPath: 'sprites/'
}
}
```

<a id="plain-sprite"></a>
### Plain sprite

Expand Down
16 changes: 8 additions & 8 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class SVGSpritePlugin {
apply(compiler) {
this.rules = getMatchedRule(compiler);

const path = this.rules.outputPath ? this.rules.outputPath : this.rules.publicPath;
this.filenamePrefix = path
? path.replace(/^\//, '')
: '';

if (compiler.hooks) {
compiler.hooks
.thisCompilation
Expand Down Expand Up @@ -162,11 +167,8 @@ class SVGSpritePlugin {
})
.then((sprite) => {
const content = sprite.render();
const filenamePrefix = this.rules.publicPath
? this.rules.publicPath.replace(/^\//, '')
: '';

compilation.assets[`${filenamePrefix}${filename}`] = {
compilation.assets[`${this.filenamePrefix}${filename}`] = {
source() { return content; },
size() { return content.length; }
};
Expand Down Expand Up @@ -209,11 +211,9 @@ class SVGSpritePlugin {

beforeHtmlGeneration(compilation) {
const itemsBySprite = this.map.groupItemsBySpriteFilename();
const filenamePrefix = this.rules.publicPath
? this.rules.publicPath.replace(/^\//, '')
: '';

const sprites = Object.keys(itemsBySprite).reduce((acc, filename) => {
acc[filenamePrefix + filename] = compilation.assets[filenamePrefix + filename].source();
acc[this.filenamePrefix + filename] = compilation.assets[this.filenamePrefix + filename].source();
return acc;
}, {});

Expand Down

0 comments on commit 2c7eceb

Please sign in to comment.