var path = require('path'); var webpack = require('webpack'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const CleanWebpackPlugin = require('clean-webpack-plugin'); const packageInfo = require('./package.json'); const CompressionWebpackPlugin = require("compression-webpack-plugin"); module.exports = (env = {}) =>{ console.log(`------------------- ${env.Generative?'生产':'开发'}环境 -------------------`); var plugins = (module.exports.plugins || []).concat([ new CleanWebpackPlugin(['dist']), new webpack.optimize.UglifyJsPlugin({ // sourceMap: true, // compress: { // warnings: false // } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ]) if(!env.Generative){ plugins = []; } plugins.push( new CompressionWebpackPlugin({ filename: '[path].gz[query]', //压缩后的文件名 algorithm: 'gzip',// 压缩格式 有:gzip、brotliCompress、 test: /\.(js|css|svg)$/, threshold: 10240,// 只处理比这个值大的资源,按字节算 minRatio: 0.8, //只有压缩率比这个值小的文件才会被处理,压缩率=压缩大小/原始大小,如果压缩后和原始文件大小没有太大区别,就不用压缩 deleteOriginalAssets: false //是否删除原文件,最好不删除,服务器会自动优先返回同名的.gzip资源,如果找不到还可以拿原始文件 })); plugins.push(new CopyWebpackPlugin([{ // from: './src/static', // to: 'static' from : path.join(__dirname,'./src/static'), to : 'static' }])); plugins.push(new HtmlWebpackPlugin({ // title: "自动生成网页标题", filename: "./index.html", hash:true, showErrors: true, version: packageInfo.version, template: './src/index.html' })); return { entry: { app:['babel-polyfill','./src/main.js'], vendor: ['vue', 'vue-router', 'vuex', 'element-ui'] }, output: { path: path.resolve(__dirname, './dist'),//输出结果 publicPath: env.production ? './' : '/', //文件路径 filename: '[name].js', chunkFilename: 'js/[name].js' }, module: { rules: [ // 使用vue-loader 加载 .vue 结尾的文件 { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { }, // other vue-loader options go here presets: ['es2015'], plugins: ['transform-runtime', 'transform-object-rest-spread'] }, // query: { // } }, // 使用babel 加载 .js 结尾的文件 { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, // 加载图标 { test: /\.(png|jpg|gif|svg)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]', limit: 8192, outputPath: 'static/images/' } }, //加载css { test: /\.css$/, loader: "style-loader!css-loader", // loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]', // exclude: /node_modules/ }, // { // test: /\.scss$/, // loader: "style-loader!css-loader!sass-loader!" // }, // { // test: /\.(woff|woff2|svg|ttf|eot)$/, // use:[ // {loader:'file-loader',options:{name:'fonts/[name].[hash:8].[ext]'}}//项目设置打包到dist下的fonts文件夹下 // // {loader:'url-loader',options:{name:'fonts/[name].[hash:8].[ext]'}}//项目设置打包到dist下的fonts文件夹下 // ] // }, { test: /\.(woff|woff2|svg|eot|ttf)\??.*$/, // loader: 'url-loader' loader:'url-loader',options:{name:'fonts/[name].[hash:8].[ext]'}//项目设置打包到dist下的fonts文件夹下 }, // { // test: /\.html$/, // loader: 'html-loader', // options: { // minimize: false // } // } ] }, resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js' } }, externals: { "BMap": "BMap" }, devServer: { inline: true, //检测文件变化,实时构建并刷新浏览器 port: "8880", proxy: { '/T': { // target: 'https://opendouyinapitest.malls.iformall.com/T', target: 'https://opendouyinapi.malls.iformall.com/T', pathRewrite: { "^/T": "" }, secure: false, changeOrigin: true }, }, historyApiFallback: true, }, performance: { hints: false }, plugins:plugins, // devtool: '#eval-source-map'//开发模式下更方便定位错误 devtool:env.Generative?false:'#eval-source-map' } }