npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-vue-tsx

v3510.0.3

Published

Babel plugin for Vue 2.0 JSX

Downloads

13,260

Readme

babel-plugin-transform-vue-tsx Build Status

Note

此插件基于babel-plugin-transform-vue-jsx修改,解决 TSX 语法严格类型下的一些问题:

对于 tsx, 标签的类型是限定的,为了支持其他类库的自定义组件正常使用,在vue-tsx-helper库中提供了 anyslot 标签,包含必须属性 is,其值为目标组件 id 例如在 TSX 中添加一个 vue-router link 使用<RouterLink></RouterLink>会报类型错误,但 TypeScript 认为固有标签应该全部是小写,因此也无法定义 RouterLink 的 type 使用<router-link></router-link>不适合 TSX 语法,因为不允许-字符

因此可以使用如下

import { Component, Prop } from "vue-property-decorator";
import { VueComponent } from "vue-tsx-helper"; // 同时全局提供了anyslot的类型定义
import TestComponent from "../TestComponent";

/**
 * Vue component props types
 */
interface IProps {}

@Component
export default class WrapTestComponent extends VueComponent<IProps> {
  @Prop() msg;

  render(h) {
    return (
      <TestComponent msg={"msg from parent"}>
        <h2>Page writed without .vue but plain tsx</h2>
        <anyslot is="router-link" to="/">
          Back to home
        </anyslot>
      </TestComponent>
    );
  }
}

Babel plugin for Vue 2.0 TSX

Requirements

  • Assumes you are using Babel with a module bundler e.g. Webpack, because the spread merge helper is imported as a module to avoid duplication.

  • This is mutually exclusive with babel-plugin-transform-react-jsx.

Usage

npm install\
  babel-plugin-syntax-jsx\
  babel-plugin-transform-vue-tsx\
  babel-helper-vue-jsx-merge-props\
  babel-preset-env\
  --save-dev

In your .babelrc:

{
  "presets": ["env"],
  "plugins": ["transform-vue-tsx"]
}

The plugin transpiles the following JSX:

<div id="foo">{this.text}</div>

To the following JavaScript:

h(
  "div",
  {
    attrs: {
      id: "foo"
    }
  },
  [this.text]
);

Note the h function, which is a shorthand for a Vue instance's $createElement method, must be in the scope where the JSX is. Since this method is passed to component render functions as the first argument, in most cases you'd do this:

Vue.component("jsx-example", {
  render(h) {
    // <-- h must be in scope
    return <div id="foo">bar</div>;
  }
});

h auto-injection

Starting with version 3.4.0 we automatically inject const h = this.$createElement in any method and getter (not functions or arrow functions) declared in ES2015 syntax that has JSX so you can drop the (h) parameter.

Vue.component("jsx-example", {
  render() {
    // h will be injected
    return <div id="foo">bar</div>;
  },
  myMethod: function() {
    // h will not be injected
    return <div id="foo">bar</div>;
  },
  someOtherMethod: () => {
    // h will not be injected
    return <div id="foo">bar</div>;
  }
});

@Component
class App extends Vue {
  get computed() {
    // h will be injected
    return <div id="foo">bar</div>;
  }
}

Difference from React JSX

First, Vue 2.0's vnode format is different from React's. The second argument to the createElement call is a "data object" that accepts nested objects. Each nested object will be then processed by corresponding modules:

render (h) {
  return h('div', {
    // Component props
    props: {
      msg: 'hi'
    },
    // normal HTML attributes
    attrs: {
      id: 'foo'
    },
    // DOM props
    domProps: {
      innerHTML: 'bar'
    },
    // Event handlers are nested under "on", though
    // modifiers such as in v-on:keyup.enter are not
    // supported. You'll have to manually check the
    // keyCode in the handler instead.
    on: {
      click: this.clickHandler
    },
    // For components only. Allows you to listen to
    // native events, rather than events emitted from
    // the component using vm.$emit.
    nativeOn: {
      click: this.nativeClickHandler
    },
    // class is a special module, same API as `v-bind:class`
    class: {
      foo: true,
      bar: false
    },
    // style is also same as `v-bind:style`
    style: {
      color: 'red',
      fontSize: '14px'
    },
    // other special top-level properties
    key: 'key',
    ref: 'ref',
    // assign the `ref` is used on elements/components with v-for
    refInFor: true,
    slot: 'slot'
  })
}

The equivalent of the above in Vue 2.0 JSX is:

render (h) {
  return (
    <div
      // normal attributes or component props.
      id="foo"
      // DOM properties are prefixed with `domProps`
      domPropsInnerHTML="bar"
      // event listeners are prefixed with `on` or `nativeOn`
      onClick={this.clickHandler}
      nativeOnClick={this.nativeClickHandler}
      // other special top-level properties
      class={{ foo: true, bar: false }}
      style={{ color: 'red', fontSize: '14px' }}
      key="key"
      ref="ref"
      // assign the `ref` is used on elements/components with v-for
      refInFor
      slot="slot">
    </div>
  )
}

Component Tip

If a custom element starts with lowercase, it will be treated as a string id and used to lookup a registered component. If it starts with uppercase, it will be treated as an identifier, which allows you to do:

import Todo from "./Todo.js";

export default {
  render(h) {
    return <Todo />; // no need to register Todo via components option
  }
};

JSX Spread

JSX spread is supported, and this plugin will intelligently merge nested data properties. For example:

const data = {
  class: ["b", "c"]
};
const vnode = <div class="a" {...data} />;

The merged data will be:

{ class: ['a', 'b', 'c'] }

Vue directives

Note that almost all built-in Vue directives are not supported when using JSX, the sole exception being v-show, which can be used with the v-show={value} syntax. In most cases there are obvious programmatic equivalents, for example v-if is just a ternary expression, and v-for is just an array.map() expression, etc.

For custom directives, you can use the v-name={value} syntax. However, note that directive arguments and modifiers are not supported using this syntax. There are two workarounds:

  1. Pass everything as an object via value, e.g. v-name={{ value, modifier: true }}

  2. Use the raw vnode directive data format:

const directives = [{ name: "my-dir", value: 123, modifiers: { abc: true } }];

return <div {...{ directives }} />;