angular之html转wxml模板node.js微信小程序wxml模板转换工具(初版)

转换js脚本: 

const fs = require('fs');

const jsdom = require('jsdom');

const { JSDOM } = jsdom;

const ngHtmlToWxml = ngHtmlFilePath => {
    const exists = fs.existsSync(ngHtmlFilePath);

    if (!exists) {
        console.error(ngHtmlFilePath + '不存在。 ');
        return;
    }

    const data = fs.readFileSync(ngHtmlFilePath, 'utf8');

    const dom = new JSDOM(data);
    // dom.window.document.querySelector("ion-content").innerHTML;

    const elements = dom.window.document.getElementsByTagName('*');
    for (let index = 0; index < elements.length; index++) {
        const element = elements[index];

        let clickValue = element.getAttribute('(click)');
        if (clickValue) {
            element.removeAttribute('(click)');
            element.setAttribute('bindtap', clickValue.substring(0, clickValue.indexOf('(')));
            const paramsArray = clickValue
                .substring(clickValue.indexOf('(') + 1, clickValue.lastIndexOf(')'))
                .split(',');
            paramsArray.forEach(ele => {
                const paramsName = ele.trim();
                if (paramsName) {
                    element.setAttribute('data-' + paramsName, `{{${paramsName}}}`);
                }
            });
        }

        let ifValue = element.getAttribute('*ngIf');
        if (ifValue) {
            element.removeAttribute('*ngIf');
            element.setAttribute('wx:if', `{{${ifValue}}}`);
        }

        let forValue = element.getAttribute('*ngFor');
        if (forValue) {
            let forValueArrays = forValue.split(' ');
            element.removeAttribute('*ngFor');
            element.setAttribute('wx:for', `{{${forValueArrays[3]}}}`);
            element.setAttribute('wx:for-index', `i`);
            element.setAttribute('wx:for-item', `${forValueArrays[1]}`);
            element.setAttribute('wx:key', 'id');
        }

        // TODO 这里只处理了input
        let ngModel = element.getAttribute('[(ngModel)]');
        if (ngModel) {
            element.removeAttribute('[(ngModel)]');
            // input
            element.setAttribute('bindinput', ngModel);
        }

        // TODO 未完待续 哈哈哈 20200325 01:10

        


    }

    const body = dom.window.document.getElementsByTagName('body');

    const newHtml = body[0].innerHTML;

    return newHtml;
};

const saveWxml = (wxmlFilePath, wxml) => {
    const exists = fs.existsSync(wxmlFilePath);
    if (exists) {
        console.error(wxmlFilePath + '已存在!');
        return;
    }
    fs.writeFileSync(wxmlFilePath, wxml);
    console.log('wxml转换成功,请检查测试!');
};

const ngHtmlFilePath = './mall-product-details.page.html';

const wxml = ngHtmlToWxml(ngHtmlFilePath);

const wxmlFilePath = ngHtmlFilePath.replace('.html', '.wxml');

saveWxml(wxmlFilePath, wxml);

依赖信息:

{
    "name": "wx-cli",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "fs": "0.0.1-security",
        "jsdom": "^15.2.0"
    }
}

 执行方式: node  index.js

发布了82 篇原创文章 · 获赞 44 · 访问量 10万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览