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'); var ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin'); const CompressionWebpackPlugin = require("compression-webpack-plugin"); const FileManagerPlugin = require('filemanager-webpack-plugin'); module.exports = (env = {}) => { var plugins = (module.exports.plugins || []).concat([ new CleanWebpackPlugin(['dist']), new webpack.optimize.CommonsChunkPlugin({ names: ['vendor'], minChunks: Infinity, filename: 'common.bundle.[chunkhash].js', }), new webpack.LoaderOptionsPlugin({ minimize: true }), // new webpack.HotModuleReplacementPlugin(), //热加载插件 //代码压缩插件,暂不需要 // new FileManagerPlugin({ // onEnd: { // mkdir: ['./zip'], // archive: [ // { source: './dist', destination: './zip/test.zip' }, // ] // } // }), new ParallelUglifyPlugin({ // cacheDir: '/dist', uglifyJS: { output: { beautify: false, comments: false }, compress: { // warnings: true, // drop_console:true, collapse_vars: true, reduce_vars: 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' }, { from: path.join(__dirname, './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'], // entry:'./src/main.js',//入口 vendor: ['vue', 'vuex', 'element-ui'] // 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'], }, }, // 使用babel 加载 .js 结尾的文件 { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/, }, // 加载图标 { test: /\.(png|jpg|gif|svg)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]', limit: 1000, 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$/, use: ['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]', limit: 1000, }//项目设置打包到dist下的fonts文件夹下 }, // { // test: /\.html$/, // loader: 'html-loader', // options: { // minimize: false // } // } ] }, resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js' } }, node: { fs: 'empty' }, externals: { "BMap": "BMap", echarts: 'echarts', 'vue-ueditor-wrap': 'vue-ueditor-wrap', // 'element-ui': 'element-ui',//用了cdn引入这里直接不打包 // bootstrap:'bootstrap' }, devServer: { inline: true, //检测文件变化,实时构建并刷新浏览器 port: "8888", proxy: { '/api': { target: 'https://admintest.malls.iformall.com/api', // target: 'https://admin.malls.iformall.com/api', // target: 'https://admin.malls.iformall.com/api', pathRewrite: { "^/api": "" }, secure: false, changeOrigin: true }, }, // hot:true, disableHostCheck: true,//新版的webpack-dev-server修改了一些东西,默认检查hostname。如果hostname不是配置内的,将不可访问 //404 页面返回 index.html historyApiFallback: true, }, performance: { hints: false }, plugins: plugins, // devtool: '#eval-source-map'//开发模式下更方便定位错误 devtool: env.Generative ? false : '#eval-source-map' } }