Skip to content

Commit

Permalink
test: remove plugin-vue from tailwind playground (#14073)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 14, 2023
1 parent d36d6fb commit 0768625
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test.runIf(isServe)('postcss plugins in different dir', async () => {
await server.listen()
try {
await page.goto(`http://localhost:${port}`)
const tailwindStyle = await page.$('.tailwind-style')
const tailwindStyle = page.locator('#tailwind-style')
expect(await getBgColor(tailwindStyle)).toBe('rgb(254, 226, 226)')
expect(await getColor(tailwindStyle)).toBe('rgb(136, 136, 136)')
} finally {
Expand Down
114 changes: 56 additions & 58 deletions playground/tailwind/__test__/tailwind.spec.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,82 @@
import { expect, test } from 'vitest'
import {
editFile,
getBgColor,
getColor,
isBuild,
isServe,
page,
untilBrowserLogAfter,
untilUpdated,
} from '~utils'

test('should render', async () => {
expect(await page.textContent('#pagetitle')).toBe('|Page title|')
expect(await page.textContent('#pagetitle')).toBe('Page title')
})

if (!isBuild) {
test('regenerate CSS and HMR (glob pattern)', async () => {
const el = await page.$('#pagetitle')
const el2 = await page.$('#helloroot')
expect(await getColor(el)).toBe('rgb(11, 22, 33)')
test.runIf(isServe)('regenerate CSS and HMR (glob pattern)', async () => {
const el = page.locator('#view1-text')
expect(await getColor(el)).toBe('rgb(22, 163, 74)')

await untilBrowserLogAfter(
() =>
editFile('src/views/Page.vue', (code) =>
code.replace('|Page title|', '|Page title updated|'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/views/Page.vue',
],
false,
)
await untilUpdated(() => el.textContent(), '|Page title updated|')
await untilBrowserLogAfter(
() =>
editFile('src/views/view1.js', (code) =>
code.replace('|view1|', '|view1 updated|'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/views/view1.js via /src/main.js',
],
false,
)
await untilUpdated(() => el.textContent(), '|view1 updated|')

await untilBrowserLogAfter(
() =>
editFile('src/components/HelloWorld.vue', (code) =>
code.replace('text-gray-800', 'text-[rgb(10,20,30)]'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/components/HelloWorld.vue',
],
false,
)
await untilUpdated(() => getColor(el2), 'rgb(10, 20, 30)')
})
await untilBrowserLogAfter(
() =>
editFile('src/views/view1.js', (code) =>
code.replace('text-green-600', 'text-orange-600'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/views/view1.js via /src/main.js',
],
false,
)
// await new Promise(resolve => setTimeout(resolve, 10))
await untilUpdated(async () => getColor(el), 'rgb(234, 88, 12)')
})

test('regenerate CSS and HMR (relative path)', async () => {
const el = await page.$('h1')
expect(await getColor(el)).toBe('black')
test.runIf(isServe)(
'same file duplicated in module graph (#4267)',
async () => {
const el = page.locator('#component1')
expect(await getColor(el)).toBe('rgb(220, 38, 38)')

// when duplicated, page reload happens
await untilBrowserLogAfter(
() =>
editFile('src/App.vue', (code) =>
code.replace('text-black', 'text-[rgb(11,22,33)]'),
editFile('src/components/component1.js', (code) =>
code.replace('text-red-600', 'text-blue-600'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/App.vue',
'[vite] hot updated: /src/components/component1.js',
],
false,
)
await untilUpdated(() => getColor(el), 'rgb(11, 22, 33)')
})
await untilUpdated(() => getColor(el), 'rgb(37, 99, 235)')
},
)

test('regenerate CSS and HMR (pug template)', async () => {
const el = await page.$('.pug')
expect(await getBgColor(el)).toBe('rgb(248, 113, 113)')
test.runIf(isServe)('regenerate CSS and HMR (relative path)', async () => {
const el = page.locator('#pagetitle')
expect(await getColor(el)).toBe('rgb(124, 58, 237)')

await untilBrowserLogAfter(
() =>
editFile('src/components/PugTemplate.vue', (code) =>
code.replace('bg-red-400', 'bg-red-600'),
),
[
'[vite] css hot updated: /index.css',
'[vite] hot updated: /src/components/PugTemplate.vue?vue&type=template&lang.js',
],
false,
)
await untilUpdated(() => getBgColor(el), 'rgb(220, 38, 38)')
})
}
await untilBrowserLogAfter(
() =>
editFile('src/main.js', (code) =>
code.replace('text-violet-600', 'text-cyan-600'),
),
['[vite] css hot updated: /index.css', '[vite] hot updated: /src/main.js'],
false,
)
await untilUpdated(() => getColor(el), 'rgb(8, 145, 178)')
})
19 changes: 5 additions & 14 deletions playground/tailwind/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
<link rel="stylesheet" href="./index.css" />

<div id="app"></div>

<script type="module" src="/src/main.js" defer></script>
12 changes: 0 additions & 12 deletions playground/tailwind/src/App.vue

This file was deleted.

Binary file removed playground/tailwind/src/assets/logo.png
Binary file not shown.
10 changes: 0 additions & 10 deletions playground/tailwind/src/components/HelloWorld.vue

This file was deleted.

3 changes: 0 additions & 3 deletions playground/tailwind/src/components/PugTemplate.vue

This file was deleted.

9 changes: 9 additions & 0 deletions playground/tailwind/src/components/component1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const component1 = /* html */ `
<div id="component1" data-id="component1" class="text-red-600">component1</div>
`

import.meta.hot?.accept((mod) => {
document.querySelectorAll('[data-id="component1"]').forEach((d) => {
d.outerHTML = mod.component1
})
})
22 changes: 17 additions & 5 deletions playground/tailwind/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// import '../index.css';
import { view1 } from './views/view1'

createApp(App).use(router).mount('#app')
export const main = (view1Content) => /* html */ `
<h1 id="pagetitle" class="text-3xl text-violet-600">Page title</h1>
${view1Content}
<!-- used in postcss-plugins-different-dir -->
<div id="tailwind-style" class="bg-red-100 text-[#888888]">style</div>
`

document.getElementById('app').innerHTML = main(view1)

import.meta.hot?.accept((mod) => {
document.getElementById('app').innerHTML = mod.main(view1)
})

import.meta.hot?.accept(['./views/view1'], ([mod]) => {
document.getElementById('app').innerHTML = main(mod.view1)
})
16 changes: 0 additions & 16 deletions playground/tailwind/src/router.ts

This file was deleted.

2 changes: 0 additions & 2 deletions playground/tailwind/src/utils.ts

This file was deleted.

33 changes: 0 additions & 33 deletions playground/tailwind/src/views/Page.vue

This file was deleted.

8 changes: 8 additions & 0 deletions playground/tailwind/src/views/view1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { component1 } from '../components/component1'

export const view1 = /* html */ `
<div data-id="view1">
<div id="view1-text" class="text-green-600">|view1|</div>
${component1}
</div>
`
6 changes: 3 additions & 3 deletions playground/tailwind/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

module.exports = {
content: [
// Before editing this section, make sure no paths are matching with `/src/App.vue`
// Before editing this section, make sure no paths are matching with `/src/main.js`
// Look https://github.com/vitejs/vite/pull/6959 for more details
__dirname + '/src/{components,views}/**/*.vue',
__dirname + '/src/App.vue',
__dirname + '/src/{components,views}/**/*.js',
__dirname + '/src/main.js',
],
theme: {
extend: {},
Expand Down
13 changes: 11 additions & 2 deletions playground/tailwind/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
resolve: {
alias: {
'/@': __dirname,
},
},
plugins: [vue()],
build: {
// to make tests faster
minify: false,
Expand All @@ -17,4 +15,15 @@ export default defineConfig({
// although it should not affect the build
origin: 'http://localhost:8080',
},
plugins: [
{
name: 'delay view',
enforce: 'pre',
async transform(_code, id) {
if (id.includes('views/view1.js')) {
await new Promise((resolve) => setTimeout(resolve, 100))
}
},
},
],
})
25 changes: 20 additions & 5 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import fs from 'node:fs'
import path from 'node:path'
import colors from 'css-color-names'
import type { ConsoleMessage, ElementHandle } from 'playwright-chromium'
import type {
ConsoleMessage,
ElementHandle,
Locator,
} from 'playwright-chromium'
import type { DepOptimizationMetadata, Manifest } from 'vite'
import { normalizePath } from 'vite'
import { fromComment } from 'convert-source-map'
Expand Down Expand Up @@ -74,29 +78,40 @@ function rgbToHex(rgb: string): string {

const timeout = (n: number) => new Promise((r) => setTimeout(r, n))

async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
async function toEl(
el: string | ElementHandle | Locator,
): Promise<ElementHandle> {
if (typeof el === 'string') {
const realEl = await page.$(el)
if (realEl == null) {
throw new Error(`Cannot find element: "${el}"`)
}
return realEl
}
if ('elementHandle' in el) {
return el.elementHandle()
}
return el
}

export async function getColor(el: string | ElementHandle): Promise<string> {
export async function getColor(
el: string | ElementHandle | Locator,
): Promise<string> {
el = await toEl(el)
const rgb = await el.evaluate((el) => getComputedStyle(el as Element).color)
return hexToNameMap[rgbToHex(rgb)] ?? rgb
}

export async function getBg(el: string | ElementHandle): Promise<string> {
export async function getBg(
el: string | ElementHandle | Locator,
): Promise<string> {
el = await toEl(el)
return el.evaluate((el) => getComputedStyle(el as Element).backgroundImage)
}

export async function getBgColor(el: string | ElementHandle): Promise<string> {
export async function getBgColor(
el: string | ElementHandle | Locator,
): Promise<string> {
el = await toEl(el)
return el.evaluate((el) => getComputedStyle(el as Element).backgroundColor)
}
Expand Down

0 comments on commit 0768625

Please sign in to comment.