template-shift

0.1.0 • Public • Published

Template-Shift

A script used to shift HTML-like template code.

Usage

You can use it by two ways, both of them need a user-defined handler.

  1. Command line
$ template-shift ./test/fixtures/handler.js
  1. Nodejs package
const TemplateShift = require('template-shift');
const Handler = {
  entry: '',
  dist: '',
  mapping: {},
  visitor() {},
}
const result = TemplateShift(templateString, Handler);

Handler

Fields Type Description
entry String|Array<String> raw template file path
dist String the dist folder that output result file into
mapping Object mapping relation used to rename tag name or attribute name
visitor Function manipulate every nodes by what you want, receives the current node ast.

There is an example file in /test/fixtures/handler.js, which do a basic shift from weapp wxml file to html.

module.exports = {
  entry: ['./test/fixtures/example.wxml'],
  dist: './dist/',
  mapping: {
    tag: {
      'view': 'div',
      'image': 'img',
      'text': 'span',
    },
    attr: {
      'wx:for': 'v-for',
      'wx:key': ':key',
      'class': ':class',
    }
  },
  visit(node) {
    const { attrs } = node;

    if (attrs) {
      attrs.forEach((attr) => {
        if (/^{{.+}}$/.test(attr.value)) {
          attr.value = attr.value.match(/^{{(.+)}}$/)[1].trim();
          if (attr.name[0] !== ':') attr.name = `:${attr.name}`
        }
      });
    }
  },
}

Dependents (0)

Package Sidebar

Install

npm i template-shift

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

8.38 kB

Total Files

7

Last publish

Collaborators

  • guox