-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (53 loc) · 1.54 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const path = require('path')
const parse = require('eft-parser')
module.exports = async function(template) {
if (this.cacheable) this.cacheable()
const camelCase = (await import('camelcase')).default
const filePath = this.resourcePath
const fileName = path.parse(filePath).name
const importLines = []
const exportLines = []
let componentName = camelCase(fileName, {pascalCase: true})
let componentScope = null
const commentHandler = ({depth, content}) => {
if (depth > 0) return
if (content[0] !== '!') return
content = content.slice(1).trim()
const splitedContent = content.split(/\s+/)
const directive = splitedContent.shift()
switch (directive) {
case 'import': {
importLines.push(content)
break
}
case 'export': {
exportLines.push(content)
break
}
case 'scope': {
componentScope = content.match(/{.+}/)
break
}
case 'name': {
componentName = camelCase(splitedContent.join('_'), {pascalCase: true})
break
}
default: {
throw new TypeError(`[EFML] Unknown directive "${directive}"`)
}
}
}
const ast = JSON.stringify(JSON.stringify(parse(template, commentHandler)))
const code = [
...importLines,
componentScope &&
`import { create, scoped } from 'ef-core'` ||
`import { create } from 'ef-core'`,
componentScope &&
`export default class ${componentName} extends scoped(create(JSON.parse(${ast})), ${componentScope}) {}` ||
`export default class ${componentName} extends create(JSON.parse(${ast})) {}`,
...exportLines,
''
]
return code.join(';\n')
}