0.1.0 • Published 6 years ago

template-shift v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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

FieldsTypeDescription
entryString|Array\<String>raw template file path
distStringthe dist folder that output result file into
mappingObjectmapping relation used to rename tag name or attribute name
visitorFunctionmanipulate 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}`
        }
      });
    }
  },
}