Skip to content

Commit 91f374a

Browse files
committed
feat: vue scoped case commit
1 parent 2bcabb5 commit 91f374a

File tree

8 files changed

+390
-28
lines changed

8 files changed

+390
-28
lines changed

packages/unplugin-tailwindcss-mangle/src/css/plugins.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ const postcssMangleTailwindcssPlugin: PostcssMangleTailwindcssPlugin = (options)
2020
postcssPlugin,
2121
Rule(rule, helper) {
2222
rule.selector = parser((selectors) => {
23-
selectors.walk((s) => {
23+
selectors.walkClasses((s) => {
2424
if (s.value) {
2525
const hit = newClassMap[s.value]
2626
if (hit) {
2727
// console.log(s.value, hit.name)
2828
s.value = hit.name
2929
}
30+
// for vue scoped gap-y-4[data-v-0f84999b]
31+
// const idx = s.value.indexOf('[data-v-')
32+
// const isVueScoped = idx > -1
33+
// if (isVueScoped) {
34+
// const prefixCls = s.value.substring(0, idx)
35+
// const hit = newClassMap[prefixCls]
36+
// if (hit) {
37+
// s.value = hit.name + s.value.substring(idx)
38+
// }
39+
// } else {
40+
// const hit = newClassMap[s.value]
41+
// if (hit) {
42+
// // console.log(s.value, hit.name)
43+
// s.value = hit.name
44+
// }
45+
// }
3046
}
3147
})
3248
}).processSync(rule.selector)

packages/unplugin-tailwindcss-mangle/src/index.ts

+11-24
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
5151
for (let i = 0; i < groupedEntries.js.length; i++) {
5252
const [file, chunk] = groupedEntries.js[i] as [string, OutputChunk]
5353
if (isInclude(file)) {
54-
chunk.code = jsHandler(chunk.code, {
54+
const code = jsHandler(chunk.code, {
5555
runtimeSet,
5656
classGenerator
5757
}).code
58+
if (code) {
59+
chunk.code = code
60+
}
5861
}
5962
}
6063
}
@@ -155,8 +158,10 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
155158
runtimeSet,
156159
classGenerator
157160
}).code
158-
const source = new ConcatSource(code)
159-
compilation.updateAsset(file, source)
161+
if (code) {
162+
const source = new ConcatSource(code)
163+
compilation.updateAsset(file, source)
164+
}
160165
}
161166
}
162167
}
@@ -197,8 +202,9 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
197202
runtimeSet,
198203
classGenerator
199204
}).code
200-
201-
overwriteServerSideAsset(key, file, code)
205+
if (code) {
206+
overwriteServerSideAsset(key, file, code)
207+
}
202208
}
203209
})
204210
js.clear()
@@ -228,28 +234,9 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
228234
export const vitePlugin = unplugin.vite
229235
export const webpackPlugin = unplugin.webpack
230236

231-
// export const nuxtPlugin = function (options: Options = {}, nuxt: any) {
232-
// // install webpack plugin
233-
// nuxt.hook('webpack:config', async (config: any) => {
234-
// config.plugins = config.plugins || []
235-
// config.plugins.unshift(unplugin.webpack(options))
236-
// })
237-
238-
// // install vite plugin
239-
// nuxt.hook('vite:extendConfig', async (config: any) => {
240-
// config.plugins = config.plugins || []
241-
// config.plugins.push(unplugin.vite(options))
242-
// })
243-
// }
244-
245237
// export default unplugin
246238
// export const vitePlugin = unplugin.vite
247239
// export const rollupPlugin = unplugin.rollup
248240
// export const webpackPlugin = unplugin.webpack
249241
// export const rspackPlugin = unplugin.rspack
250242
// export const esbuildPlugin = unplugin.esbuild
251-
// export const vitePlugin = unplugin.vite
252-
// export const rollupPlugin = unplugin.rollup
253-
// export const webpackPlugin = unplugin.webpack
254-
// export const rspackPlugin = unplugin.rspack
255-
// export const esbuildPlugin = unplugin.esbuild

packages/unplugin-tailwindcss-mangle/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function getGroupedEntries<T>(
3636
return /\.html?$/.test(file)
3737
},
3838
jsMatcher(file: string) {
39-
return /\.js$/.test(file)
39+
return /\.[cm]?js$/.test(file)
4040
}
4141
}
4242
) {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { unplugin } from '.'
2-
import type { } from 'vite'
2+
import type {} from 'vite'
33
export default unplugin.vite
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`css vue scoped .gap-y-4[data-v-0f84999b] 1`] = `
4+
"@media (min-width: 768px) {
5+
.tw-a[data-v-0f84999b] {
6+
}
7+
}"
8+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ClassGenerator from '../src/classGenerator'
2+
import { cssHandler } from '../src/css/index'
3+
4+
describe('css', () => {
5+
it('vue scoped .gap-y-4[data-v-0f84999b]', () => {
6+
const classGenerator = new ClassGenerator()
7+
const runtimeSet = new Set<string>()
8+
runtimeSet.add('gap-y-4')
9+
classGenerator.generateClassName('gap-y-4')
10+
const testCase = `@media (min-width: 768px) {
11+
.gap-y-4[data-v-0f84999b] {
12+
}
13+
}`
14+
const css = cssHandler(testCase, {
15+
classGenerator,
16+
runtimeSet
17+
})
18+
expect(css).toMatchSnapshot()
19+
})
20+
})

0 commit comments

Comments
 (0)