Skip to content

Commit 0d5aef6

Browse files
committed
Add vite plugin integration fixture
1 parent ca4c9b8 commit 0d5aef6

9 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/
2+
node_modules/
3+
!tailwind.config.js
4+
!index.html

integrations/vite-plugin/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="./index.css" />
7+
<title>Vite Plugin Integration</title>
8+
</head>
9+
<body>
10+
<div class="w-1/2 h-[80.5%] bg-[#6b2c3e] p-4 rounded"></div>
11+
</body>
12+
</html>

integrations/vite-plugin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.css'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@tailwindcss/integrations-vite-plugin",
3+
"private": true,
4+
"version": "0.0.0",
5+
"main": "./index.js",
6+
"browser": "./index.js",
7+
"scripts": {
8+
"build": "vite build",
9+
"test": "jest --runInBand --forceExit"
10+
},
11+
"jest": {
12+
"testTimeout": 10000,
13+
"displayName": "vite-plugin",
14+
"setupFilesAfterEnv": [
15+
"<rootDir>/../../jest/customMatchers.js"
16+
],
17+
"transform": {
18+
"\\.js$": "@swc/jest",
19+
"\\.ts$": "@swc/jest"
20+
}
21+
},
22+
"devDependencies": {
23+
"isomorphic-fetch": "^3.0.0",
24+
"vite": "^5.2.8"
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function formatNumber(value) {
2+
if (Number.isInteger(value)) {
3+
return String(value)
4+
}
5+
6+
return value.toFixed(6).replace(/\.?0+$/, '')
7+
}
8+
9+
function remToRpx(rootValue = 32) {
10+
return {
11+
postcssPlugin: 'rem-to-rpx',
12+
Declaration(decl) {
13+
decl.value = decl.value.replace(/(-?\d*\.?\d+)rem\b/g, (_, rawValue) => {
14+
return `${formatNumber(Number(rawValue) * rootValue)}rpx`
15+
})
16+
},
17+
}
18+
}
19+
20+
remToRpx.postcss = true
21+
22+
module.exports = remToRpx
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
content: ['./index.html'],
3+
miniPrograms: true,
4+
theme: {
5+
extend: {},
6+
},
7+
corePlugins: {
8+
preflight: false,
9+
},
10+
plugins: [],
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
try {
2+
require('isomorphic-fetch')
3+
} catch {}
4+
5+
let $ = require('../../execute')
6+
let { css } = require('../../syntax')
7+
8+
let { readOutputFile } = require('../../io')({
9+
output: 'dist',
10+
input: '.',
11+
})
12+
13+
describe('static build', () => {
14+
test('the Vite plugin transforms classes and preserves user PostCSS plugins', async () => {
15+
await $('vite build', {
16+
env: { NODE_ENV: 'production', NO_COLOR: '1' },
17+
})
18+
19+
expect(await readOutputFile('index.html')).toContain(
20+
'class="w-1_2 h-_80_5__ bg-__6b2c3e_ p-4 rounded"'
21+
)
22+
23+
expect(await readOutputFile(/index.[a-z0-9_-]+\.css$/i)).toIncludeCss(
24+
css`
25+
.h-_80_5__ {
26+
height: 80.5%;
27+
}
28+
.w-1_2 {
29+
width: 50%;
30+
}
31+
.rounded {
32+
border-radius: 8rpx;
33+
}
34+
.bg-__6b2c3e_ {
35+
--tw-bg-opacity: 1;
36+
background-color: rgb(107 44 62 / var(--tw-bg-opacity, 1));
37+
}
38+
.p-4 {
39+
padding: 32rpx;
40+
}
41+
`
42+
)
43+
})
44+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let path = require('path')
2+
let tailwindcss = require(path.resolve(__dirname, '..', '..', 'vite'))
3+
let remToRpx = require('./rem-to-rpx')
4+
5+
module.exports = {
6+
plugins: [tailwindcss()],
7+
css: {
8+
postcss: {
9+
plugins: [remToRpx()],
10+
},
11+
},
12+
}

0 commit comments

Comments
 (0)