From 2c7eceb386cb93c23d68217e81cf2d04bec40e08 Mon Sep 17 00:00:00 2001 From: Antoine CARON Date: Fri, 24 Apr 2020 11:18:08 +0200 Subject: [PATCH 1/2] feat(outputPath): add possibility to define output path --- README.md | 20 ++++++++++++++++++++ lib/plugin.js | 16 ++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 41a9eb1..2a50198 100644 --- a/README.md +++ b/README.md @@ -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) @@ -241,6 +242,25 @@ Custom public path for sprite file. } ``` + +### `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/' + } +} +``` + ### Plain sprite diff --git a/lib/plugin.js b/lib/plugin.js index a79898a..f820150 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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 @@ -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; } }; @@ -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; }, {}); From 4451995252f1ae5f32788e0421e9a8f423f25c2c Mon Sep 17 00:00:00 2001 From: Slashgear Date: Wed, 29 Apr 2020 23:35:57 +0200 Subject: [PATCH 2/2] add test --- test/loader.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/loader.test.js b/test/loader.test.js index 444115c..ecb38e6 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -446,6 +446,28 @@ describe('loader and plugin', () => { assets['main.js'].source().should.contain(`__webpack_require__.p + "${spriteFilename}`); }); + it('should generate asset with output path without changing publicPath', async () => { + const publicPath = '/olala/'; + const spriteFilename = defaultSpriteFilename; + + const v4Config = {}; + if (webpackVersion.IS_4) { + v4Config.mode = 'development'; + v4Config.devtool = false; + } + + const { assets } = await compile(Object.assign(v4Config, { + entry: './entry', + output: { publicPath }, + module: rules( + svgRule({ extract: true, spriteFilename, outputPath: '/foo/' }) + ), + plugins: [new SpritePlugin()] + })); + + assets['main.js'].source().should.contain(`__webpack_require__.p + "${spriteFilename}`); + }); + it('should emit only built chunks', () => { // TODO test with webpack-recompilation-emulator });