Skip to content

Commit

Permalink
feat: noTemplateOutput, see #34
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 25, 2019
1 parent 8d986cd commit 6038a0a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/target-js/compilers/element-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { ExprType } from 'san'
*/
export class ElementCompiler {
private compileAnode
private noTemplateOutput: boolean

constructor (compileAnode) {
constructor (compileAnode, noTemplateOutput) {
this.compileAnode = compileAnode
this.noTemplateOutput = noTemplateOutput
}

/**
Expand All @@ -28,6 +30,8 @@ export class ElementCompiler {

if (tagName) {
sourceBuffer.joinString('<' + tagName)
} else if (this.noTemplateOutput) {
return
} else if (tagNameVariable) {
sourceBuffer.joinString('<')
sourceBuffer.joinRaw(tagNameVariable + ' || "div"')
Expand Down Expand Up @@ -213,6 +217,8 @@ export class ElementCompiler {
if (tagName === 'option') {
sourceBuffer.addRaw('$optionValue = null;')
}
} else if (this.noTemplateOutput) {
// noop
} else {
sourceBuffer.joinString('</')
sourceBuffer.joinRaw(tagNameVariable + ' || "div"')
Expand Down
5 changes: 3 additions & 2 deletions src/target-js/compilers/renderer-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export class RendererCompiler<T> {
private component: SanComponent
private funcName: string

constructor (ComponentClass: typeof SanComponent) {
constructor (ComponentClass: typeof SanComponent, noTemplateOutput) {
this.elementSourceCompiler = new ElementCompiler(
(...args) => this.aNodeCompiler.compile(...args)
(...args) => this.aNodeCompiler.compile(...args),
noTemplateOutput
)
this.funcName = 'sanssrRenderer' + ComponentClass.sanssrCid
this.component = new ComponentClass()
Expand Down
6 changes: 4 additions & 2 deletions src/target-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export default class ToJSCompiler implements Compiler {
this.tsConfigFilePath = require(tsConfigFilePath)
}

public compile (sanApp: SanApp) {
public compile (sanApp: SanApp, {
noTemplateOutput = false
}) {
const emitter = new JSEmitter()
emitter.write('module.exports = ')
emitter.writeAnonymousFunction(['data', 'noDataOutput'], () => {
emitRuntime(emitter)
for (const componentClass of sanApp.componentClasses) {
const renderCompiler = new RendererCompiler(componentClass)
const renderCompiler = new RendererCompiler(componentClass, noTemplateOutput)
emitter.writeLines(renderCompiler.compileComponentSource())
}
const funcName = 'sanssrRenderer' + sanApp.getEntryComponentClassOrThrow().sanssrCid
Expand Down
5 changes: 4 additions & 1 deletion src/target-js/utils/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export function compile (caseName) {
debug('compile', caseName)
const tsFile = join(caseRoot, caseName, 'component.ts')
const jsFile = resolve(caseRoot, caseName, 'component.js')
const noTemplateOutput = caseName.indexOf('notpl') > -1
const targetCode = sanProject.compile(
existsSync(jsFile) ? jsFile : tsFile,
ToJSCompiler
ToJSCompiler, {
noTemplateOutput
}
)

writeFileSync(join(caseRoot, caseName, 'ssr.js'), targetCode)
Expand Down
5 changes: 5 additions & 0 deletions test/cases/plain-span-notpl/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Component } from 'san'

export default class MyComponent extends Component {
static template = '<template><span>should be top</span>this too</template>'
}
1 change: 1 addition & 0 deletions test/cases/plain-span-notpl/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/cases/plain-span-notpl/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--s-data:{}--><span>should be top</span>this too

0 comments on commit 6038a0a

Please sign in to comment.