这里,我尝试用简单的方式来说明一个工程的构建。然后,再将简单的方式工程化(复杂化),最后拼凑成目前weex开源的代码结构。

安装weex-toolkit

weex-toolkit是一个很好的工具供我们工程构建。首先,第一步是安装该工具:

测试weex-toolkit是否安装成功,可以使用如下命令测试:

  1. info 0.4.4

如果显示版本号即为成功。

这里,我们不再使用weex 跑一个文件的形式。我们需要一个较为完整的方案,因此,这里采用weex init命令创建项目。

首先,我们创建一个目录存放我们的项目,启动命令行:

  1. $ mkdir test
  2. $ cd test
  3. $ weex init
  4. //下面一路空格即可
  5. prompt: Project Name: (test)
  6. file: .gitignore created.
  7. file: README.md created.
  8. file: index.html created.
  9. file: package.json created.
  10. file: src/main.we created.
  11. file: webpack.config.js created.

跑起项目

依赖安装完成,启动项目编译。

  1. $ npm run dev

启动轻量服务器。

  1. $ npm run serve

这时,打开浏览器,输入http://127.0.0.1:8080, 就会看到如下界面效果:

当然这一些都是node/npm的常识了。首先,我们打开package.json文件。可以看到如下代码:

3. 初始化工程干了一件什么事儿 - 图1

ok,看到这里明白了。npm run dev调用了webpack。那么,webpack实际上是执行了配置文件。
webpack默认是webpack.config.js作为配置文件的。所以看一下webpack.config.js中的内容。

这个文件比较好理解。一个是引入了webpack的配置,一个是使用了weex-loader模块。
entry属性是表示入口文件,output表示输出文件,默认输出到dist问价夹。所有打开dist就可以看到一个打包完成的main.js文件。

npm run serve

这个同上,不做展开,主要是做一个服务器,提供浏览器访问静态资源。

是时候,去了解我们index.html文件干了一件啥事。其实,index.html就是页面的入口文件。具体大码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Weex HTML5</title>
  6. <meta name="apple-mobile-web-app-capable" content="yes" />
  7. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  8. <meta name="apple-touch-fullscreen" content="yes" />
  9. <meta name="format-detection" content="telephone=no, email=no" />
  10. <style>
  11. html, body, #weex {
  12. width: 100%;
  13. height: 100%;
  14. }
  15. </style>
  16. <script src="./node_modules/weex-html5/dist/weex.js"></script>
  17. </head>
  18. <body>
  19. <div id="weex"></div>
  20. /**
  21. * Init weex instance depending on the url params.
  22. * There are three ways to load weex bundles, depends on the
  23. * parameter 'loader' in the url:
  24. *
  25. * the bundle's url.
  26. * + source: use the transformed code itself. 'page' should
  27. * be the transformed weex bundle.
  28. *
  29. * @param {String} bundle - It has different meaning depends on
  30. * the type of loader.
  31. */
  32. (function () {
  33. function getUrlParam (key) {
  34. var reg = new RegExp('[?|&]' + key + '=([^&]+)')
  35. var match = location.search.match(reg)
  36. return match && match[1]
  37. }
  38. var loader = getUrlParam('loader') || 'xhr'
  39. var page = getUrlParam('page') || 'dist/main.js'
  40. window.weex.init({
  41. appId: location.href,
  42. loader: loader,
  43. source: page,
  44. rootId: 'weex'
  45. })
  46. })();
  47. </script>
  48. </body>
  49. </html>

作为入口和载体,htmk做了两件事:
1) 拿到页面的URL,根据page参数获得需要加载的js文件路径
2)初始化weex实例,加载文件。
此处文章可参考:

是否可以更好的理解weex github源码结构

  1. var path = require('path');
  2. var fs = require('fs');
  3. var entry = {};
  4. function walk(dir) {
  5. dir = dir || '.'
  6. var directory = path.join(__dirname, '../examples', dir);
  7. .forEach(function(file) {
  8. var fullpath = path.join(directory, file);
  9. var stat = fs.statSync(fullpath);
  10. if (stat.isFile() && extname === '.we') {
  11. var name = path.join('examples', 'build', dir, path.basename(file, extname));
  12. entry[name] = fullpath + '?entry=true';
  13. } else if (stat.isDirectory() && file !== 'build' && file !== 'include') {
  14. var subdir = path.join(dir, file);
  15. walk(subdir);
  16. }
  17. });
  18. }
  19. walk();
  20. module.exports = {
  21. entry: entry,
  22. output : {
  23. path: '.',
  24. filename: '[name].js'
  25. },
  26. module: {
  27. loaders: [
  28. {
  29. test: /\.we(\?[^?]+)?$/,
  30. loader: 'weex'
  31. },
  32. {
  33. test: /\.js(\?[^?]+)?$/,
  34. loader: 'weex?type=script'
  35. },
  36. {
  37. test: /\.css(\?[^?]+)?$/,
  38. loader: 'weex?type=style'
  39. },
  40. {
  41. test: /\.html(\?[^?]+)?$/,
  42. loader: 'weex?type=tpl'
  43. }
  44. ]
  45. }

就是编译examples目录下所有.we文件到build目录。代码可以细看,都是Node.js File System相关的API.