|
| 1 | +import type { InlineConfig } from 'vite' |
| 2 | +import { build, createServer, preview } from 'vite' |
| 3 | +import { expect, test } from 'vitest' |
| 4 | +import { getColor, isBuild, isServe, page, ports, rootDir } from '~utils' |
| 5 | + |
| 6 | +const baseOptions = [ |
| 7 | + { base: '', label: 'relative' }, |
| 8 | + { base: '/', label: 'absolute' } |
| 9 | +] |
| 10 | + |
| 11 | +const getConfig = (base: string): InlineConfig => ({ |
| 12 | + base, |
| 13 | + root: rootDir, |
| 14 | + logLevel: 'silent', |
| 15 | + preview: { port: ports['css/dynamic-import'] }, |
| 16 | + build: { assetsInlineLimit: 0 } |
| 17 | +}) |
| 18 | + |
| 19 | +async function withBuild(base: string, fn: () => Promise<void>) { |
| 20 | + const config = getConfig(base) |
| 21 | + await build(config) |
| 22 | + const server = await preview(config) |
| 23 | + |
| 24 | + try { |
| 25 | + await page.goto(server.resolvedUrls.local[0]) |
| 26 | + await fn() |
| 27 | + } finally { |
| 28 | + server.httpServer.close() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +async function withServe(base: string, fn: () => Promise<void>) { |
| 33 | + const config = getConfig(base) |
| 34 | + const server = await createServer(config) |
| 35 | + await server.listen() |
| 36 | + await new Promise((r) => setTimeout(r, 500)) |
| 37 | + |
| 38 | + try { |
| 39 | + await page.goto(server.resolvedUrls.local[0]) |
| 40 | + await fn() |
| 41 | + } finally { |
| 42 | + await server.close() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +async function getLinks() { |
| 47 | + const links = await page.$$('link') |
| 48 | + return await Promise.all( |
| 49 | + links.map((handle) => { |
| 50 | + return handle.evaluate((link) => ({ |
| 51 | + pathname: new URL(link.href).pathname, |
| 52 | + rel: link.rel, |
| 53 | + as: link.as |
| 54 | + })) |
| 55 | + }) |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +baseOptions.forEach(({ base, label }) => { |
| 60 | + test.runIf(isBuild)( |
| 61 | + `doesn't duplicate dynamically imported css files when built with ${label} base`, |
| 62 | + async () => { |
| 63 | + await withBuild(base, async () => { |
| 64 | + await page.waitForSelector('.loaded', { state: 'attached' }) |
| 65 | + |
| 66 | + expect(await getColor('.css-dynamic-import')).toBe('green') |
| 67 | + expect(await getLinks()).toEqual([ |
| 68 | + { |
| 69 | + pathname: expect.stringMatching(/^\/assets\/index\..+\.css$/), |
| 70 | + rel: 'stylesheet', |
| 71 | + as: '' |
| 72 | + }, |
| 73 | + { |
| 74 | + pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.css$/), |
| 75 | + rel: 'preload', |
| 76 | + as: 'style' |
| 77 | + }, |
| 78 | + { |
| 79 | + pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.js$/), |
| 80 | + rel: 'modulepreload', |
| 81 | + as: 'script' |
| 82 | + }, |
| 83 | + { |
| 84 | + pathname: expect.stringMatching(/^\/assets\/dynamic\..+\.css$/), |
| 85 | + rel: 'stylesheet', |
| 86 | + as: '' |
| 87 | + }, |
| 88 | + { |
| 89 | + pathname: expect.stringMatching(/^\/assets\/static\..+\.js$/), |
| 90 | + rel: 'modulepreload', |
| 91 | + as: 'script' |
| 92 | + }, |
| 93 | + { |
| 94 | + pathname: expect.stringMatching(/^\/assets\/index\..+\.js$/), |
| 95 | + rel: 'modulepreload', |
| 96 | + as: 'script' |
| 97 | + } |
| 98 | + ]) |
| 99 | + }) |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + test.runIf(isServe)( |
| 104 | + `doesn't duplicate dynamically imported css files when served with ${label} base`, |
| 105 | + async () => { |
| 106 | + await withServe(base, async () => { |
| 107 | + await page.waitForSelector('.loaded', { state: 'attached' }) |
| 108 | + |
| 109 | + expect(await getColor('.css-dynamic-import')).toBe('green') |
| 110 | + // in serve there is no preloading |
| 111 | + expect(await getLinks()).toEqual([ |
| 112 | + { |
| 113 | + pathname: '/dynamic.css', |
| 114 | + rel: 'preload', |
| 115 | + as: 'style' |
| 116 | + } |
| 117 | + ]) |
| 118 | + }) |
| 119 | + } |
| 120 | + ) |
| 121 | +}) |
0 commit comments