webpack.config.js 7.12 KB
var path = require('path')
var webpack = require('webpack')
const { port } = require("../package");

const htmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin')
const SpritesmithPlugin = require('webpack-spritesmith');
const templateFunction = require('./sprite')

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
//localhost:10500
const IS_DEV_MODE = process.env.NODE_ENV !== 'production'
const getPublicPath = IS_DEV_MODE ? '' : './';
const resourcesPath = IS_DEV_MODE ? `http://localhost:${port}` : 'https://e-shield-old.glsx.com.cn';

module.exports = {
  mode: IS_DEV_MODE ? 'development':'production',
  entry: [path.resolve(__dirname, '../src/main.js')/*, 'webpack-hot-middleware/client?reload=true'*/],
  output: {
    path: IS_DEV_MODE ? path.resolve(__dirname, '../template/'):path.resolve(__dirname, '../build/'),
    publicPath: getPublicPath,
    chunkFilename: IS_DEV_MODE ?'js/[name].bundle.js':'js/[name].[hash].bundle.js',
    filename: IS_DEV_MODE ?'js/[name].bundle.js':'js/[name].[hash].bundle.js',
    // 微应用的包名,这里与主应用中注册的微应用名称一致
    library:  `oEshield-[name]`,
    // 将你的 library 暴露为所有的模块定义下都可运行的方式
    libraryTarget: 'umd',
    // 按需加载相关
    jsonpFunction: `webpackJsonp_oEshield`
  },
  externals: {
    vue: 'Vue',
    'element-ui': 'ElementUI'
  },
  module: {
    rules: [
      {
        test: /\.styl$/,
        use: [
          'style-loader',
          'css-loader',
          'stylus-loader'
        ],
      },
      {
        test: /\.s?[ac]ss$/,
        use: [
          IS_DEV_MODE ? 'style-loader' : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {sourceMap: true}
          },
          {
            loader: 'postcss-loader',
            options: {sourceMap: true}
          },
          {
            loader: 'sass-loader',
            options: {sourceMap: true}
          }
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: 'images/[name].[ext]?[hash]',
          publicPath: resourcesPath
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          publicPath: resourcesPath,
          name: 'fonts/[name].[hash:7].[ext]'
        }
      }
    ]
  },
  optimization: {
    //minimize: true,
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true // set to true if you want JS source maps
      }),
      new OptimizeCSSAssetsPlugin({})
    ],
  },
  plugins: [
    new htmlWebpackPlugin({
      chunks: ['main'],
      filename: 'index.html',
      template: path.resolve(__dirname, `../template/index.html`)
    }),
    new webpack.HotModuleReplacementPlugin(),
    /*new SpritesmithPlugin({
        src: {
            cwd: path.resolve(__dirname, 'src/assets/ico'),
            glob: '*.png'
        },
        target: {
            image: path.resolve(__dirname, 'src/assets/spritesmith-generated/sprite.png'),
            css: path.resolve(__dirname, 'src/assets/spritesmith-generated/sprite.styl')
        },
        apiOptions: {
            cssImageRef: "~sprite.png"
        }
    }),*/
    new SpritesmithPlugin({
      src: {
          cwd: path.resolve(__dirname, '../src/assets/ico'),
          glob: '*.png'
      },
      target: {
          image: path.resolve(__dirname, '../src/assets/spritesmith-generated/sprite.png'),
          css: [
              [path.resolve(__dirname, '../src/assets/spritesmith-generated/sprite.css'), {
                format: 'function_based_template'
              }]
          ]
      },
      customTemplates: {
        'function_based_template': templateFunction,
      },
      apiOptions: {
          cssImageRef: "~sprite.png"
      }
    }),
    new MiniCssExtractPlugin({
      filename: IS_DEV_MODE ?'css/[name].min.css':'css/[name].[hash].min.css',
      chunkFilename: IS_DEV_MODE ?'css/[name].min.css':'css/[name].[hash].min.css',
    }),
    //new BundleAnalyzerPlugin()
  ],
  resolve: {
    modules: ["node_modules", "spritesmith-generated"],
    alias: {
      'vue$': 'vue/template/vue.esm.js',
      'components': path.resolve(__dirname, '../src/components'),
      'utils': path.resolve(__dirname, '../src/utils'),
      'pages': path.resolve(__dirname, '../src/pages'),
      'scss': path.resolve(__dirname, '../src/assets/SCSS'),
      'images': path.resolve(__dirname, '../src/assets/images'),
      './patterns': path.resolve(__dirname, '../src/assets/images'),
      '@':path.resolve(__dirname,'../src')
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    /*hot: true,*/
    inline: true,
    noInfo: true,
    overlay: true,
    port: 10500,
    // 关闭主机检查,使微应用可以被 fetch
    disableHostCheck: true,
    // 配置跨域请求头,解决开发环境的跨域问题
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    contentBase: 'template/',
    proxy: {
        '/ots': {
          changeOrigin: true,
          target: 'http://192.168.2.53:8889/'//'http://e-shield.glsx.net'//'http://e-shield.glsx.net'//'http://192.168.1.158:8088'//'http://e-shield.didihu.com.cn'//'http://192.168.1.53:8088'//'http://localhost:8088'//'http://tiger-sso.didihu.com.cn'// http://vjg7.branch.glsx.com.cn/
        }
    }
  },
  performance: {
    hints: false
  },
  devtool: IS_DEV_MODE?'#eval-source-map':'#source-map'
  
}

if(IS_DEV_MODE){
  module.exports.plugins = (module.exports.plugins || []).concat([
    //new HardSourceWebpackPlugin(),
    new CleanWebpackPlugin(['template/js','template/css','template/images', 'template/fonts'], {
      verbose:  true,
      dry:      false,
      root: path.resolve(__dirname, '../')
    })
  ]);
}else{
  module.exports.plugins = (module.exports.plugins || []).concat([
    new CleanWebpackPlugin(['build/js','build/css','build/images', 'build/fonts'], {
      verbose:  true,
      dry:      false,
      root: path.resolve(__dirname, '../')
    })
  ]);
}