Skip to content

Commit 98263c6

Browse files
committed
feat: add booleanProp into macros plugin
1 parent 0463655 commit 98263c6

File tree

13 files changed

+134
-46
lines changed

13 files changed

+134
-46
lines changed

.changeset/sharp-brooms-deny.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vue-macros/boolean-prop': minor
3+
'unplugin-vue-macros': minor
4+
---
5+
6+
feat: add booleanProp into macros plugin

docs/guide/configurations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All features are enabled by default except the following.
1212
- `exportProps`
1313
- `exportRender`
1414
- `setupSFC`
15+
- `booleanProp`
1516

1617
You can disable them by setting the option to `false`.
1718

packages/boolean-prop/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"@vue/compiler-core": "^3.3.4"
5050
},
5151
"devDependencies": {
52-
"@vue/compiler-sfc": "^3.3.4"
52+
"@vue/compiler-sfc": "^3.3.4",
53+
"rollup": "^3.28.1"
5354
},
5455
"engines": {
5556
"node": ">=16.14.0"

packages/boolean-prop/src/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './core'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './transformer'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
type ConstantTypes,
3+
type NodeTransform,
4+
type NodeTypes,
5+
} from '@vue/compiler-core'
6+
7+
export interface Options {
8+
// empty
9+
}
10+
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- not be used by now
12+
export function transformBooleanProp(_options: Options = {}): NodeTransform {
13+
return (node) => {
14+
if (node.type !== (1 satisfies NodeTypes.ELEMENT)) return
15+
for (const [i, prop] of node.props.entries()) {
16+
if (
17+
prop.type !== (6 satisfies NodeTypes.ATTRIBUTE) ||
18+
prop.value !== undefined
19+
)
20+
continue
21+
node.props[i] = {
22+
type: 7 satisfies NodeTypes.DIRECTIVE,
23+
name: 'bind',
24+
arg: {
25+
type: 4 satisfies NodeTypes.SIMPLE_EXPRESSION,
26+
constType: 3 satisfies ConstantTypes.CAN_STRINGIFY,
27+
content: 'checked',
28+
isStatic: true,
29+
loc: prop.loc,
30+
},
31+
exp: {
32+
type: 4 satisfies NodeTypes.SIMPLE_EXPRESSION,
33+
constType: 3 satisfies ConstantTypes.CAN_STRINGIFY,
34+
content: 'true',
35+
isStatic: false,
36+
loc: prop.loc,
37+
},
38+
loc: prop.loc,
39+
modifiers: [],
40+
}
41+
}
42+
}
43+
}

packages/boolean-prop/src/index.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
import {
2-
type ConstantTypes,
3-
type NodeTransform,
4-
type NodeTypes,
5-
} from '@vue/compiler-core'
1+
import { type Plugin } from 'rollup'
2+
import { type Plugin as VitePlugin } from 'vite'
3+
import { type VuePluginApi, getVuePluginApi } from '@vue-macros/common'
4+
import { type Options, transformBooleanProp } from './core/index'
5+
import { generatePluginName } from '#macros' assert { type: 'macro' }
66

7-
export function transformBooleanProp(): NodeTransform {
8-
return (node) => {
9-
if (node.type !== (1 satisfies NodeTypes.ELEMENT)) return
10-
for (const [i, prop] of node.props.entries()) {
11-
if (
12-
prop.type !== (6 satisfies NodeTypes.ATTRIBUTE) ||
13-
prop.value !== undefined
14-
)
15-
continue
16-
node.props[i] = {
17-
type: 7 satisfies NodeTypes.DIRECTIVE,
18-
name: 'bind',
19-
arg: {
20-
type: 4 satisfies NodeTypes.SIMPLE_EXPRESSION,
21-
constType: 3 satisfies ConstantTypes.CAN_STRINGIFY,
22-
content: 'checked',
23-
isStatic: true,
24-
loc: prop.loc,
25-
},
26-
exp: {
27-
type: 4 satisfies NodeTypes.SIMPLE_EXPRESSION,
28-
constType: 3 satisfies ConstantTypes.CAN_STRINGIFY,
29-
content: 'true',
30-
isStatic: false,
31-
loc: prop.loc,
32-
},
33-
loc: prop.loc,
34-
modifiers: [],
7+
// legacy export
8+
export * from './api'
9+
10+
const name = generatePluginName()
11+
12+
function rollup(options: Options = {}): Plugin {
13+
return {
14+
name,
15+
buildStart(rollupOpts) {
16+
let api: VuePluginApi
17+
18+
try {
19+
api = getVuePluginApi(rollupOpts)
20+
} catch (error: any) {
21+
this.warn(error)
22+
return
3523
}
36-
}
24+
25+
api.options.template ||= {}
26+
api.options.template.compilerOptions ||= {}
27+
api.options.template.compilerOptions.nodeTransforms ||= []
28+
29+
api.options.template.compilerOptions.nodeTransforms.push(
30+
transformBooleanProp(options)
31+
)
32+
},
3733
}
3834
}
35+
36+
export default {
37+
rollup,
38+
vite: rollup as (options?: Options) => VitePlugin,
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unplugin from '.'
2+
3+
export default unplugin.rollup

packages/boolean-prop/src/vite.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unplugin from '.'
2+
3+
export default unplugin.vite

packages/boolean-prop/tests/fixtures.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ import {
66
rollupBuild,
77
testFixtures,
88
} from '@vue-macros/test-utils'
9-
import { transformBooleanProp } from '../src/index'
9+
import BooleanProp from '../src/vite'
1010

1111
describe('fixtures', async () => {
1212
await testFixtures(
1313
'tests/fixtures/*.{vue,[jt]s?(x)}',
1414
(args, id) =>
1515
rollupBuild(id, [
16-
RollupVue({
17-
template: {
18-
compilerOptions: {
19-
nodeTransforms: [transformBooleanProp()],
20-
},
21-
},
22-
}),
16+
BooleanProp(),
17+
RollupVue(),
2318
RollupEsbuildPlugin({
2419
target: 'esnext',
2520
}),

0 commit comments

Comments
 (0)