Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-nornj-in-jsx

joe-sky46MIT5.3.5TypeScript support: included

Make the NornJ work gracefully in the JSX environment

react, jsx, nornj, if, else, for, each, loop, switch, babel

readme

Babel-Plugin-NornJ-in-jsx

一个可支持自由扩展的JSX语法增强插件 :wink:

NPM Version Coverage Status NPM Downloads

Babel-Plugin-NornJ-in-jsx是一个能为 JSX 带来更多丰富语法的 Babel 插件,比如条件及循环语句:

const Button = () => {
  return (
    <div>
      <for i={0} to={10}>
        <if condition={i < 5}>
          <i>less than 5</i>
          <else>
            <i>greater than 5</i>
          </else>
        </if>
      </for>
    </div>
  );
};

以及类似 Vue、Angular 的指令:

const Button = props => {
  return (
    <div>
      <input type="button" n-show={props.showBtn} />
    </div>
  );
};

还有过滤器:

const Button = props => {
  return <div>{n`${props.name} | upperFirst`}</div>;
};

更重要的是,除了以上这些预置的语法,还支持像 Vue、Angular 那样自由扩展新的语法:

import nj from 'nornj';
import cn from 'classnames';
nj.registerExtension('class', opts => cn(opts.value()));

const Button = props => {
  return (
    <div>
      <input
        type="button"
        n-class={{
          className1: true,
          className2: props.hasClassName2
        }}
      />
    </div>
  );
};

为什么创建这个插件?

React项目开发中,原生的JSX语法并没有提供类似Vuev-ifv-forv-show等模板语法糖。当然,社区为JSX贡献了不少相关的插件,比如jsx-control-statements

Babel-Plugin-NornJ-in-jsx的设计灵感就来源于上述的jsx-control-statements。只不过,它比前者的功能要更加丰富得多;且可以支持扩展 :wink:。

License

MIT

changelog

CHANGELOG

[v5.3.2] 2021.02.07

  • 🌟 Add onlyLowercaseExName option for nornj-in-jsx.

[v5.2.8] 2020.09.16

  • 🌟 ES module adaptation.

[v5.2.0] 2020.07.08

  • 🌟 mobxFormData tag official released.

[v5.1.0] 2020.01.10

  • 🌟 Support new ways to compile n-bind directive.

[v5.0.3] 2019.12.07

  • :bug: Fix the babel config sometimes fail to read.

[v5.0.0] 2019.12.03

  • 🚩 Support complete typescript type definition.

[v5.0.0-rc.35] 2019.09.14

  • 🌟 Optimizing auto import the NornJ package.

[v5.0.0-rc.32] 2019.08.22

  • 🌟 Support syntax such as:
const foo = 100,
  bar = 100;

const tag = <input n-show={`foo === bar`} />;

[v5.0.0-rc.12] 2019.05.07

  • 🐞 Fixed error on auto add key for children elements with spread attributes.

[v5.0.0-rc.7] 2019.04.14

  • 🌟 Support tagged template tag html.

[v0.4.15] 2018.12.29

  • 🌟 支持在react-native中自动引入nornj-react/mobx/native包。

[v0.4.14] 2018.12.14

  • 🐞 修复指令无法在react-native中使用的问题。

[v0.4.13] 2018.12.14

  • 🌟 为适配react-native增加rn参数。

[v0.4.12] 2018.12.12

  • 🌟 NornJ的所有Tagged template literal功能均支持模板预编译。
  • 🌟 增加imports参数,用于修复babel-plugin-import在某些使用场景的局限。

[v0.4.10] 2018.10.17

  • 🌟 支持withforfn标签。
  • 🌟 支持可自由扩展新的标签。
  • 🐞 修复babel 7中使用指令的 bug。

[v0.4.8] 2018.09.21

  • 🐞 修复babel 7each标签的 bug。

[v0.4.7] 2018.09.05

  • 🌟 表达式支持预编译模板。

[v0.4.6] 2018.08.22

  • 🐞 修复webpack 4中构建时nj变量重复定义的 bug。

[v0.4.5] 2018.08.10

  • 🌟 增加n-mobx-modeln-mst-model指令。

[v0.4.4] 2018.08.02

  • 🐞 修复与styled-jsx共存时的 bug。

[v0.4.3] 2018.08.01

  • 🌟 增加n-shown-style指令。
  • 🌟 支持预编译模板,可使用体积最小的runtime版本NornJ包。

[v0.4.2] 2018.05.24

  • 🌟 支持ifeachswitch标签。