@@ -51,9 +51,9 @@ Code to modify and append `b` to `foo` prop of defaultExport:
51
51
``` js
52
52
import { loadFile , writeFile } from " magicast" ;
53
53
54
- const _module = await loadFile (" config.js" );
54
+ const mod = await loadFile (" config.js" );
55
55
56
- _module .exports .default . props .foo .push (" b" );
56
+ mod .exports .default .foo .push (" b" );
57
57
58
58
await writeFile (_module);
59
59
```
@@ -72,15 +72,52 @@ export default {
72
72
import { parseCode , generateCode } from " magicast" ;
73
73
74
74
// Parse to AST
75
- const _module = parseCode (` export default { foo: ['a'] }` );
75
+ const mod = parseCode (` export default { }` );
76
76
77
+ // Ensure foo is an array
78
+ mod .exports .default .foo || = [];
77
79
// Add a new array member
78
- _module .exports .default .props .foo .push (" b" );
80
+ mod .exports .default .foo .push (" b" );
81
+ mod .exports .default .foo .unshift (" a" );
79
82
80
83
// Generate code
81
84
const { code , map } = generateCode (_module);
82
85
```
83
86
87
+ Generated code:
88
+
89
+ ``` js
90
+ export default {
91
+ foo: [" a" , " b" ],
92
+ };
93
+ ```
94
+
95
+ ** Example:** Get the AST directly:
96
+
97
+ ``` js
98
+ import { parseCode , generateCode } from " magicast" ;
99
+
100
+ const mod = parseCode (` export default { }` );
101
+
102
+ const ast = mod .exports .default .$ast
103
+ // do something with ast
104
+ ```
105
+
106
+ ** Example:** Function parameters:
107
+
108
+ ``` js
109
+ import { parseCode , generateCode } from " magicast" ;
110
+
111
+ const mod = parseCode (` export default defineConfig({ foo: 'bar' })` );
112
+
113
+ // Support for both bare object export and `defineConfig` wrapper
114
+ const options = mod .exports .default .$type === ' function-call'
115
+ ? mod .exports .default .arguments [0 ]
116
+ : mod .exports .default ;
117
+
118
+ console .log (options .foo ) // bar
119
+ ```
120
+
84
121
## Development
85
122
86
123
- Clone this repository
0 commit comments