Skip to content

Commit 62285d6

Browse files
authored
fix: delete excess injection code (#18)
1 parent a40f72e commit 62285d6

File tree

9 files changed

+94
-38
lines changed

9 files changed

+94
-38
lines changed

README.ZH-CN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# unplugin-vue-cssvars
2-
🌀 一个 vue 的插件能够能让你在 css 文件中使用 CSSVars 特性
2+
🌀 一个 vue3 的插件能够能让你在 css 文件中使用 CSSVars 特性
33

44
[English](https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.md) | 中文
55

@@ -146,6 +146,8 @@ export interface Options {
146146
}
147147
```
148148
### 关于 revoke 详细说明
149+
> 💡 正式版本以解决重复代码问题,正式版本不再需要设置
150+
149151
有如下两个文件 `App.vue``test.css`
150152
````
151153
<script setup lang="ts">
@@ -261,4 +263,4 @@ div[data-v-1dfefb04] {
261263
## Thanks
262264
* [vue](https://github.com/vuejs/core)
263265
* [vite](https://github.com/vitejs/vite)
264-
* [unplugin](https://github.com/unjs/unplugin)
266+
* [unplugin](https://github.com/unjs/unplugin)

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# unplugin-vue-cssvars
2-
🌀 A vue plugin that allows you to use vue's CSSVars feature in css files
2+
🌀 A vue plugin that allows you to use vue3's CSSVars feature in css files
33

44
English | [中文](https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.ZH-CN.md)
55

@@ -146,6 +146,8 @@ export interface Options {
146146
}
147147
```
148148
### Details about revoke
149+
> 💡 v1.0.0 version to solve the problem of duplicate code, no longer need to set
150+
149151
Suppose there are two files `App.vue` and `test.css`
150152
````
151153
<script setup lang="ts">
@@ -266,4 +268,4 @@ if there is a variable conflict, `script setup` will take precedence
266268
## Thanks
267269
* [vue](https://github.com/vuejs/core)
268270
* [vite](https://github.com/vitejs/vite)
269-
* [unplugin](https://github.com/unjs/unplugin)
271+
* [unplugin](https://github.com/unjs/unplugin)

packages/core/index.ts

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createCSSModule } from './runtime/process-css'
77
import { initOption } from './option'
88
import { getVariable } from './parser'
99
import { injectCSSVars } from './inject/inject-cssvars'
10-
import { revokeCSSVars } from './inject/revoke-cssvars'
10+
import { removeInjectImporter, revokeCSSVars } from './inject/revoke-cssvars'
1111
import type { IBundle, Options } from './types'
1212

1313
import type { OutputOptions } from 'rollup'
@@ -21,37 +21,54 @@ const unplugin = createUnplugin<Options>(
2121
)
2222
// 预处理 css 文件
2323
const preProcessCSSRes = preProcessCSS(userOptions)
24-
return [{
25-
name: NAME,
26-
enforce: 'pre',
24+
return [
25+
{
26+
name: NAME,
27+
enforce: 'pre',
2728

28-
transformInclude(id: string) {
29-
return filter(id)
30-
},
29+
transformInclude(id: string) {
30+
return filter(id)
31+
},
3132

32-
async transform(code: string, id: string) {
33-
try {
33+
async transform(code: string, id: string) {
34+
try {
3435
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
35-
if (id.endsWith('.vue')) {
36-
const { descriptor } = parse(code)
37-
const importCSSModule = createCSSModule(descriptor, id, preProcessCSSRes)
38-
const variableName = getVariable(descriptor)
39-
code = injectCSSVars(code, importCSSModule, variableName)
40-
console.log(code)
36+
if (id.endsWith('.vue')) {
37+
const { descriptor } = parse(code)
38+
const importCSSModule = createCSSModule(descriptor, id, preProcessCSSRes)
39+
const variableName = getVariable(descriptor)
40+
code = injectCSSVars(code, importCSSModule, variableName)
41+
}
42+
return code
43+
} catch (err: unknown) {
44+
this.error(`${name} ${err}`)
4145
}
42-
return code
43-
} catch (err: unknown) {
44-
this.error(`${name} ${err}`)
45-
}
46+
},
4647
},
47-
},
48-
{
49-
name: `${NAME}:revoke-inject`,
50-
async writeBundle(options: OutputOptions, bundle: IBundle) {
51-
if (userOptions.revoke)
52-
await revokeCSSVars(options, bundle)
48+
{
49+
name: `${NAME}:revoke-inject`,
50+
enforce: 'post',
51+
transformInclude(id: string) {
52+
return filter(id)
53+
},
54+
55+
async transform(code: string, id: string) {
56+
try {
57+
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
58+
if (id.endsWith('.vue'))
59+
code = removeInjectImporter(code)
60+
61+
return code
62+
} catch (err: unknown) {
63+
this.error(`${name} ${err}`)
64+
}
65+
},
66+
async writeBundle(options: OutputOptions, bundle: IBundle) {
67+
if (userOptions.revoke)
68+
await revokeCSSVars(options, bundle)
69+
},
5370
},
54-
}]
71+
]
5572
})
5673

5774
export const viteVueCSSVars = unplugin.vite

packages/core/inject/__test__/__snapshots__/inject-cssvars.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
exports[`inject cssvars > injectCSSVars: basic 1`] = `
44
"<style>.foo{ color: red }</style>
5-
<style scoped>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
5+
<style scoped unplugin-vue-cssvars>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
66
</style>"
77
`;
88
99
exports[`inject cssvars > injectCSSVars: multiple value 1`] = `
1010
"<style>.foo{ color: red }</style>
11-
<style scoped>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.bar{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
11+
<style scoped unplugin-vue-cssvars>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.bar{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
1212
</style>"
1313
`;
1414

packages/core/inject/__test__/inject-cssvars.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('inject cssvars', () => {
1717
},
1818
},
1919
]
20-
expectContent = `${mockCode}\n<style scoped>${[...mockCSSModule[0].vBindCode.color]}\n</style>`
20+
expectContent = `${mockCode}\n<style scoped unplugin-vue-cssvars>${[...mockCSSModule[0].vBindCode.color]}\n</style>`
2121
})
2222

2323
test('injectCSSVars: basic', () => {
@@ -41,7 +41,7 @@ describe('inject cssvars', () => {
4141

4242
test('injectCSSVars: multiple value', () => {
4343
vBindValue.add('\\n/* created by @unplugin-vue-cssvars */\\n/* <inject start> */\\n.bar{color:v-bind(color)}\\n/* <inject end> */\\n')
44-
expectContent = `${mockCode}\n<style scoped>${[...mockCSSModule[0].vBindCode.color].join('')}\n</style>`
44+
expectContent = `${mockCode}\n<style scoped unplugin-vue-cssvars>${[...mockCSSModule[0].vBindCode.color].join('')}\n</style>`
4545
const res = injectCSSVars(mockCode, mockCSSModule as any, mockVariableName as any)
4646
expect(res).toBe(expectContent)
4747
expect(res).matchSnapshot()

packages/core/inject/__test__/revoke-cssvars.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { cwd } from 'node:process'
33
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
44
import { pathExists, readFile, remove } from 'fs-extra'
55
import MagicString from 'magic-string'
6-
import { deleteInjectCSS, findInjects, revokeCSSVars } from '../revoke-cssvars'
6+
import {
7+
deleteInjectCSS,
8+
findInjects,
9+
removeInjectImporter,
10+
revokeCSSVars,
11+
} from '../revoke-cssvars'
712
describe('revoke css', () => {
813
let mockText = ''
914
const testFileDir = `${cwd()}/mock.css`
@@ -164,4 +169,32 @@ describe('revoke css', () => {
164169
const bufferRes2 = await readFile(testFileDir2)
165170
expect(bufferRes2.toString()).toBe(deleteRes)
166171
})
172+
173+
test('removeInjectImporter should remove lines matching unplugin-vue-cssvars=true', () => {
174+
const input = `
175+
import foo from 'bar';
176+
// unplugin-vue-cssvars=true
177+
import baz from 'qux';
178+
const code = 'hello world';
179+
`
180+
const expectedOutput = `
181+
import foo from 'bar';
182+
183+
import baz from 'qux';
184+
const code = 'hello world';
185+
`
186+
expect(removeInjectImporter(input)).toEqual(expectedOutput)
187+
})
188+
189+
test('removeInjectImporter should return original code if no lines match unplugin-vue-cssvars=true', () => {
190+
const input = `
191+
import foo from 'bar';
192+
const code = 'hello world';
193+
`
194+
expect(removeInjectImporter(input)).toEqual(input)
195+
})
196+
197+
test('removeInjectImporter should handle empty code string', () => {
198+
expect(removeInjectImporter('')).toEqual('')
199+
})
167200
})

packages/core/inject/inject-cssvars.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const injectCSSVars = (
1717
}
1818
})
1919
if (injectCSSSet.length > 0)
20-
code = `${code}\n<style scoped>${injectCSSSet.join('')}\n</style>`
20+
code = `${code}\n<style scoped unplugin-vue-cssvars>${injectCSSSet.join('')}\n</style>`
2121

2222
return code
2323
}

packages/core/inject/revoke-cssvars.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ export function deleteInjectCSS(injectList: InjectStr, mgc: MagicString) {
5252
})
5353
return mgc.toString()
5454
}
55+
56+
export const removeInjectImporter = (code: string) => code.replace(/^.*unplugin-vue-cssvars=true.*$/gm, '')

play/src/App.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const appAsd = () => 'red'
66
const fooColor = appAsd()
77
const appTheme2 = 'blue'
88
const lessColor = 'greenyellow'
9-
const sassColor = '#94c9ff'
9+
const sassColor = ref('#94c9ff')
1010
const stylColor = '#fd1d7c'
1111
const appTheme3 = ref('red')
1212
const appTheme4 = reactive({ color: 'red' })
@@ -64,7 +64,7 @@ export default {
6464
</script> -->
6565

6666
<template>
67-
<div class="sass">
67+
<div class="sass" @click="sassColor = 'red'">
6868
TEST
6969
<!-- <Comp /> -->
7070
</div>

0 commit comments

Comments
 (0)