Giter VIP home page Giter VIP logo

express-tsx-views's Introduction

Server-side JSX/TSX rendering for your express or NestJS application

npm version Test Coverage Build Status

Description

With this template engine, TSX files can be rendered server-side by your Express application. Unlike other JSX express renderers, this one does not rely on JSX files being transpiled by babel at runtime. Instead, TSX files are processed once by the tsc compiler.

For this to work, the templates are imported dynamically during rendering. And for this you have to provide a default export in your main TSX files. (Embeddable TSX components don't have to use a default export).

Highlights

  • Fast, since the JSX/TSX files do not have to be transpiled on-the-fly with every request
  • Works with compiled files (.js / node) and uncompiled files (.tsx / ts-node, ts-jest, ...)

Table of contents

Usage

$ npm install --save express-tsx-views

You have to set the jsx setting in your TypeScript configuration tsconfig.json to the value react and to enable esModuleInterop:

{
  "compilerOptions": {
    "jsx": "react",
    "esModuleInterop":  true,
  }
}

This template engine can be used in express and NestJS applications. The function setupReactViews() is provided, with which the engine is made available to the application.

import { setupReactViews } from 'express-tsx-views'

const options = {
  viewsDirectory: path.resolve(__dirname, '../views'),
}

setupReactViews(app, options)

The following options may be passed:

Option Type Description Default
viewsDirectory string The directory where your views (.tsx files) are stored. Must be specified. -
doctype string Doctype to be used. <!DOCTYPE html>\n
prettify boolean If activated, the generated HTML string is formatted using prettier. false
transform (html: string) => string With this optional function the rendered HTML document can be modified. For this purpose a function must be defined which gets the HTML string as argument. The function returns a modified version of the HTML string as string. -

Express

Example express app (See also example/app.ts in this project):

import express from 'express'
import { resolve } from 'path'
import { setupReactViews } from 'express-tsx-views'
import { Props } from './views/my-view'

export const app = express()

setupReactViews(app, {
  viewsDirectory: resolve(__dirname, 'views'),
  prettify: true,  // Prettify HTML output
})

app.get('/my-route', (req, res, next) => {
  const data: Props = { title: 'Test', lang: 'de' }
  res.render('my-view', data)
})

app.listen(8080)

views/my-view.tsx:

import React, { Component } from 'react'
import MyComponent from './my-component'
import { MyLayout } from './my-layout'

export interface Props {
  title: string
  lang: string
}

// Important -- use the `default` export
export default class MyView extends Component<Props> {
  render() {
    return (
      <div>Hello from React! Title: {this.props.title}</div>
    )
  }
}

NestJS

express-tsx-views can also be used in NestJS. For this purpose the template engine must be made available in your main.ts:

import { setupReactViews } from 'express-tsx-views'

async function bootstrap() {
  // ...
  setupReactViews(app.getHttpAdapter().getInstance(), {
    viewsDirectory: resolve(__dirname, '../views'),
  })
}
// ...

Example controller:

import { Props } from './views/my-view'

@Get('/my-route')
@Render('my-view')
getMyRoute(): Props {
  return { title: "Hello from NestJS", lang: "de" }
}

License

express-tsx-views is distributed under the MIT license. See LICENSE for details.

express-tsx-views's People

Contributors

pmb0 avatar renovate-bot avatar semantic-release-bot avatar thachp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.