Skip to content

Commit badf5cb

Browse files
committed
chore: use normalizePath
1 parent 6b423fe commit badf5cb

File tree

11 files changed

+83
-76
lines changed

11 files changed

+83
-76
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"test:coverage": "vitest --coverage"
7474
},
7575
"peerDependencies": {
76-
"baiwusanyu-utils": "^1.0.5",
76+
"baiwusanyu-utils": "^1.0.8",
7777
"chalk": "^4.1.2",
7878
"estree-walker-ts": "^1.0.0",
7979
"fast-glob": "^3.2.12",
@@ -84,7 +84,7 @@
8484
"vue": "^3.2.47"
8585
},
8686
"dependencies": {
87-
"baiwusanyu-utils": "^1.0.5",
87+
"baiwusanyu-utils": "^1.0.8",
8888
"chalk": "^4.1.2",
8989
"estree-walker-ts": "^1.0.0",
9090
"fast-glob": "^3.2.12",

packages/core/hmr/__test__/hmr.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'path'
22
import { beforeEach, describe, expect, test } from 'vitest'
3-
import { transformSymbol } from '@unplugin-vue-cssvars/utils'
3+
import { normalizePath } from 'baiwusanyu-utils'
44
import { reloadSFCModules, updatedCSSModules, viteHMR } from '../hmr'
55

66
const mockOption = {
@@ -9,7 +9,7 @@ const mockOption = {
99
includeCompile: ['**/**.scss', '**/**.css'],
1010
server: true,
1111
}
12-
const file = transformSymbol(`${resolve()}/packages/core/hmr/__test__/style/foo.css`)
12+
const file = normalizePath(`${resolve()}/packages/core/hmr/__test__/style/foo.css`)
1313
const mockModuleNode = new Set<any>()
1414
mockModuleNode.add({ id: 'foo.vue' })
1515

packages/core/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createUnplugin } from 'unplugin'
2-
import { NAME, transformSymbol } from '@unplugin-vue-cssvars/utils'
3-
import { log, setGlobalPrefix } from 'baiwusanyu-utils'
2+
import { NAME } from '@unplugin-vue-cssvars/utils'
3+
import { log, normalizePath, setGlobalPrefix } from 'baiwusanyu-utils'
4+
45
import { createFilter } from '@rollup/pluginutils'
56
import MagicString from 'magic-string'
67
import { preProcessCSS } from './runtime/pre-process-css'
@@ -50,7 +51,7 @@ const unplugin = createUnplugin<Options>(
5051
return filter(id)
5152
},
5253
async transform(code: string, id: string) {
53-
const transId = transformSymbol(id)
54+
const transId = normalizePath(id)
5455
let mgcStr = new MagicString(code)
5556
try {
5657
if (context.framework !== 'webpack'
@@ -98,7 +99,7 @@ const unplugin = createUnplugin<Options>(
9899
return filter(id)
99100
},
100101
async transform(code: string, id: string) {
101-
const transId = transformSymbol(id)
102+
const transId = normalizePath(id)
102103
let mgcStr = new MagicString(code)
103104
try {
104105
// transform in dev

packages/core/runtime/__test__/pre-process-css.spec.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'path'
22
import { describe, expect, test } from 'vitest'
3-
import { transformSymbol } from '@unplugin-vue-cssvars/utils'
3+
import { normalizePath } from 'baiwusanyu-utils'
44
import { getAllCSSFilePath, preProcessCSS } from '../pre-process-css'
55

66
describe('pre process css', () => {
@@ -11,8 +11,8 @@ describe('pre process css', () => {
1111
includeCompile: ['**/**.css'],
1212
},
1313
)
14-
const mockPathTest1 = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.css`)
15-
const mockPathTest2 = transformSymbol(`${resolve()}/core/runtime/__test__/style/test2.css`)
14+
const mockPathTest1 = normalizePath(`${resolve()}/core/runtime/__test__/style/test.css`)
15+
const mockPathTest2 = normalizePath(`${resolve()}/core/runtime/__test__/style/test2.css`)
1616
const resTest1 = res.get(mockPathTest1)
1717
const resTest2 = res.get(mockPathTest2)
1818
expect(resTest1).toBeTruthy()
@@ -33,9 +33,9 @@ describe('pre process css', () => {
3333
includeCompile: ['**/**.css', '**/**.scss'],
3434
},
3535
)
36-
const mockPathFooSCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo.scss`)
37-
const mockPathTestSCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.scss`)
38-
const mockPathTest2CSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test2.css`)
36+
const mockPathFooSCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo.scss`)
37+
const mockPathTestSCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.scss`)
38+
const mockPathTest2CSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test2.css`)
3939
// foo.scss -> test.css or test.scss ? -> test.scss
4040
const importerFooSCSS = res.get(mockPathFooSCSS)
4141
expect([...importerFooSCSS!.importer][0]).toBe(mockPathTestSCSS)
@@ -44,8 +44,8 @@ describe('pre process css', () => {
4444
expect([...importerTestSCSS!.importer][0]).toBe(mockPathTest2CSS)
4545

4646
// foo2.scss -> test2.css
47-
const mockPathFoo2SCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo2.scss`)
48-
const mockPathTestCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.css`)
47+
const mockPathFoo2SCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo2.scss`)
48+
const mockPathTestCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.css`)
4949
const importerFoo2SCSS = res.get(mockPathFoo2SCSS)
5050
expect([...importerFoo2SCSS!.importer][0]).toBe(mockPathTest2CSS)
5151
// test2.css -> test.css or test.scss ? -> test.css
@@ -60,9 +60,9 @@ describe('pre process css', () => {
6060
includeCompile: ['**/**.css', '**/**.less'],
6161
},
6262
)
63-
const mockPathFooLESS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo.less`)
64-
const mockPathTestLESS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.less`)
65-
const mockPathTest2CSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test2.css`)
63+
const mockPathFooLESS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo.less`)
64+
const mockPathTestLESS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.less`)
65+
const mockPathTest2CSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test2.css`)
6666
// foo.less -> test.css or test.less ? -> test.less
6767
const importerFooLESS = res.get(mockPathFooLESS)
6868
expect([...importerFooLESS!.importer][0]).toBe(mockPathTestLESS)
@@ -71,8 +71,8 @@ describe('pre process css', () => {
7171
expect([...importerTestLESS!.importer][0]).toBe(mockPathTest2CSS)
7272

7373
// foo2.less -> test2.css
74-
const mockPathFoo2LESS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo2.less`)
75-
const mockPathTestCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.css`)
74+
const mockPathFoo2LESS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo2.less`)
75+
const mockPathTestCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.css`)
7676
const importerFoo2LESS = res.get(mockPathFoo2LESS)
7777
expect([...importerFoo2LESS!.importer][0]).toBe(mockPathTest2CSS)
7878
// test2.css -> test.css or test.less ? -> test.css
@@ -87,9 +87,9 @@ describe('pre process css', () => {
8787
includeCompile: ['**/**.css', '**/**.styl'],
8888
},
8989
)
90-
const mockPathFooSTYL = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo.styl`)
91-
const mockPathTestSTYL = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.styl`)
92-
const mockPathTest2CSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test2.css`)
90+
const mockPathFooSTYL = normalizePath(`${resolve()}/core/runtime/__test__/style/foo.styl`)
91+
const mockPathTestSTYL = normalizePath(`${resolve()}/core/runtime/__test__/style/test.styl`)
92+
const mockPathTest2CSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test2.css`)
9393
// foo.styl -> test.css or test.styl ? -> test.styl
9494
const importerFooSTYL = res.get(mockPathFooSTYL)
9595
expect([...importerFooSTYL!.importer][0]).toBe(mockPathTestSTYL)
@@ -98,8 +98,8 @@ describe('pre process css', () => {
9898
expect([...importerTestSTYL!.importer][0]).toBe(mockPathTest2CSS)
9999

100100
// foo2.styl -> test2.css
101-
const mockPathFoo2STYL = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo2.styl`)
102-
const mockPathTestCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.css`)
101+
const mockPathFoo2STYL = normalizePath(`${resolve()}/core/runtime/__test__/style/foo2.styl`)
102+
const mockPathTestCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.css`)
103103
const importerFoo2STYL = res.get(mockPathFoo2STYL)
104104
expect([...importerFoo2STYL!.importer][0]).toBe(mockPathTest2CSS)
105105
// test2.css -> test.css or test.styl ? -> test.css
@@ -112,9 +112,9 @@ describe('pre process css', () => {
112112
rootDir: resolve('packages'),
113113
includeCompile: ['**/**.css', '**/**.sass'],
114114
})
115-
const mockPathFooSASS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo.sass`)
116-
const mockPathTestSASS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.sass`)
117-
const mockPathTest2CSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test2.css`)
115+
const mockPathFooSASS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo.sass`)
116+
const mockPathTestSASS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.sass`)
117+
const mockPathTest2CSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test2.css`)
118118
// foo.sass -> test.css or test.sass ? -> test.sass
119119
const importerFooSASS = res.get(mockPathFooSASS)
120120
expect([...importerFooSASS!.importer][0]).toBe(mockPathTestSASS)
@@ -123,8 +123,8 @@ describe('pre process css', () => {
123123
expect([...importerTestSASS!.importer][0]).toBe(mockPathTest2CSS)
124124

125125
// foo2.sass -> test2.css
126-
const mockPathFoo2SASS = transformSymbol(`${resolve()}/core/runtime/__test__/style/foo2.sass`)
127-
const mockPathTestCSS = transformSymbol(`${resolve()}/core/runtime/__test__/style/test.css`)
126+
const mockPathFoo2SASS = normalizePath(`${resolve()}/core/runtime/__test__/style/foo2.sass`)
127+
const mockPathTestCSS = normalizePath(`${resolve()}/core/runtime/__test__/style/test.css`)
128128
const importerFoo2SASS = res.get(mockPathFoo2SASS)
129129
expect([...importerFoo2SASS!.importer][0]).toBe(mockPathTest2CSS)
130130
// test2.css -> test.css or test.sass ? -> test.css

packages/core/runtime/__test__/process-css.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'path'
22
import { describe, expect, test, vi } from 'vitest'
3-
import { transformSymbol } from '@unplugin-vue-cssvars/utils'
3+
import { normalizePath } from 'baiwusanyu-utils'
44
import { getCSSFileRecursion, getVBindVariableListByPath, handleAlias } from '../process-css'
55
import type { ICSSFile } from '../../types'
66

@@ -131,7 +131,7 @@ describe('process css', () => {
131131
content: 'content foo color',
132132
lang: 'scss',
133133
}
134-
mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
134+
mockCssFiles.set(normalizePath(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
135135
const mockDescriptor = {
136136
styles: [{
137137
content: '@import "./assets/test";\n'
@@ -140,7 +140,7 @@ describe('process css', () => {
140140
+ ' }',
141141
}],
142142
}
143-
const mockId = transformSymbol(resolve('/play/src/App.vue'))
143+
const mockId = normalizePath(resolve('/play/src/App.vue'))
144144
const res = getVBindVariableListByPath(mockDescriptor as any, mockId, mockCssFiles, false)
145145
expect(res.vbindVariableListByPath).toMatchObject(['fooColor'])
146146
expect([...res.injectCSSContent]).toMatchObject([{
@@ -158,7 +158,7 @@ describe('process css', () => {
158158
content: 'content foo color',
159159
lang: 'scss',
160160
}
161-
mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
161+
mockCssFiles.set(normalizePath(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
162162
const mockDescriptor = {
163163
styles: [{
164164
content: '@import "./assets/test";\n'
@@ -167,7 +167,7 @@ describe('process css', () => {
167167
+ ' }',
168168
}],
169169
}
170-
const mockId = transformSymbol(resolve('/play/src/App.vue'))
170+
const mockId = normalizePath(resolve('/play/src/App.vue'))
171171
const res = getVBindVariableListByPath(mockDescriptor as any, mockId, mockCssFiles, true)
172172
expect(res.vbindVariableListByPath).toMatchObject(['fooColor'])
173173
expect([...res.injectCSSContent]).toMatchObject([])
@@ -182,7 +182,7 @@ describe('process css', () => {
182182
content: 'content foo color',
183183
lang: 'scss',
184184
}
185-
mockCssFiles.set(transformSymbol('/play/src/assets/test.css'), mockCSSFilesContent)
185+
mockCssFiles.set(normalizePath('/play/src/assets/test.css'), mockCSSFilesContent)
186186
const mockDescriptor = {
187187
styles: [{
188188
content: '@import "@/assets/test";\n'
@@ -191,7 +191,7 @@ describe('process css', () => {
191191
+ ' }',
192192
}],
193193
}
194-
const mockId = transformSymbol('/play/src/App.vue')
194+
const mockId = normalizePath('/play/src/App.vue')
195195
const res = getVBindVariableListByPath(
196196
mockDescriptor as any,
197197
mockId,
@@ -212,7 +212,7 @@ describe('process css', () => {
212212
importer: new Set(),
213213
vBindCode: ['fooColor'],
214214
}
215-
mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
215+
mockCssFiles.set(normalizePath(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
216216
const mockDescriptor = {
217217
styles: [{
218218
lang: 'scss',
@@ -222,7 +222,7 @@ describe('process css', () => {
222222
+ ' }',
223223
}],
224224
}
225-
const mockId = transformSymbol(resolve('/play/src/App.vue'))
225+
const mockId = normalizePath(resolve('/play/src/App.vue'))
226226
const res = getVBindVariableListByPath(mockDescriptor as any, mockId, mockCssFiles, true)
227227
expect(res.vbindVariableListByPath).toMatchObject(['fooColor'])
228228
expect(res).matchSnapshot()
@@ -234,12 +234,12 @@ describe('process css', () => {
234234
importer: new Set(),
235235
vBindCode: ['fooColor'],
236236
}
237-
mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
237+
mockCssFiles.set(normalizePath(resolve('/play/src/assets/test.css')), mockCSSFilesContent)
238238
const mockCSSFilesContent2 = {
239239
importer: new Set(),
240240
vBindCode: ['barColor'],
241241
}
242-
mockCssFiles.set(transformSymbol(resolve('/play/src/assets/test2.css')), mockCSSFilesContent2)
242+
mockCssFiles.set(normalizePath(resolve('/play/src/assets/test2.css')), mockCSSFilesContent2)
243243
const mockDescriptor = {
244244
styles: [{
245245
content: '@import "./assets/test";\n'

packages/core/runtime/pre-process-css.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
FG_IGNORE_LIST,
66
SUPPORT_FILE,
77
completeSuffix,
8-
transformSymbol,
98
} from '@unplugin-vue-cssvars/utils'
9+
import { normalizePath } from 'baiwusanyu-utils'
1010
import { parseImports, parseVBindM } from '../parser'
1111
import { transformQuotes } from '../transform/transform-quotes'
1212
import { handleAlias } from './process-css'
@@ -41,7 +41,7 @@ export function createCSSFileModuleMap(files: string[], rootDir: string, alias?:
4141
const cssFiles: ICSSFileMap = new Map()
4242
for (const file of files) {
4343
let absoluteFilePath = resolve(parse(file).dir, parse(file).base)
44-
absoluteFilePath = transformSymbol(absoluteFilePath)
44+
absoluteFilePath = normalizePath(absoluteFilePath)
4545
if (!cssFiles.get(absoluteFilePath)) {
4646
cssFiles.set(absoluteFilePath, {
4747
importer: new Set(),
@@ -56,10 +56,10 @@ export function createCSSFileModuleMap(files: string[], rootDir: string, alias?:
5656
const fileDirParse = parse(file)
5757
const fileSuffix = fileDirParse.ext
5858

59-
const code = fs.readFileSync(transformSymbol(resolve(rootDir!, file)), { encoding: 'utf-8' })
59+
const code = fs.readFileSync(normalizePath(resolve(rootDir!, file)), { encoding: 'utf-8' })
6060
const { imports } = parseImports(code, [transformQuotes])
6161

62-
const absoluteFilePath = transformSymbol(resolve(fileDirParse.dir, fileDirParse.base))
62+
const absoluteFilePath = normalizePath(resolve(fileDirParse.dir, fileDirParse.base))
6363
// scss、less、stylus 中规则:scss、less、stylus文件可以引用 css 文件、
6464
// 以及对应的scss或less文件或stylus文件,对同名文件的css文件和对应的预处理器后缀文件进行转换分析
6565
// 编译时,如果出现 scss 和 css 同名,只会处理 scss的。其次才处理 css 的

packages/core/runtime/process-css.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parse, resolve } from 'path'
2-
import { SUPPORT_FILE, completeSuffix, setTArray, transformSymbol } from '@unplugin-vue-cssvars/utils'
2+
import { SUPPORT_FILE, completeSuffix, setTArray } from '@unplugin-vue-cssvars/utils'
3+
import { normalizePath } from 'baiwusanyu-utils'
34
import { parseImports } from '../parser'
45
import type { ICSSFile, ICSSFileMap } from '../types'
56
import type { SFCDescriptor } from '@vue/compiler-sfc'
@@ -99,10 +100,10 @@ export function handleAlias(path: string, alias?: Record<string, string>, idDirP
99100
}
100101
}
101102

102-
if (importerPath) return transformSymbol(importerPath)
103+
if (importerPath) return normalizePath(importerPath)
103104
importerPath = idDirPath ? resolve(idDirPath, path) : path
104105
} else {
105106
idDirPath && (importerPath = resolve(idDirPath, path))
106107
}
107-
return transformSymbol(importerPath)
108+
return normalizePath(importerPath)
108109
}

packages/core/runtime/vite.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { SUPPORT_FILE_REG, transformSymbol } from '@unplugin-vue-cssvars/utils'
1+
import { SUPPORT_FILE_REG } from '@unplugin-vue-cssvars/utils'
2+
import { normalizePath } from 'baiwusanyu-utils'
23
import { injectCSSOnServer, injectCssOnBuild } from '../inject'
34
import { viteHMR } from '../hmr/hmr'
45
import { handleVBindVariable } from './handle-variable'
@@ -65,7 +66,7 @@ export const vitePlugin = (ctx: IVueCSSVarsCtx) => {
6566
viteHMR(
6667
ctx.CSSFileModuleMap,
6768
ctx.userOptions,
68-
transformSymbol(hmr.file),
69+
normalizePath(hmr.file),
6970
hmr.server,
7071
)
7172
}

packages/core/runtime/webpack.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { NAME, SUPPORT_FILE_REG, setTArray, transformSymbol } from '@unplugin-vue-cssvars/utils'
2-
import { log } from 'baiwusanyu-utils'
1+
import { NAME, SUPPORT_FILE_REG, setTArray } from '@unplugin-vue-cssvars/utils'
2+
import { log, normalizePath } from 'baiwusanyu-utils'
3+
34
import { webpackHMR } from '../hmr/hmr'
45
import { injectCSSOnServer } from '../inject'
56
import { handleVBindVariable } from './handle-variable'
@@ -52,7 +53,7 @@ export const webpackPlugin = (ctx: IVueCSSVarsCtx, compiler: Compiler) => {
5253
let modifiedFile = ''
5354
compiler.hooks.watchRun.tapAsync(NAME, (compilation1, watchRunCallBack) => {
5455
if (compilation1.modifiedFiles) {
55-
modifiedFile = transformSymbol(setTArray(compilation1.modifiedFiles)[0] as string)
56+
modifiedFile = normalizePath(setTArray(compilation1.modifiedFiles)[0] as string)
5657
if (SUPPORT_FILE_REG.test(modifiedFile)) {
5758
ctx.isHMR = true
5859
webpackHMR(
@@ -70,7 +71,7 @@ export const webpackPlugin = (ctx: IVueCSSVarsCtx, compiler: Compiler) => {
7071
if (ctx.isHMR) {
7172
const needRebuildModules = new Map<string, any>()
7273
for (const value of modules) {
73-
const resource = transformSymbol(value.resource)
74+
const resource = normalizePath(value.resource)
7475
if (resource.includes('?vue&type=script')) {
7576
const sfcPathKey = resource.split('?vue')[0]
7677
if (ctx.CSSFileModuleMap.get(modifiedFile).sfcPath.has(sfcPathKey))

0 commit comments

Comments
 (0)