-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (85 loc) · 2.24 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import path from 'path'
import parse from 'eft-parser'
import JSON5 from 'json5'
import camelCase from 'camelcase'
import {createFilter} from 'rollup-pluginutils'
export default (options = {}) => {
const { include = ['**/*.ef', '**/*.eft', '**/*.efml'], exclude, useJSONParse } = options
const filter = createFilter(include, exclude)
return {
name: 'eft',
transform(template, id) {
if (!filter(id)) return
const fileName = path.parse(id).name
const importLines = []
const exportLines = []
let componentName = camelCase(fileName, {pascalCase: true})
let componentScope = null
let componentData = null
let componentMethods = 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
}
case 'data': {
componentData = content.match(/{.+}/)
break
}
case 'methods': {
componentMethods = content.match(/{.+}/)
break
}
default: {
throw new TypeError(`[EFML] Unknown directive "${directive}"`)
}
}
}
let ast = parse(template, commentHandler)
if (useJSONParse) {
ast = JSON.stringify(JSON.stringify(ast))
} else {
ast = JSON5.stringify(ast)
}
const code = [
...importLines,
`import { create } from 'ef-core'`,
`export default class ${componentName} extends create(${useJSONParse && 'JSON.parse(' || ''}${ast}${useJSONParse && ')' || ''}) {${componentData && `
static initData() {
return ${componentData}
}` || ''}${componentMethods && `
static initMethods() {
return ${componentMethods}
}` || ''}${componentScope && `
static __defaultScope() {
return ${componentScope}
}` || ''}
}`,
...exportLines,
''
].join(';\n')
return {
code,
map: { mappings: '' }
}
}
}
}