|
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' } |
6 | 6 |
|
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 |
35 | 23 | } |
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 | + }, |
37 | 33 | } |
38 | 34 | } |
| 35 | + |
| 36 | +export default { |
| 37 | + rollup, |
| 38 | + vite: rollup as (options?: Options) => VitePlugin, |
| 39 | +} |
0 commit comments