webpack.config.js 5.3 KB
Newer Older
W
wangzhe409 已提交
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
'use strict';

const path = require('path');
const glob = require('glob');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const modifyVars = Object.assign({});

//需要webpack排除的依赖包
const webpackExternals = {
  'jquery': {
    root: '$',
    commonjs2: 'jquery',
    commonjs: 'jquery',
    amd: 'jquery'
  }
};

module.exports = function(webpack, config, isProd, useWatch, useHmr) {
  //获取需要打包的文件
  let needExtractCommons = [],
    entrys = { common: ['./src/common/indexConfig', 'react', 'react-dom', 'mobx', 'mobx-react', 'nornj', 'nornj-react', 'core-decorators'] }
  glob.sync('./src/pages/**/container.js', { ignore: null }).forEach(file => {
    let path = file.substring(file.indexOf("pages/"), file.lastIndexOf("."));

    if (!/\/index$/.test(path)) {
      needExtractCommons.push(path);
      entrys[path] = useHmr ? ['webpack-hot-middleware/client', './src/' + path + '.js'] : './src/' + path + '.js';
    } else {
      entrys[path] = './src/' + path + '.js';
    }
  });

  //配置插件
  let plugins = [
    new webpack.DefinePlugin({
      G_VER: JSON.stringify(isProd ? config.ver : 'random'),
      G_WEB_DOMAIN: JSON.stringify(config.webDomain),
      'process.env': {
        'NODE_ENV': JSON.stringify(isProd ? 'production' : 'development')
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'common',
      // (the commons chunk name)

      filename: 'common.js',
      // (the filename of the commons chunk)

      minChunks: 4,
      // (Modules must be shared between 3 entries)

      chunks: needExtractCommons,
      // (Only use these entries)
    }),
    new ExtractTextPlugin({ filename: 'css/[name].css', allChunks: true }),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
    // new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    new webpack.LoaderOptionsPlugin({
      // test: /\.xxx$/, // may apply this only for some modules
      options: {
        postcss: [autoprefixer({ browsers: ['last 50 versions'] })]
      }
    })
  ];

  if (isProd) {
    plugins.push(new webpack.optimize.UglifyJsPlugin({
      compressor: {
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        screw_ie8: false,
        warnings: false
      },
      sourceMap: true
    }));
  }
  if (useHmr) {
    plugins.push(new webpack.HotModuleReplacementPlugin());
  }

  return {
    //devtool: isProd ? 'source-map' : false,
    devtool: 'source-map',
    watch: useWatch,
    externals: webpackExternals,
    entry: entrys,
    output: {
      //path: path.join(__dirname, 'server/public/resources/app'),
      filename: '[name].js',
      library: 'Global_[name]',
      libraryTarget: 'umd',
      umdNamedDefine: true,
      publicPath: '/resources/app/'
    },
    module: {
      rules: [{
          test: /\.css$/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [{
              loader: 'css-loader',
              options: {
                minimize: isProd,
                sourceMap: isProd
              }
            }]
          }),
        },
        {
          test: /\.less$/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [{
              loader: 'css-loader',
              options: {
                minimize: isProd,
                sourceMap: isProd
              }
            }, 'postcss-loader', {
              loader: 'less-loader',
              options: {
                "modifyVars": modifyVars
              }
            }]
          }),
          exclude: /.m.less$/
        },
        {
          test: /\.m.less$/,
          use: ['style-loader', {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[name]__[local]-[hash:base64:5]'
            }
          }, 'postcss-loader', 'less-loader']
        },
        {
          test: /\.js$/,
          use: [{
            loader: 'babel-loader',
            options: {
              plugins: ['external-helpers']
            }
          }],
          exclude: /node_modules/
        },
        {
          test: /\.t.html(\?[\s\S]+)*$/,
          use: [{
            loader: 'nornj-loader',
            options: {
              outputH: true,
              delimiters: 'react',
              extensionConfig: require('nornj-react/mobx/extensionConfig')
            }
          }]
        },
        {
          test: /\.((woff2?|svg)(\?v=[0-9]\.[0-9]\.[0-9]))|(woff2?|svg)$/,
          use: [{
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: 'fonts/[hash:8][name].[ext]'
            }
          }]
        },
        {
          test: /\.((ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9]))|(ttf|eot)$/,
          use: [{
            loader: 'file-loader',
            options: {
              limit: 8192,
              name: 'fonts/[hash:8][name].[ext]'
            }
          }]
        },
        {
          test: /\.(jpe?g|png|gif|ico)$/,
          use: [{
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: 'images/[hash:8][name].[ext]'
            }
          }]
        }
      ],
    },
    resolve: {
      extensions: ['.js', '.css', '.less']
    },
    plugins
  };
};