Skip to content

Commit

Permalink
fix: Support compileToSource to pass parameter fileContent for js fil…
Browse files Browse the repository at this point in the history
…es (#169)

* feat: support compileToSource to pass parameter fileContent for js files

* feat: support compileToSource to pass parameter fileContent for js files

* feat: support compileToSource to pass parameter fileContent for js files

* style: delete the useless file
  • Loading branch information
Nirvana-Jie authored Jul 6, 2023
1 parent f302ba2 commit 3137832
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
22 changes: 22 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ writeFileSync('./dist/render-from-js.js', project.compileToSource('./component.j
render = require('./dist/render-from-js.js')
console.log(render(data))

// compile component.js to source(case2)
// Tips: in this case, your fileContent must strictly follow the writing rules
console.log('---- .js to Source Code(case2) ---')
writeFileSync('./dist/render-from-js2.js', project.compileToSource({
filePath: '__virtual.js',
fileContent: `const { defineComponent } = require('san')
module.exports = defineComponent({
computed: {
name: function () {
const f = this.data.get('firstName')
const l = this.data.get('lastName')
return f + ' ' + l
}
},
template: '<div><h1>{{name}}</h1></div>'
})
`
}))
render = require('./dist/render-from-js2.js')
console.log(render(data))

// compile component.san.html to source
console.log('---- .san to Source Code ---')
const dom = new JSDOM(readFileSync('./component.san.html'))
Expand Down
64 changes: 43 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/models/san-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class SanProject {
!fileContent && sourceFile.refreshFromFileSystemSync()
return new TypeScriptSanParser().parse(sourceFile, formattedOptions)
}
return new JavaScriptSanParser(filePath, formattedOptions).parse()
return new JavaScriptSanParser(filePath, formattedOptions, fileContent).parse()
}

private checkAndFormatParseSanSourceFileOptions (options?: parseSanSourceFileOptions)
Expand Down

0 comments on commit 3137832

Please sign in to comment.