getCommonConfig.js 6.1 KB
Newer Older
C
init  
Conan 已提交
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

const path = require('path');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const {getBabelPath, getExcludeBabelPath, getGlobalCheckWhiteList, getFreePort} = require('./utils');
var UglifyJsPlugin = require('uglifyjs-webpack-plugin')
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const ChameleonWebpackPlugin = require('chameleon-webpack-plugin')
const WebpackCheckPlugin = require('webpack-check-plugin')
const config = require('./config.js');
const ChameleonErrorsWebpackPlugin = require('chameleon-errors-webpack-plugin');

module.exports = function (options) {
  let {
    type,
    media,
    root
  } = options;

  function getstaticPath(filetype) {
    return `static/${filetype}/[name]_[hash:7].[ext]`
  }

  let webServerPort = getFreePort().webServerPort;

  let publicPath;
  let defaultPublichPathMap = {
    'wx': '/',
    'alipay': '/',
30
    'baidu': `http://${config.ip}:${webServerPort}/baidu/`, // baidu小程序的publicPath不能设置能/  所以在启动dev服务的时候 也将dist作为静态资源
C
init  
Conan 已提交
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 82 83 84 85 86
    'web': `http://${config.ip}:${webServerPort}/`,
    'weex': `http://${config.ip}:${webServerPort}/weex/`
  }

  publicPath = options.publicPath || defaultPublichPathMap[type];


  let commonConfig = {
    stats: cml.logLevel === 'debug' ? 'verbose' : 'none',
    output: {
      publicPath: publicPath
    },
    resolve: {
      symlinks: false,
      extensions: ['.cml', '.interface', '.vue', '.js'],
      alias: {
        '$CMLPROJECT': path.resolve(cml.root),
        '$PROJECT': path.resolve(root),
        '$ROUTER': path.resolve(root, 'node_modules/chameleon-runtime/.temp/router.js'),
        '$ROUTER_CONFIG': path.resolve(root, './src/router.config.json')
      },
      modules: [
        'node_modules',
        path.join(cml.root, '/node_modules')
      ]
    },
    resolveLoader: {
      modules: [
        'node_modules',
        path.join(cml.root, '/node_modules')
      ]
    },
    module: {
      rules: [{
        test: path.resolve(root, 'node_modules/chameleon-runtime/.temp/router.js'),
        loader: path.join(__dirname, 'routerLoader.js')
      },
      {
        test: /\.js$/,
        exclude: getExcludeBabelPath(),
        // 不能babel babel-runtime
        include: getBabelPath(),
        use: [{
          loader: 'babel-loader',
          options: {
            'filename': path.join(cml.root, 'chameleon.js')
          }
        }

        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'chameleon-url-loader',
        options: {
          limit: false, // 不做limit的base64转换,需要添加?inline参数
Y
yylgit 已提交
87 88 89 90 91 92
          name: getstaticPath('img'),
          outputPath: function(output) {
            // 处理图片中的@符号 改成_ 解决在支付宝小程序中上传失败的问题
            output = cml.utils.handleSpecialChar(output)
            return output;
          }
C
init  
Conan 已提交
93 94 95 96 97 98
        }
      }, {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'file-loader',
        options: {
          name: getstaticPath('media')
Y
yylgit 已提交
99
         
C
init  
Conan 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        }
      },
      {
        test: /\.(woff|woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'file-loader',
        options: {
          name: getstaticPath('fonts')
        }
      },

      {
        test: /\.interface$/,
        // exclude: /(node_modules|bower_components)/,
        // 不能babel babel-runtime
        include: getBabelPath(),
        use: [{
          loader: 'babel-loader',
          options: {
            'filename': path.join(cml.root, 'chameleon.js')
          }
        },

        {
          loader: 'interface-loader',
          options: {
            cmlType: type,
            media,
            check: cml.config.get().check
          }
        }
        ]
      }
      ]

    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env.platform': JSON.stringify(type)
      }),
      new ChameleonErrorsWebpackPlugin({
        cmlType: type
Y
yylgit 已提交
141
      })
C
init  
Conan 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    ]
  }
  if (cml.config.get().enableGlobalCheck === true) {
    commonConfig.plugins.push(
      new WebpackCheckPlugin({
        cmlType: type,
        whiteListFile: getGlobalCheckWhiteList()
      })
    )
  }

  if (options.definePlugin) {
    commonConfig.plugins.push(new webpack.DefinePlugin(options.definePlugin))
  }
  if (options.analysis) {
    commonConfig.plugins.push(new BundleAnalyzerPlugin())
  }
Y
yylgit 已提交
159 160 161 162
  let devApiPrefix = `http://${config.ip}:${webServerPort}`
  // 兼容旧版api
  let apiPrefix = options.apiPrefix || devApiPrefix;
  // 新版api 优先读取domainMap
Y
api  
yylgit 已提交
163
  let domainMap = (cml.config.get().domainMap && cml.config.get().domainMap[cml.media]) || {
Y
yylgit 已提交
164 165
    apiPrefix
  };
J
JiM-W 已提交
166
  let defaultDomainKey = cml.config.get().defaultDomainKey || 'apiPrefix';
Y
yangyiliang 已提交
167
  if (options.media === 'dev') {
Y
yylgit 已提交
168 169 170 171
    // dev模式默认apiPrefix
    commonConfig.plugins.push(new webpack.DefinePlugin({
      'process.env.devApiPrefix': JSON.stringify(devApiPrefix)
    }))
C
init  
Conan 已提交
172
  }
Y
yylgit 已提交
173
  // 兼容旧版api
Y
yangyiliang 已提交
174
  commonConfig.plugins.push(new webpack.DefinePlugin({
Y
yylgit 已提交
175
    'process.env.cmlApiPrefix': JSON.stringify(apiPrefix)
Y
yangyiliang 已提交
176 177 178 179 180 181 182 183 184 185
  }))
  commonConfig.plugins.push(new webpack.DefinePlugin({
    'process.env.domainMap': JSON.stringify(domainMap)
  }))
  commonConfig.plugins.push(new webpack.DefinePlugin({
    'process.env.defaultDomainKey': JSON.stringify(defaultDomainKey)
  }))
  commonConfig.plugins.push(new webpack.DefinePlugin({
    'process.env.media': JSON.stringify(options.media)
  }))
C
init  
Conan 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

  if (options.minimize) {
    commonConfig.plugins = commonConfig.plugins.concat([
      new OptimizeCSSPlugin({
        assetNameRegExp: /\.css$/,
        cssProcessorOptions: { safe: true, discardComments: { removeAll: true } }
      }),
      new UglifyJsPlugin({})
    ])
  }

  let moduleIdType = options.moduleIdType
  let moduleIdMap = {
    hash: new webpack.HashedModuleIdsPlugin(),
    name: new webpack.NamedModulesPlugin(),
    chameleon: new ChameleonWebpackPlugin({openModuleHash: true, openChunkHash: true})
  }
  if (moduleIdType && moduleIdMap[moduleIdType]) {
    commonConfig.plugins.push(moduleIdMap[moduleIdType])
  }

  return commonConfig;
}