All Projects → Saviio → ts-sfc-plugin

Saviio / ts-sfc-plugin

Licence: MIT license
A plugin for optimizing stateless component of React (tsx)

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ts-sfc-plugin

Xbanner
🔥【图片轮播】支持图片无限轮播,支持AndroidX、自定义指示点、显示提示文字、切换动画、自定义布局,一屏显示多个等功能
Stars: ✭ 1,734 (+5879.31%)
Mutual labels:  transform
Transformation Matrix
Javascript isomorphic 2D affine transformations written in ES6 syntax. Manipulate transformation matrices with this totally tested library!
Stars: ✭ 184 (+534.48%)
Mutual labels:  transform
ios-scriptable-tsx
在 vscode 上使用 typescript 和 jsx 开发 ios 小组件的小框架.基于 Scriptable app.
Stars: ✭ 113 (+289.66%)
Mutual labels:  tsx
Jstransformer
Normalize the API of any JSTransformer.
Stars: ✭ 139 (+379.31%)
Mutual labels:  transform
Ugm
Ubpa Graphics Mathematics
Stars: ✭ 178 (+513.79%)
Mutual labels:  transform
Pixelsdk
The modern photo and video editor for your iPhone / iPad app. A fully customizable image & video editing iOS Swift framework.
Stars: ✭ 192 (+562.07%)
Mutual labels:  transform
Misp Maltego
Set of Maltego transforms to inferface with a MISP Threat Sharing instance, and also to explore the whole MITRE ATT&CK dataset.
Stars: ✭ 112 (+286.21%)
Mutual labels:  transform
MSX devs
MSX develops repository
Stars: ✭ 21 (-27.59%)
Mutual labels:  tsx
Trans
Object transformer
Stars: ✭ 179 (+517.24%)
Mutual labels:  transform
Aws Etl Orchestrator
A serverless architecture for orchestrating ETL jobs in arbitrarily-complex workflows using AWS Step Functions and AWS Lambda.
Stars: ✭ 245 (+744.83%)
Mutual labels:  transform
Omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
Stars: ✭ 148 (+410.34%)
Mutual labels:  transform
Typescript Transform Paths
Transforms absolute imports to relative
Stars: ✭ 166 (+472.41%)
Mutual labels:  transform
Transferee
一个帮助您完成从缩略视图到原视图无缝过渡转变的神奇框架
Stars: ✭ 2,697 (+9200%)
Mutual labels:  transform
Etl.net
Mass processing data with a complete ETL for .net developers
Stars: ✭ 129 (+344.83%)
Mutual labels:  transform
android-gradle-plugin-transform-patch
android gradle plugin transform patch
Stars: ✭ 28 (-3.45%)
Mutual labels:  transform
Css Transform
CSS Transform Playground. Online tool to visualize 2D & 3D CSS3 Transform functions.
Stars: ✭ 113 (+289.66%)
Mutual labels:  transform
Redux Persist Transform Filter
Filter transformator for redux-persist
Stars: ✭ 184 (+534.48%)
Mutual labels:  transform
vuetify-tsx
Vuetify TSX is just a wrapper lib around vuetify components.
Stars: ✭ 20 (-31.03%)
Mutual labels:  tsx
react-mops
🐶 Modify Orientation Position Size
Stars: ✭ 40 (+37.93%)
Mutual labels:  transform
Slow Cheetah
XML Transforms for app.config and other XML files
Stars: ✭ 243 (+737.93%)
Mutual labels:  transform

Codacy grade npm Coveralls github branch CircleCI branch GitHub license

ts-sfc-plugin

A plugin for optimizing stateless component of React (tsx)

Important

After react 16.8, react team announced new feature [Hooks], this plugin cannot work with it as its special runtime mechanism.

Why

React functional component(SFC) is easy to use and help to reduce code size significantly, but sometimes people might have been some misunderstanding about its perfomance. Usually, we think functional components would avoid some overheads like mounting / unmounting / lifecycle checking and memory allocations, but in fact, there're no special optimizations currently (but after react 16 was released, sfc is indeed faster than before).

Fortunately SFC just function in react world, if we do care about performance in production there're still a way to improve and this plugin here come to simplify these situation.

const code1 = (
  <div>
    <Avatar />
  </div>
)

const code2 = (
  <div>
    { Avatar() }
  </div>
)

As we cannot recognize if the component is functional, we have to use an anotation to tag the expression:

<Avatar sfc />
// Plugin use `sfc` as identifier by default, but you can pass an option to override it.

How to use

webpack

  module: {
    rules: [
      {
        test: /\.(jsx|tsx|js|ts)$/,
        loader: 'ts-loader',
        options: {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [sfcPlugin()],
          }),
          compilerOptions: {
            module: 'esnext',
          },
        },
        exclude: /node_modules/,
      }
    ],
  }

code

import React from 'react'

export const Avatar = ({ name }) => {
  return (
    <div>
      <img src=... />
      <span>{ name }</span>
    </div>
  )
}
import React from 'react'
import { Avatar } from './avatar.component'

export class App extends React.PureComponent {
  render() {
    return (
      <div>
        <Avatar name={ 'hello world' } sfc />
      </div>
    )
  }
}

option

sfcPlugin(option?: Option)

interface Option {
  pragma?: string
  mode?: 1 | 2
}

Deopt

Reason Deopt (mode 1) Deopt (mode 2)
spread operator true false
prop: key true false

Considering we transform the code from tsx to native function-call which means the additional vd-layer will be eliminatd, and the effects of key will be removed as well.

// before

const Message = () => <div>bravo</div>

export class App extends React.PureComponent {
  render() {
    return <Message key={ 1 } />
  }
}
// after

const Message = () => <div>bravo</div>

export class App extends React.PureComponent {
  render() {
    // won't get benefit from prop: `key`
    return Message()
  }
}

Notice

Unlike @babel/plugin-transform-react-inline-elements, we won't take ref into account because of this plugin will be applied to typescript only.

const Message = () => <div>bravo</div>

export class App extends React.PureComponent {
  render() {
    // ERROR: this is not type-safe
    // { ref: any } is not assignable to IntrinsicAttributes
    return <Message ref={ ... } />
  }
}

Defect

The following code is recommanded:

<Avatar sfc>
// enable rule: `jsx-boolean-value` in tslint.json

using declaration merging in global .d.ts

import React from 'react'

declare module 'react' {
  namespace JSX {
    interface IntrinsicAttributes extends React.Attributes {
      sfc?: boolean
    }
  }
}

Exception

code like the usage will not work, because the plugin does not include any runtime type checking.

const component = <Avatar sfc={ this.props.flag } />

Benchmark

React 16.4, <Dot />, 50 times, MacBook Pro (Retina, 13-inch, Early 2013)

Classical Functional Direct-call Auto-transform
660ms 408ms 226ms 229ms

Refs

scu vs sfc

45% faster react functional components now

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].