Skip to content

Commit a751505

Browse files
committed
feat: add vue compiler
1 parent aaf7078 commit a751505

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

packages/core/src/html/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Parser } from 'htmlparser2'
22
import MagicString from 'magic-string'
33
import type { IHtmlHandlerOptions } from '../types'
44
import { makeRegex, splitCode } from '../shared'
5+
import { jsHandler } from '@/js'
56

67
export function htmlHandler(raw: string | MagicString, options: IHtmlHandlerOptions) {
7-
const { ctx } = options
8+
const { ctx, isVue } = options
89
const { replaceMap } = ctx
910
const ms: MagicString = typeof raw === 'string' ? new MagicString(raw) : raw
1011
const parser = new Parser({
@@ -21,6 +22,14 @@ export function htmlHandler(raw: string | MagicString, options: IHtmlHandlerOpti
2122
}
2223
ms.update(parser.startIndex + name.length + 2, parser.endIndex - 1, rawValue)
2324
}
25+
if (isVue) {
26+
if (name === ':class') {
27+
const { code } = jsHandler(value, {
28+
ctx,
29+
})
30+
ms.update(parser.startIndex + name.length + 2, parser.endIndex - 1, code)
31+
}
32+
}
2433
},
2534
})
2635
parser.write(ms.original)

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './ctx'
22
export * from './css'
33
export * from './html'
44
export * from './js'
5+
export * from './vue'
56
export { ClassGenerator } from './shared'
67
export * from './types'

packages/core/src/js/pre.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { splitCode } from '@tailwindcss-mangle/shared'
33
import { sort } from 'fast-sort'
44
import { jsStringEscape } from '@ast-core/escape'
55
import type { ParseResult } from '@babel/parser'
6+
import type { NodePath } from '@babel/traverse'
7+
import type { File, StringLiteral, TemplateElement } from '@babel/types'
68
import { escapeStringRegexp } from '@/utils'
79
import type { Context } from '@/ctx'
810
import { between } from '@/math'
@@ -18,7 +20,7 @@ interface Options {
1820

1921
type HandleValueOptions = {
2022
raw: string
21-
path: babel.NodePath<babel.types.StringLiteral | babel.types.TemplateElement>
23+
path: NodePath<StringLiteral | TemplateElement>
2224
offset: number
2325
escape: boolean
2426
} & Options
@@ -62,7 +64,7 @@ export function preProcessJs(options: IPreProcessJsOptions): string {
6264
const { code, id, ctx } = options
6365
const { replaceMap } = ctx
6466
const magicString = typeof code === 'string' ? new MagicString(code) : code
65-
let ast: ParseResult<babel.types.File>
67+
let ast: ParseResult<File>
6668
try {
6769
const file = parse(magicString.original, {
6870
sourceType: 'unambiguous',
@@ -167,7 +169,7 @@ export function preProcessRawCode(options: IPreProcessJsOptions): string {
167169
}
168170
// magicString.original.matchAll(regex)
169171
for (const regExpMatch of allArr) {
170-
let ast: ParseResult<babel.types.File> | null
172+
let ast: ParseResult<File> | null
171173
try {
172174
ast = parse(regExpMatch[0], {
173175
sourceType: 'unambiguous',

packages/core/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export interface IHandlerOptions {
2020
ctx: Context
2121
}
2222

23-
export interface IHtmlHandlerOptions extends IHandlerOptions {}
23+
export interface IHtmlHandlerOptions extends IHandlerOptions {
24+
isVue?: boolean
25+
}
2426

2527
export interface IJsHandlerOptions extends IHandlerOptions {
2628
splitQuote?: boolean

packages/core/src/vue/index.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
import { parse as parseVue } from '@vue/compiler-sfc'
22
import MagicString from 'magic-string'
33
import type { IVueHandlerOptions } from '@/types'
4-
import { parse as parseJs, traverse } from '@/babel'
4+
import { jsHandler } from '@/js'
5+
import { htmlHandler } from '@/html'
56

67
export function vueHandler(raw: string | MagicString, options: IVueHandlerOptions) {
78
const { ctx } = options
89
const ms: MagicString = typeof raw === 'string' ? new MagicString(raw) : raw
910
const { descriptor } = parseVue(ms.original)
1011
const { template, scriptSetup, script } = descriptor
11-
// console.log(template, scriptSetup, script)
12-
if (scriptSetup) {
13-
const ast = parseJs(scriptSetup.content, {
14-
sourceType: 'module',
15-
plugins: ['typescript'],
16-
})
17-
18-
traverse(ast, {
19-
StringLiteral: {
20-
enter(p) {
21-
22-
},
23-
},
24-
})
12+
if (template) {
13+
const code = htmlHandler(template.content, { ctx, isVue: true })
14+
ms.update(template.loc.start.offset, template.loc.end.offset, code)
2515
}
2616
if (script) {
27-
parseJs(script.content, {
28-
sourceType: 'module',
29-
plugins: ['typescript'],
30-
})
17+
const x = jsHandler(script.content, { ctx })
18+
ms.update(script.loc.start.offset, script.loc.end.offset, x.code)
19+
}
20+
if (scriptSetup) {
21+
const x = jsHandler(scriptSetup.content, { ctx })
22+
ms.update(scriptSetup.loc.start.offset, scriptSetup.loc.end.offset, x.code)
3123
}
3224

3325
return ms.toString()

packages/core/test/__snapshots__/vue.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ exports[`vue > test for vue 1`] = `
44
"<script setup lang="ts">
55
import { twMerge } from 'tailwind-merge'
66
// 'hover:bg-dark-red p-3 bg-[#B91C1C]'
7-
const aaa = twMerge('px-2 py-1 bg-red-100 hover:bg-red-800', 'p-3 bg-[#B91C1C]')
7+
const aaa = twMerge('px-2 py-1 bg-red-100 hover:bg-red-800', 'p-3 tw-c')
88
99
</script>
1010
1111
<template>
12-
<main class="flex min-h-screen flex-col items-center justify-between p-24 " :class="['before:text-[#123456]']">
12+
<main class="flex min-h-screen flex-col items-center tw-a p-24 " :class="['tw-b']">
1313
<nav :class="aaa">{{ aaa }}</nav>
14-
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
14+
<div class="z-10 w-full max-w-5xl items-center tw-a font-mono text-sm lg:flex">
1515
<p
1616
class="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
1717
Get started by editing&nbsp;

packages/core/test/vue.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ describe('vue', () => {
99
ctx = new Context()
1010
})
1111
it('test for vue', () => {
12+
ctx.replaceMap.set('bg-[#B91C1C]', 'xx')
13+
ctx.replaceMap.set('before:text-[#123456]', 'yy')
14+
ctx.replaceMap.set('justify-between', 'zz')
1215
const testCase = getTestCase('preserve-fn-case1.vue')
1316
expect(vueHandler(testCase, {
1417
ctx,

0 commit comments

Comments
 (0)