Skip to content

Commit b99ef1c

Browse files
authored
feat: automatically identify server in vite (#28)
1 parent 8758aba commit b99ef1c

File tree

8 files changed

+30
-18
lines changed

8 files changed

+30
-18
lines changed

README.ZH-CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ export interface Options {
159159

160160
/**
161161
* 标记是否为开发服务器使用
162-
* 因为 unplugin-vue-cssvars uses 在开发服务器上和打包中使用了不同策略
162+
* 因为 unplugin-vue-cssvars 在开发服务器上和打包中使用了不同策略,
163+
* vite 中如果不传递它,unplugin-vue-cssvars 将自动识别 config 的 command 来决定 server 值
163164
* @default true
164165
*/
165166
server?: boolean

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export interface Options {
162162
/**
163163
* Flag whether to start with server at development time,
164164
* because unplugin-vue-cssvars uses different strategies for building and server development
165+
* If it is not passed in vite, unplugin-vue-cssvars will automatically
166+
* recognize the command of config to determine the server value
165167
* @default true
166168
*/
167169
server?: boolean

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@
6363
"test:coverage": "vitest --coverage"
6464
},
6565
"peerDependencies": {
66-
"chalk": "^5.2.0",
66+
"chalk": "^4.1.2",
6767
"estree-walker": "^3.0.3",
6868
"fast-glob": "^3.2.12",
6969
"fs-extra": "^10.0.1",
7070
"hash-sum": "^2.0.0",
7171
"magic-string": "^0.27.0",
72-
"unplugin": "^0.9.6",
72+
"unplugin": "^1.3.1",
7373
"vue": "^3.2.47"
7474
},
7575
"dependencies": {
76-
"chalk": "^5.2.0",
76+
"chalk": "^4.1.2",
7777
"estree-walker": "^3.0.3",
7878
"fast-glob": "^3.2.12",
7979
"fs-extra": "^10.0.1",
8080
"hash-sum": "^2.0.0",
8181
"magic-string": "^0.27.0",
82-
"unplugin": "^0.9.6",
82+
"unplugin": "^1.3.1",
8383
"vue": "^3.2.47"
8484
},
8585
"devDependencies": {

packages/core/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
injectCssOnBuild,
1212
injectCssOnServer,
1313
} from './inject'
14+
import type { ResolvedConfig } from 'vite'
1415
import type { TMatchVariable } from './parser'
1516
import type { Options } from './types'
1617

@@ -25,6 +26,7 @@ const unplugin = createUnplugin<Options>(
2526
const CSSFileModuleMap = preProcessCSS(userOptions)
2627
const vbindVariableList = new Map<string, TMatchVariable>()
2728
let isScriptSetup = false
29+
let isServer = false
2830
return [
2931
{
3032
name: NAME,
@@ -41,18 +43,27 @@ const unplugin = createUnplugin<Options>(
4143
const {
4244
vbindVariableListByPath,
4345
injectCSSContent,
44-
} = getVBindVariableListByPath(descriptor, id, CSSFileModuleMap, !!userOptions.server)
46+
} = getVBindVariableListByPath(descriptor, id, CSSFileModuleMap, isServer)
4547
const variableName = getVariable(descriptor)
4648
vbindVariableList.set(id, matchVariable(vbindVariableListByPath, variableName))
4749

48-
if (!userOptions.server)
50+
if (!isServer)
4951
code = injectCssOnBuild(code, injectCSSContent, descriptor)
5052
}
5153
return code
5254
} catch (err: unknown) {
5355
this.error(`${NAME} ${err}`)
5456
}
5557
},
58+
vite: {
59+
// Vite plugin
60+
configResolved(config: ResolvedConfig) {
61+
if (userOptions.server !== undefined)
62+
isServer = userOptions.server
63+
else
64+
isServer = config.command === 'serve'
65+
},
66+
},
5667
},
5768
{
5869
name: `${NAME}:inject`,
@@ -61,7 +72,7 @@ const unplugin = createUnplugin<Options>(
6172
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
6273
try {
6374
// transform in dev
64-
if (userOptions.server) {
75+
if (isServer) {
6576
if (id.endsWith('.vue')) {
6677
const injectRes = injectCSSVars(code, vbindVariableList.get(id), isScriptSetup)
6778
code = injectRes.code

packages/core/option/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const defaultOption: Options = {
1313
exclude: DEFAULT_EXCLUDE_REG,
1414
revoke: true,
1515
includeCompile: SUPPORT_FILE_LIST,
16-
server: true,
1716
}
1817

1918
export function initOption(option: Options) {

play/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "module",
55
"version": "0.0.0",
66
"scripts": {
7-
"dev": "vite --host",
7+
"dev": "vite serve --host",
88
"build": "vite build",
99
"preview": "vite preview --host"
1010
},

play/vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export default defineConfig({
2929
viteVueCSSVars({
3030
include: [/.vue/],
3131
includeCompile: ['**/**.scss'],
32-
server: false,
3332
}),
3433
],
3534
})

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)