-
Notifications
You must be signed in to change notification settings - Fork 37
/
parse.js
40 lines (38 loc) · 897 Bytes
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var babylon = require('babylon')
const acorn = require('acorn')
const escodegen = require('escodegen')
// const plugin = require('babel-plugin-import')
// console.log(babylon)
const code = `import{ Group as Groups,
Cell } from 'vux'; import { X } from 'x'
import a from './a'
import x from 'vux/src/components/cell'
var b = '1'`
const rs = babylon.parse(code, {
sourceType: 'module'
})
// console.log(rs)
const format = function (obj) {
return JSON.stringify(obj, null, 2)
}
const tree = acorn.parse(code, {
sourceType: 'module'
})
console.log(format(tree.body))
const append = acorn.parse(`import{ a, xb,
c,de} from 'xxx'`, {
sourceType: 'module'
})
// 重新生成
tree.body.unshift(append)
const code2 = escodegen.generate(tree, {
format: {
semicolons: false,
indent: {
style: ' ',
base: 0,
adjustMultilineComment: false
}
}
})
console.log(code2)