-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathbabel.plugin.js
41 lines (35 loc) · 1.08 KB
/
babel.plugin.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
41
const replaceVersionPlugin = (babel) => {
const t = babel.types
return {
name: 'replace-version',
visitor: {
Identifier(path, state) {
let replacementDescriptor =
state.opts.hasOwnProperty(path.node.name) && state.opts[path.node.name]
if (!replacementDescriptor) return
console.log('replacementDescriptor', replacementDescriptor, state.opts[path.node.name])
const type = typeof replacementDescriptor
if (type === 'string' || type === 'boolean') {
replacementDescriptor = {
type: type,
replacement: replacementDescriptor,
}
}
const replacement = replacementDescriptor.replacement
switch (replacementDescriptor.type) {
case 'boolean':
path.replaceWith(t.booleanLiteral(replacement))
break
default:
// treat as string
const str = String(replacement)
path.replaceWith(t.stringLiteral(str))
break
}
},
},
}
}
module.exports = {
replaceVersionPlugin,
}