template-shift
v0.1.0
Published
A script used to shift HTML-like template code
Downloads
2
Readme
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.
- Command line
$ template-shift ./test/fixtures/handler.js
- 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}`
}
});
}
},
}