all files / configs/ getWeexBuildConfig.js

22.22% Statements 8/36
0% Branches 0/16
0% Functions 0/2
22.22% Lines 8/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                  
var path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin')
const AssetsPlugin = require('assets-webpack-plugin')
 
 
var getWeexCommonConfig = require('./getWeexCommonConfig.js');
var getWeexExportConfig = require('./component_export/getWeexExportConfig_new');
var utils = require('./utils.js');
 
 
var merge = require('webpack-merge')
 
module.exports = function (options) {
  if (options.media === 'export') {
    return getWeexExportConfig(options);
  }
  var outputPath = path.resolve(options.root, 'dist/weex');
  var buildConfig = {
    output: {
      path: outputPath
    },
    plugins: [
      new CleanWebpackPlugin(['./*'], {root: outputPath, verbose: false}),
      new AssetsPlugin({
        filename: '/dist/config.json',
        processOutput: function (assets) {
          let config = cml.config.get();
          config.buildInfo = config.buildInfo || {};
          let {wxAppId} = config.buildInfo
          let weexjs = assets[config.projectName].js;
          let {routerConfig, hasError} = cml.utils.getRouterConfig();
          if (hasError) {
            throw new Error('router.config.json格式不正确')
          }
 
          if (routerConfig) {
            let result = [];
            if (!routerConfig.domain) {
              throw new Error('router.config.json 中未设置web端需要的domain字段');
            }
            let {domain, mode} = routerConfig;
 
            routerConfig.routes.forEach(item => {
              let webUrl = domain;
              if (mode === 'history') {
                webUrl += item.url;
              } else if (mode === 'hash') {
                webUrl += ('#' + item.url);
              }
              let route = {
                wx: {
                  appId: wxAppId,
                  path: item.path
                },
                web: {
                  url: webUrl
                },
                weex: {
                  url: weexjs,
                  query: {
                    path: item.path
                  }
                }
              }
              if (item.extra) {
                route.extra = item.extra;
              }
              result.push(route);
            })
            return JSON.stringify(result, '', 4);
          }
 
        }
      })
    ]
  }
 
 
  return merge(getWeexCommonConfig(options), buildConfig)
}