You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

4 years ago
  1. /* eslint-env mocha */
  2. 'use strict'
  3. var assert = require('assert')
  4. var fs = require('fs')
  5. var path = require('path')
  6. var gifsicle = require('imagemin-gifsicle')
  7. var svgo = require('imagemin-svgo')
  8. var run = require('./run-webpack')
  9. var fixtureGif = fs.readFileSync(path.resolve(__dirname, './fixture.gif'))
  10. var fixtureSvg = fs.readFileSync(path.resolve(__dirname, './fixture.svg'))
  11. describe('img-loader', () => {
  12. it('passes the img though unchanged by default', function () {
  13. return run('./fixture.gif').then(function (image) {
  14. assert(image.equals(fixtureGif), 'gif should be unchanged')
  15. })
  16. })
  17. it('can apply optimizations for gif', function () {
  18. return run('./fixture.gif', {
  19. plugins: [ gifsicle({}) ]
  20. }).then(function (image) {
  21. assert(!image.equals(fixtureGif), 'gif should be changed')
  22. assert(image.length < fixtureGif.length, 'optimized gif should be smaller')
  23. })
  24. })
  25. it('can apply optimizations for svg', function () {
  26. return run('./fixture.svg', {
  27. plugins: [ svgo({}) ]
  28. }).then(function (image) {
  29. assert(!image.equals(fixtureSvg), 'svg should be changed')
  30. assert(image.length < fixtureSvg.length, 'optimized svg should be smaller')
  31. assert.strictEqual(image.toString('utf8'), '<svg/>')
  32. })
  33. })
  34. it('can use a function for plugins', function () {
  35. var context
  36. return run('./fixture.svg', {
  37. plugins: function (ctx) {
  38. context = ctx
  39. return [ svgo({}) ]
  40. }
  41. }).then(function (image) {
  42. assert.strictEqual(path.basename(context.resourcePath), 'fixture.svg')
  43. assert(image.length < fixtureSvg.length, 'optimized svg should be smaller')
  44. })
  45. })
  46. })