Home / File/ css-dynamic-import.spec.ts — vite Source File

css-dynamic-import.spec.ts — vite Source File

Architecture documentation for css-dynamic-import.spec.ts, a typescript file in the vite codebase. 3 imports, 0 dependents.

File typescript ViteCore ConfigEngine 3 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  dd71c169_a6c5_edad_b5a0_65b1cfb5183c["css-dynamic-import.spec.ts"]
  54c37fea_4fe7_2693_71cb_1276b77f55f9["vite"]
  dd71c169_a6c5_edad_b5a0_65b1cfb5183c --> 54c37fea_4fe7_2693_71cb_1276b77f55f9
  a340ba46_b2b7_3048_3bb3_6907a74c8464["vitest"]
  dd71c169_a6c5_edad_b5a0_65b1cfb5183c --> a340ba46_b2b7_3048_3bb3_6907a74c8464
  d3fd5575_295b_d6be_24dd_62d277645dc9["~utils"]
  dd71c169_a6c5_edad_b5a0_65b1cfb5183c --> d3fd5575_295b_d6be_24dd_62d277645dc9
  style dd71c169_a6c5_edad_b5a0_65b1cfb5183c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { InlineConfig } from 'vite'
import { build, createServer, preview } from 'vite'
import { expect, test } from 'vitest'
import { getColor, isBuild, isServe, page, ports, rootDir } from '~utils'

const baseOptions = [
  { base: '', label: 'relative' },
  { base: '/', label: 'absolute' },
]

const getConfig = (base: string): InlineConfig => ({
  base,
  root: rootDir,
  logLevel: 'silent',
  server: { port: ports['css/dynamic-import'] },
  preview: { port: ports['css/dynamic-import'] },
  build: { assetsInlineLimit: 0 },
})

async function withBuild(base: string, fn: () => Promise<void>) {
  const config = getConfig(base)
  await build(config)
  const server = await preview(config)

  try {
    await page.goto(server.resolvedUrls.local[0])
    await fn()
  } finally {
    server.httpServer.close()
  }
}

async function withServe(base: string, fn: () => Promise<void>) {
  const config = getConfig(base)
  const server = await createServer(config)
  await server.listen()

  try {
    await page.goto(server.resolvedUrls.local[0])
    await fn()
  } finally {
    await page.goto('about:blank') // move to a different page to avoid auto-refresh after server start
    await server.close()
  }
}

async function getLinks() {
  const links = await page.$$('link')
  return await Promise.all(
    links.map((handle) => {
      return handle.evaluate((link) => ({
        pathname: new URL(link.href).pathname,
        rel: link.rel,
        as: link.as,
      }))
    }),
  )
}

baseOptions.forEach(({ base, label }) => {
  test.runIf(isBuild)(
    `doesn't duplicate dynamically imported css files when built with ${label} base`,
    async () => {
      await withBuild(base, async () => {
        await page.waitForSelector('.loaded', { state: 'attached' })

        expect(await getColor('.css-dynamic-import')).toBe('green')
        const linkUrls = (await getLinks()).map((link) => link.pathname)
        const uniqueLinkUrls = [...new Set(linkUrls)]
        expect(linkUrls).toStrictEqual(uniqueLinkUrls)
      })
    },
  )

  test.runIf(isServe)(
    `doesn't duplicate dynamically imported css files when served with ${label} base`,
    async () => {
      await withServe(base, async () => {
        await page.waitForSelector('.loaded', { state: 'attached' })

        expect(await getColor('.css-dynamic-import')).toBe('green')
        // in serve there is no preloading
        expect(await getLinks()).toEqual([
          {
            pathname: '/dynamic.css',
            rel: 'preload',
            as: 'style',
          },
        ])
      })
    },
  )
})

Domain

Subdomains

Dependencies

  • vite
  • vitest
  • ~utils

Frequently Asked Questions

What does css-dynamic-import.spec.ts do?
css-dynamic-import.spec.ts is a source file in the vite codebase, written in typescript. It belongs to the ViteCore domain, ConfigEngine subdomain.
What functions are defined in css-dynamic-import.spec.ts?
css-dynamic-import.spec.ts defines 4 function(s): getConfig, getLinks, withBuild, withServe.
What does css-dynamic-import.spec.ts depend on?
css-dynamic-import.spec.ts imports 3 module(s): vite, vitest, ~utils.
Where is css-dynamic-import.spec.ts in the architecture?
css-dynamic-import.spec.ts is located at playground/css-dynamic-import/__tests__/css-dynamic-import.spec.ts (domain: ViteCore, subdomain: ConfigEngine, directory: playground/css-dynamic-import/__tests__).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free