|
1 | | -import { dirname } from 'node:path'; |
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { dirname, join } from 'node:path'; |
2 | 3 | import { fileURLToPath } from 'node:url'; |
3 | 4 | import { expect, test } from '@playwright/test'; |
4 | 5 | import { createRsbuild } from '@rsbuild/core'; |
5 | 6 | import { pluginImageCompress } from '../../src'; |
6 | | -import { getRandomPort } from '../helper'; |
7 | 7 |
|
8 | 8 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
9 | 9 |
|
10 | | -test('should render page as expected', async ({ page }) => { |
| 10 | +test('should compress image with use plugin-image-compress', async () => { |
11 | 11 | const rsbuild = await createRsbuild({ |
12 | 12 | cwd: __dirname, |
13 | 13 | rsbuildConfig: { |
14 | 14 | plugins: [pluginImageCompress()], |
15 | | - server: { |
16 | | - port: getRandomPort(), |
17 | | - }, |
18 | 15 | }, |
19 | 16 | }); |
20 | 17 |
|
21 | | - const { server, urls } = await rsbuild.startDevServer(); |
22 | | - |
23 | | - await page.goto(urls[0]); |
24 | | - expect(await page.evaluate('window.test')).toBe(1); |
25 | | - |
26 | | - await server.close(); |
27 | | -}); |
28 | | - |
29 | | -test('should build succeed', async ({ page }) => { |
30 | | - const rsbuild = await createRsbuild({ |
31 | | - cwd: __dirname, |
32 | | - rsbuildConfig: { |
33 | | - plugins: [pluginImageCompress()], |
34 | | - }, |
35 | | - }); |
36 | | - |
37 | | - await rsbuild.build(); |
38 | | - const { server, urls } = await rsbuild.preview(); |
39 | | - |
40 | | - await page.goto(urls[0]); |
41 | | - expect(await page.evaluate('window.test')).toBe(1); |
42 | | - |
43 | | - await server.close(); |
| 18 | + await expect(rsbuild.build()).resolves.toBeDefined(); |
| 19 | + |
| 20 | + const jpeg = readFileSync( |
| 21 | + join(__dirname, 'dist/static/image/image.jpeg'), |
| 22 | + 'utf-8', |
| 23 | + ); |
| 24 | + const png = readFileSync( |
| 25 | + join(__dirname, 'dist/static/image/image.png'), |
| 26 | + 'utf-8', |
| 27 | + ); |
| 28 | + const svg = readFileSync( |
| 29 | + join(__dirname, 'dist/static/image/mobile.svg'), |
| 30 | + 'utf-8', |
| 31 | + ); |
| 32 | + // const ico = names.find((item) => item.endsWith('.ico'))!; |
| 33 | + |
| 34 | + const assetsDir = join(__dirname, '../assets'); |
| 35 | + const originJpeg = readFileSync(join(assetsDir, 'image.jpeg'), 'utf-8'); |
| 36 | + const originPng = readFileSync(join(assetsDir, 'image.png'), 'utf-8'); |
| 37 | + const originSvg = readFileSync(join(assetsDir, 'mobile.svg'), 'utf-8'); |
| 38 | + // const originIco = readFileSync(join(assetsDir, 'image.ico'), 'utf-8'); |
| 39 | + |
| 40 | + expect(outputs[jpeg].length).toBeLessThan(originJpeg.length); |
| 41 | + expect(outputs[png].length).toBeLessThan(originPng.length); |
| 42 | + expect(outputs[svg].length).toBeLessThan(originSvg.length); |
| 43 | + // TODO ico file size is not less than origin |
| 44 | + // expect(outputs[ico].length).toBeLessThan(originIco.length); |
44 | 45 | }); |
0 commit comments