|  | 
| 1 | 1 | /* | 
| 2 |  | -  MIT License http://www.opensource.org/licenses/mit-license.php | 
| 3 |  | -  Author Tobias Koppers @sokra | 
|  | 2 | +MIT License http://www.opensource.org/licenses/mit-license.php | 
|  | 3 | +Author Tobias Koppers @sokra | 
| 4 | 4 | */ | 
| 5 | 5 | import url from 'url'; | 
| 6 | 6 | import async from 'async'; | 
| @@ -38,54 +38,53 @@ class CompressionPlugin { | 
| 38 | 38 |   } | 
| 39 | 39 | 
 | 
| 40 | 40 |   apply(compiler) { | 
| 41 |  | -    compiler.plugin('this-compilation', (compilation) => { | 
| 42 |  | -      compilation.plugin('optimize-assets', (assets, callback) => { | 
| 43 |  | -        async.forEach(Object.keys(assets), (file, cb) => { | 
| 44 |  | -          if (Array.isArray(this.test)) { | 
| 45 |  | -            if (this.test.every(t => !t.test(file))) { | 
| 46 |  | -              return cb(); | 
| 47 |  | -            } | 
| 48 |  | -          } else if (this.test && !this.test.test(file)) { | 
|  | 41 | +    compiler.plugin('emit', (compilation, callback) => { | 
|  | 42 | +      const assets = compilation.assets; | 
|  | 43 | +      async.forEach(Object.keys(assets), (file, cb) => { | 
|  | 44 | +        if (Array.isArray(this.test)) { | 
|  | 45 | +          if (this.test.every(t => !t.test(file))) { | 
| 49 | 46 |             return cb(); | 
| 50 | 47 |           } | 
| 51 |  | -          const asset = assets[file]; | 
| 52 |  | -          let content = asset.source(); | 
|  | 48 | +        } else if (this.test && !this.test.test(file)) { | 
|  | 49 | +          return cb(); | 
|  | 50 | +        } | 
|  | 51 | +        const asset = assets[file]; | 
|  | 52 | +        let content = asset.source(); | 
| 53 | 53 | 
 | 
| 54 |  | -          if (!Buffer.isBuffer(content)) { | 
| 55 |  | -            content = new Buffer(content, 'utf-8'); | 
| 56 |  | -          } | 
|  | 54 | +        if (!Buffer.isBuffer(content)) { | 
|  | 55 | +          content = new Buffer(content, 'utf-8'); | 
|  | 56 | +        } | 
| 57 | 57 | 
 | 
| 58 |  | -          const originalSize = content.length; | 
|  | 58 | +        const originalSize = content.length; | 
| 59 | 59 | 
 | 
| 60 |  | -          if (originalSize < this.threshold) { | 
| 61 |  | -            return cb(); | 
| 62 |  | -          } | 
|  | 60 | +        if (originalSize < this.threshold) { | 
|  | 61 | +          return cb(); | 
|  | 62 | +        } | 
| 63 | 63 | 
 | 
| 64 |  | -          this.algorithm(content, this.compressionOptions, (err, result) => { | 
| 65 |  | -            if (err) { return cb(err); } | 
|  | 64 | +        this.algorithm(content, this.compressionOptions, (err, result) => { | 
|  | 65 | +          if (err) { return cb(err); } | 
| 66 | 66 | 
 | 
| 67 |  | -            if (result.length / originalSize > this.minRatio) { return cb(); } | 
|  | 67 | +          if (result.length / originalSize > this.minRatio) { return cb(); } | 
| 68 | 68 | 
 | 
| 69 |  | -            const parse = url.parse(file); | 
| 70 |  | -            const sub = { | 
| 71 |  | -              file, | 
| 72 |  | -              path: parse.pathname, | 
| 73 |  | -              query: parse.query || '', | 
| 74 |  | -            }; | 
|  | 69 | +          const parse = url.parse(file); | 
|  | 70 | +          const sub = { | 
|  | 71 | +            file, | 
|  | 72 | +            path: parse.pathname, | 
|  | 73 | +            query: parse.query || '', | 
|  | 74 | +          }; | 
| 75 | 75 | 
 | 
| 76 |  | -            let newFile = this.asset.replace(/\[(file|path|query)\]/g, (p0, p1) => sub[p1]); | 
|  | 76 | +          let newFile = this.asset.replace(/\[(file|path|query)\]/g, (p0, p1) => sub[p1]); | 
| 77 | 77 | 
 | 
| 78 |  | -            if (typeof this.filename === 'function') { | 
| 79 |  | -              newFile = this.filename(newFile); | 
| 80 |  | -            } | 
| 81 |  | -            assets[newFile] = new RawSource(result); | 
| 82 |  | -            if (this.deleteOriginalAssets) { | 
| 83 |  | -              delete assets[file]; | 
| 84 |  | -            } | 
| 85 |  | -            cb(); | 
| 86 |  | -          }); | 
| 87 |  | -        }, callback); | 
| 88 |  | -      }); | 
|  | 78 | +          if (typeof this.filename === 'function') { | 
|  | 79 | +            newFile = this.filename(newFile); | 
|  | 80 | +          } | 
|  | 81 | +          assets[newFile] = new RawSource(result); | 
|  | 82 | +          if (this.deleteOriginalAssets) { | 
|  | 83 | +            delete assets[file]; | 
|  | 84 | +          } | 
|  | 85 | +          cb(); | 
|  | 86 | +        }); | 
|  | 87 | +      }, callback); | 
| 89 | 88 |     }); | 
| 90 | 89 |   } | 
| 91 | 90 | } | 
|  | 
0 commit comments