Skip to content

Commit 1fa7964

Browse files
committed
Merge branch 'outputPath' of https://github.com/Slashgear/svg-sprite-loader into Slashgear-outputPath
2 parents 2982066 + 4451995 commit 1fa7964

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-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
@@ -60,6 +60,11 @@ class SVGSpritePlugin {
6060
apply(compiler) {
6161
this.rules = getMatchedRule(compiler);
6262

63+
const path = this.rules.outputPath ? this.rules.outputPath : this.rules.publicPath;
64+
this.filenamePrefix = path
65+
? path.replace(/^\//, '')
66+
: '';
67+
6368
if (compiler.hooks) {
6469
compiler.hooks
6570
.thisCompilation
@@ -159,11 +164,8 @@ class SVGSpritePlugin {
159164
})
160165
.then((sprite) => {
161166
const content = sprite.render();
162-
const filenamePrefix = this.rules.publicPath
163-
? this.rules.publicPath.replace(/^\//, '')
164-
: '';
165167

166-
compilation.assets[`${filenamePrefix}${filename}`] = {
168+
compilation.assets[`${this.filenamePrefix}${filename}`] = {
167169
source() { return content; },
168170
size() { return content.length; }
169171
};
@@ -200,11 +202,9 @@ class SVGSpritePlugin {
200202

201203
beforeHtmlGeneration(compilation) {
202204
const itemsBySprite = this.map.groupItemsBySpriteFilename();
203-
const filenamePrefix = this.rules.publicPath
204-
? this.rules.publicPath.replace(/^\//, '')
205-
: '';
205+
206206
const sprites = Object.keys(itemsBySprite).reduce((acc, filename) => {
207-
acc[filenamePrefix + filename] = compilation.assets[filenamePrefix + filename].source();
207+
acc[this.filenamePrefix + filename] = compilation.assets[this.filenamePrefix + filename].source();
208208
return acc;
209209
}, {});
210210

test/loader.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,28 @@ describe('loader and plugin', () => {
446446
assets['main.js'].source().should.contain(`__webpack_require__.p + "${spriteFilename}`);
447447
});
448448

449+
it('should generate asset with output path without changing publicPath', async () => {
450+
const publicPath = '/olala/';
451+
const spriteFilename = defaultSpriteFilename;
452+
453+
const v4Config = {};
454+
if (webpackVersion.IS_4) {
455+
v4Config.mode = 'development';
456+
v4Config.devtool = false;
457+
}
458+
459+
const { assets } = await compile(Object.assign(v4Config, {
460+
entry: './entry',
461+
output: { publicPath },
462+
module: rules(
463+
svgRule({ extract: true, spriteFilename, outputPath: '/foo/' })
464+
),
465+
plugins: [new SpritePlugin()]
466+
}));
467+
468+
assets['main.js'].source().should.contain(`__webpack_require__.p + "${spriteFilename}`);
469+
});
470+
449471
it('should emit only built chunks', () => {
450472
// TODO test with webpack-recompilation-emulator
451473
});

0 commit comments

Comments
 (0)