11import path from 'path'
22import parse from 'eft-parser'
3+ import JSON5 from 'json5'
34import camelCase from 'camelcase'
45import { createFilter } from 'rollup-pluginutils'
56
67export default ( options = { } ) => {
7- const { include = [ '**/*.ef' , '**/*.eft' , '**/*.efml' ] , exclude } = options
8+ const { include = [ '**/*.ef' , '**/*.eft' , '**/*.efml' ] , exclude, useJSONParse } = options
89 const filter = createFilter ( include , exclude )
910
1011 return {
@@ -18,6 +19,8 @@ export default (options = {}) => {
1819 const exportLines = [ ]
1920 let componentName = camelCase ( fileName , { pascalCase : true } )
2021 let componentScope = null
22+ let componentData = null
23+ let componentMethods = null
2124
2225 const commentHandler = ( { depth, content} ) => {
2326 if ( depth > 0 ) return
@@ -45,19 +48,43 @@ export default (options = {}) => {
4548 componentName = camelCase ( splitedContent . join ( '_' ) , { pascalCase : true } )
4649 break
4750 }
51+ case 'data' : {
52+ componentData = content . match ( / { .+ } / )
53+ break
54+ }
55+ case 'methods' : {
56+ componentMethods = content . match ( / { .+ } / )
57+ break
58+ }
4859 default : {
4960 throw new TypeError ( `[EFML] Unknown directive "${ directive } "` )
5061 }
5162 }
5263
5364 }
5465
55- const ast = JSON . stringify ( JSON . stringify ( parse ( template , commentHandler ) ) )
66+ let ast = parse ( template , commentHandler )
67+
68+ if ( useJSONParse ) {
69+ ast = JSON . stringify ( JSON . stringify ( ast ) )
70+ } else {
71+ ast = JSON5 . stringify ( ast )
72+ }
5673
5774 const code = [
5875 ...importLines ,
59- componentScope && `import { create, scoped } from 'ef-core'` || `import { create } from 'ef-core'` ,
60- componentScope && `export default class ${ componentName } extends scoped(create(JSON.parse(${ ast } )), ${ componentScope } ) {}` || `export default class ${ componentName } extends create(JSON.parse(${ ast } )) {}` ,
76+ `import { create } from 'ef-core'` ,
77+ `export default class ${ componentName } extends create(${ useJSONParse && 'JSON.parse(' || '' } ${ ast } ${ useJSONParse && ')' || '' } ) {${ componentData && `
78+ static initData() {
79+ return ${ componentData }
80+ }` || '' } ${ componentMethods && `
81+ static initMethods() {
82+ return ${ componentMethods }
83+ }` || '' } ${ componentScope && `
84+ static __defaultScope() {
85+ return ${ componentScope }
86+ }` || '' }
87+ }` ,
6188 ...exportLines ,
6289 ''
6390 ] . join ( ';\n' )
0 commit comments