Skip to content

Commit c99097d

Browse files
committed
refactor: replace direct Vue compiler import with dynamic import utility
1 parent 84099ec commit c99097d

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/prettierCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// @ts-expect-error vue
2-
import { parse } from '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'
1+
import { getVueCompilerSfc } from './utils'
32

43
const emptyStyle = /<style[\s\w'=]*>(\s+)/
54

65
export async function prettierCode(code: string) {
6+
const { parse } = await getVueCompilerSfc()
77
const {
88
descriptor: { styles },
99
} = parse(code)

src/transformCss.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import fsp from 'node:fs/promises'
22
import path from 'node:path'
33
import { escapeRegExp } from '@unocss/core'
4-
// @ts-expect-error vue
5-
import { parse } from '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'
64
import {
75
isNot,
86
joinWithUnderLine,
@@ -18,6 +16,7 @@ import {
1816
diffTemplateStyle,
1917
getCssType,
2018
getStyleScoped,
19+
getVueCompilerSfc,
2120
isEmptyStyle,
2221
TRANSFER_FLAG,
2322
transformUnocssBack,
@@ -57,6 +56,7 @@ export async function transformCss(
5756
): Promise<string> {
5857
isRem = _isRem
5958
const allChanges: AllChange[] = []
59+
const { parse } = await getVueCompilerSfc()
6060
let newCode = (await importCss(code, style, filepath, isJsx, debug)) as string
6161

6262
if (debug) {

src/transformVue.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { CssType } from './utils'
2-
// @ts-expect-error vue
3-
import { parse } from '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'
42
import { compilerCss } from './compilerCss'
53
import { prettierCode } from './prettierCode'
64
import { transformCss } from './transformCss'
75
import { transformInlineStyle } from './transformInlineStyle'
86
import { transformMedia } from './transformMedia'
7+
import { getVueCompilerSfc } from './utils' // 从utils引入公共函数
98

109
interface Options {
1110
isJsx?: boolean
@@ -17,7 +16,7 @@ interface Options {
1716

1817
export async function transformVue(code: string, options?: Options) {
1918
const { isJsx, filepath, isRem, globalCss, debug } = options || {}
20-
19+
const { parse } = await getVueCompilerSfc()
2120
if (debug) {
2221
console.log(
2322
'[DEBUG] transformVue started:',

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,12 @@ export function getCssType(filename: string) {
6666
const result = ext === 'styl' ? 'stylus' : ext
6767
return result as CssType
6868
}
69+
70+
/**
71+
* 动态导入 Vue Compiler SFC,避免打包时的问题
72+
* @returns Vue Compiler SFC 中的方法
73+
*/
74+
export async function getVueCompilerSfc() {
75+
const { parse } = await import('@vue/compiler-sfc')
76+
return { parse }
77+
}

0 commit comments

Comments
 (0)