Weex开发让前端飞

weex+express+gulp开发文件搭建

2016-11-22  本文已影响592人  子龙爱弹琴

开发环境搭建

快速入门

下载weex-toolkit

npm install -g weex-toolkit

编写一个简单的we文件如:

<template>
  <container>
    <text style="font-size: {{size}}">{{title}}</text>
  </container>
</template>

<script>
  module.exports = {
    data: {
      size: 48,
      title: 'Alibaba Weex Team'
    }
  }
</script>

在浏览器中调试查看使用命令

weex codePath
weex codePath --qr

在gulp中集成weex开发和部署工程

const gulp = require('gulp');
const weex = require('gulp-weex');

gulp.task('weex', ()=>{
    return gulp.src(codePath)
        .pipe(weex({}))
        .pipe(gulp.dest(outputPath));
});

gulp-weex编译生成的jsbuddle可以在web,android和ios上直接使用,但是需要使用一些手段来进行加载该jsbuddle

window.weex.init({
    appId: location.href,
    loader: loader,
    source: page,
    rootId: 'weex'
})
  1. appId是weex的实例id,可以为任何唯一的字符串或者是数字,用来表明weex实例;
  2. loader模块加载方式,默认为xhr,即异步的加载方式,也可以为jsonp,或者是sorce也就是直接将jsBuddle的编译码放在surce里面
  3. sorce:指定jsBuddle的位置
  4. rootId:weex的根目录id,默认为weex,即<div id="weex"></div>,也就会将生成的dom节点放在这个div里面

我们想要达到的效果是,可以输出一个页面的地址目录,然后点击每个目录可以有相应的weex页面可以显示出来,同时可以通过gulp进行批量出来,而不是需要使用weex codePath才能看到效果,而是可以通过我们自己起的服务来实现这个效果,这样就可以在我们的node项目里面一起启动了

我们使用3步来实现这个效果

  1. 使用gulp-weex对相应的weex文件编译成可以直接使用的jsbuddle这样在我们开发环境和生产环境就能保证文件的一致性
  2. 使用through2对生成的文件进行处理,取出路径和文件名,这个路径就是我们进入当前业务的加载模块的路径
  3. 按照上面得到的文件列表组装成相应的文件目录,得出我们最终要显示的文件列表页面

首先,我们会将我们的weex文件放在一个项目目录里面,然后使用gulp对这些文件进行处理,同时取出

{
    "weex": {
        "views": "app/views/weex/",
        "mainHtml": "task/weexTemplate/",
        "transportJs": "app/public/weex/",
        "previewPath": "app/public/weexPreview/"
    }
}
const gulp = require('gulp');
const weex = require('gulp-weex');

/**
 * 只对每个业务的入口文件进行weex的编译
 */
gulp.task('weex', function () {
    return gulp.src(config.weex.views + '**/*.we')
        .pipe(weex({}))
        .pipe(gulp.dest(config.weex.transportJs));
});
const titleRegExp    = /<!--title: (.*)-->/;
const pathNameRegExp = /(.*)\.we/;
/**
 * 在浏览器上预览weex页面
 */
gulp.task('weex:ToJson', function () {
    return gulp.src(config.weex.views + '**/*.we')
        .pipe(through2.obj(function (file, encoding, callback) {
            if ( file.isNull() ) {
                return callback(null, file);
            }

            var html = file.contents.toString();

            if ( !html ) {
                return callback(null, file);
            }
            
            //取出页面标题
            var title = html.match(titleRegExp)[1];
            //取出页面地址
            var path  = file.relative;
            path      = path.match(pathNameRegExp)[1] + '.js';
            
            //页面地址放到内存数组中
            sourcemap.push({
                title: title,
                path: path
            });

            callback();
        }));
});
html:
    <h2>Weex 页面</h2>
    <!--遍历输出页面路近-->
    <ul>
        {% for item in data %}
        <li><a href="/public/weexPreview/index.html?hot-reload_controller&page={{item.path}}&loader=xhr">{{item.title}}</a></li>
        {% endfor %}
    </ul>
    
gulpJS:
gulp.task('weex:dev', ['weex', 'weex:ToJson'], function () {
    return gulp.src(config.weex.mainHtml + 'main.html')
        .pipe(nunjucks.compile({ data: sourcemap }))
        .pipe(gulp.dest(config.weex.previewPath));
});
<div id="weex"></div>
<script src="/public/weexPreview/lib/weex.js"></script>
<script>
    (function () {
        function getUrlParam(key) {
            var reg   = new RegExp('[?|&]' + key + '=([^&]+)')
            var match = location.search.match(reg)
            return match && match[1]
        }

        var loader = 'xhr';
        var page   = '/public/weex/' + getUrlParam('page');

        window.weex.init({
            appId: location.href,
            loader: loader,
            source: page,
            rootId: 'weex'
        })
    })();
</script>
const nodemon = require('gulp-nodemon');
const open = require('gulp-open');

gulp.task('open:weex', function () {
    return gulp.src('')
        .pipe(open({ uri: 'http://localhost:9080/public/weexPreview/main.html' }));
});

/**
 * 本地开发weex页面
 */
gulp.task('dev:weex', [ 'weex:dev', 'copy:weex' ], function () {
    nodemon({
        script: 'index.js',
        ext: 'js html',
        ignore: [
            '../'
        ],
        env: { NODE_ENV: 'development', PORT: 9080 }
    }).on('start', [ 'open:weex', 'watch:weex' ]);
});
上一篇 下一篇

猜你喜欢

热点阅读