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-inline-template-next

v1.0.5

Published

transform vue inline template to render function Babel plugin

Downloads

27

Readme

babel-plugin-transform-vue-inline-template-next

介绍

转换内联 vue template 模板 Babel 插件,外部依赖 vue-template-compiler、vue-template-es2015-compiler,使用时必须根据不同的 vue 版本安装对应的 vue-template-compiler、vue-template-es2015-compiler。

安装教程

1.  npm install -D babel-plugin-transform-vue-inline-template-next

1.4 版本更新,添加自定义 vue-template-compile、vue-template-es2015-compiler 配置项

vueTemplateCompilerOpts、 vueTemplateEs2015CompilerOpts,与 vue-template-compile、vue-template-es2015-compiler 配置一样,默认配置也一样。

使用说明

babel 配置

{
     plugins: [
      ['babel-plugin-transform-vue-inline-template-next', {
          flagStr: "h", // 标记编译魔法注释,全量编译不开启时,在代码中添加该魔法注释,将编译代码
          compileAll: false, // 是否开启全量编译
          ignoreFlagStr: "ignore", // 忽略编译魔法注释,全量编译开启时,在代码中添加该魔法注释,将忽略编译代码
          vueTemplateCompilerOpts: {  // vue-template-compile 配置
            whitespace: "condense",
            pad: "line",
          },
          vueTemplateEs2015CompilerOpts: { // vue-template-es2015-compiler 配置
            modules: false,
            stripWith: true,
            stripWithFunctional: false,
          },
        }
      ]
    ],
}

在 vue 的内联模板 template 上方加上魔法注释 /* h */,打包时便会识别到编译成 render 函数。

代码示例

new Vue({
  el: "#app",
  data: function () {
    return {
      test2: 18,
      test: "test",
    };
  },
  /* h */
  template: `
    <div v-show="test2 > 1" @click="testFn" :test="test2">{{test}}</div>
  `,
  methods: {
    testFn() {
      console.log(this.test2);
      this.test2--;
    },
  },
});

// or

var test = {
  el: "#app",
  data: function () {
    return {
      test2: 18,
      test: "test",
    };
  },
  /* h */
  template: `
    <div v-show="test2 > 1" @click="testFn" :test="test2">{{test}}</div>
  `,
  methods: {
    testFn() {
      console.log(this.test2);
      this.test2--;
    },
  },
};

编译后的代码

new Vue({
  el: "#app",
  data: function data() {
    return {
      test2: 18,
      test: "test",
    };
  },
  render: function _render1() {
    var _vm = this;

    var _h = _vm.$createElement;

    var _c = _vm._self._c || _h;

    return _c(
      "div",
      {
        directives: [
          {
            name: "show",
            rawName: "v-show",
            value: _vm.test2 > 1,
            expression: "test2 > 1",
          },
        ],
        attrs: {
          test: _vm.test2,
        },
        on: {
          click: _vm.testFn,
        },
      },
      [_vm._v(_vm._s(_vm.test))]
    );
  },
  methods: {
    testFn: function testFn() {
      console.log(this.test2);
      this.test2--;
    },
  },
});

// or

var test = {
  el: "#app",
  data: function () {
    return {
      test2: 18,
      test: "test",
    };
  },
  render: function _render1() {
    var _vm = this;

    var _h = _vm.$createElement;

    var _c = _vm._self._c || _h;

    return _c(
      "div",
      {
        directives: [
          {
            name: "show",
            rawName: "v-show",
            value: _vm.test2 > 1,
            expression: "test2 > 1",
          },
        ],
        attrs: {
          test: _vm.test2,
        },
        on: {
          click: _vm.testFn,
        },
      },
      [_vm._v(_vm._s(_vm.test))]
    );
  },
  methods: {
    testFn() {
      console.log(this.test2);
      this.test2--;
    },
  },
};

自定义魔法注释

// flagStr 标记编译,ignoreFlagStr标记忽略编译,只有开启全量编译ignoreFlagStr才有效
{
  plugins: [['babel-plugin-transform-vue-inline-template-next', {
      flagStr: "custom h",
      ignoreFlagStr: "custom ignore1"
    }
  ]],
}

全量编译会忽略 flagStr 魔法注释,所有的 vue 内联都会编译

// compileAll
{
  plugins: [['babel-plugin-transform-vue-inline-template-next',
   {compileAll: true}
  ]],
}

可以用过魔法注释/* ignore1 */忽略编译

new Vue({
  el: "#app",
  data: function () {
    return {
      test2: 18,
      test: "test",
    };
  },
  /* ignore1 */
  template: `
    <div v-show="test2 > 1" @click="testFn" :test="test2">{{test}}</div>
  `,
  methods: {
    testFn() {
      console.log(this.test2);
      this.test2--;
    },
  },
});