webpack配置

base

/* global require, module, __dirname */
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const config = require('../config')
const projectRoot = path.resolve(__dirname, '../')
const isProd = process.env.NODE_ENV === 'production'

module.exports = {
    performance: {
        maxEntrypointSize: 300000,
        hints: isProd ? 'warning' : false
    },
    context: path.join(__dirname, '../src'),
    entry: {
        'app': ['./app.js'],
        'admin': ['./admin.js'],
        'vendor': ['vue', 'vue-router', 'vuex-router-sync', 'axios', 'jquery', 'lodash', './assets/js/utils.js', 'nprogress/nprogress.css', 'jsoneditor/dist/jsoneditor.css', 'element-ui/lib/theme-chalk/index.css', 'vue-appui/packages/theme-default/lib/index.css', 'vue-appui/lib/app.css']
    },
    output: {
        path: config.build.assetsRoot,
        publicPath: config.assetsPublicPath,
        filename: 'js/[name].js',
        chunkFilename: 'js/[name].[id].chunk.js'
    },
    resolve: {
        extensions: [
            '.js', '.vue', '.less'
        ],
        modules: [
            path.join(__dirname, '../node_modules'),
        ],
        alias: {
            '~src': path.resolve(__dirname, '../src'),
            '~components': path.resolve(__dirname, '../src/components'),
            '~api': path.resolve(__dirname, '../src/api'),
            '~views': path.resolve(__dirname, '../src/views'),
            '~store': path.resolve(__dirname, '../src/store'),
            '~assets': path.resolve(__dirname, '../src/assets'),
            '~filters': path.resolve(__dirname, '../src/filters'),
            '~directives': path.resolve(__dirname, '../src/directives'),
            'vue': 'vue/dist/vue.common.js'
        }
    },
    resolveLoader: {
        modules: [
            path.join(__dirname, '../node_modules'),
        ]
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'eslint-loader',
                enforce: "pre",
                include: projectRoot,
                exclude: /node_modules/
            }, {
                test: /\.js$/,
                loader: 'eslint-loader',
                enforce: "pre",
                include: projectRoot,
                exclude: [/node_modules/, /dist\/js/]
            }, {
                test: /\.vue$/,
                loader: 'vue-loader'
            }, {
                test: /\.js$/,
                loader: 'babel-loader',
                include: projectRoot,
                exclude: /node_modules/
            }, {
                test: /\.json$/,
                loader: 'json-loader'
            }, {
                test: /\.html$/,
                loader: 'vue-html-loader'
            }, {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                query: {
                    limit: 10000,
                    name: 'staticimg/[name].[hash:7].[ext]'
                }
            }, {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                query: {
                    limit: 10000,
                    name: 'static/fonts/[name].[hash:7].[ext]'
                }
            },{
                test: /\.css$/,
                loader: ExtractTextPlugin.extract(['css-loader', 'postcss-loader'])
            },  {
                test: /\.less/,
                loader: ExtractTextPlugin.extract(['css-loader', 'postcss-loader', 'less-loader'])
            }         
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({'$': 'jquery', 'jQuery': 'jquery', 'window.jQuery': 'jquery', '_': 'lodash'}),
        new CopyWebpackPlugin([{from: '../src/assets/img', to: 'img'}]),
        new webpack.LoaderOptionsPlugin({
            minimize: isProd,
            options: {
                context: __dirname,
                vue: {
                        css: ExtractTextPlugin.extract({fallback: 'vue-style-loader', use: 'css-loader'}),
                        less: ExtractTextPlugin.extract({fallback: 'vue-style-loader', use: 'css-loader!less-loader'})
                    }
            }
        }),
    ]
}

 

dev

/* global require, module */
const path = require("path")
const webpack = require('webpack')
const merge = require('webpack-merge')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const config = require('../config')
const baseWebpackConfig = require('./webpack.base.config')


module.exports = merge(baseWebpackConfig, {
    // eval-source-map is faster for development
    devtool: '#cheap-module-eval-source-map',
    plugins: [
        new webpack.DefinePlugin({'process.env': {NODE_ENV: '"development"'} }),
        new FriendlyErrorsPlugin(),
        // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            names: ["vendor"]
        }),
         new ExtractTextPlugin({
             filename: 'css/[name].css',
             allChunks: true
        }),
        // https://github.com/ampedandwired/html-webpack-plugin
        new HtmlWebpackPlugin({
            chunks: [
                'vendor', 'app',
            ],
            filename: 'index.html',
            template: '../index.html',
            inject: true,
        }),
        new HtmlWebpackPlugin({
            chunks: [
                'vendor', 'admin',
            ],
            filename: 'admin.html',
            template: '../admin.html',
            inject: true,
        })
    ],
})

 

prod

/* global require, module, process */
var path = require("path")
var webpack = require('webpack')
var merge = require('webpack-merge')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
var config = require('../config')
var baseWebpackConfig = require('./webpack.base.config')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

function generateTemplate(title){
    var tpl =  '<!DOCTYPE html>' ;
    return tpl;
}

module.exports = merge(baseWebpackConfig, {
    output: {
        path: config.build.assetsRoot,
        publicPath: config.assetsPublicPath,
        filename: 'js/[name].[hash].js',
        chunkFilename: 'js/[name].[id].chunk.js'
    },
    devtool: config.build.productionSourceMap ? '#source-map' : false,
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),
        // extract webpack runtime and module manifest to its own file in order to
        // prevent vendor hash from being updated whenever app bundle is updated
        // new webpack.optimize.CommonsChunkPlugin({name: 'manifest', chunks: ['vendor']}),
    
        // new webpack.optimize.OccurenceOrderPlugin(),
        // extract css into its own file
        new ExtractTextPlugin({
             filename: 'css/[name].[hash].css',
             allChunks: true
        }),

        // new webpack.optimize.UglifyJsPlugin({
        //     compress: {
        //         warnings: false
        //     },
        //     ecma: 8
        // }),
        new UglifyJsPlugin({
            uglifyOptions: {
              compress: {
                warnings: false
              },
              ecma: 8
            },
            // sourceMap: config.build.productionSourceMap,
            // parallel: true
        }),
        new OptimizeCssAssetsPlugin({
            assetNameRegExp: /\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorOptions: { 
                discardComments: {removeAll: true },
                safe: true
            },
// 避免 cssnano 重新计算 z-index canPrint:
true }), // generate dist index.html with correct asset hash for caching. // you can customize output by editing /index.html // see https://github.com/ampedandwired/html-webpack-plugin new HtmlWebpackPlugin({ chunks: [ 'vendor', 'app' ], filename: '../../WEB-INF/views/manual.html', templateContent: generateTemplate(''), // 生成的文件放在哪里(相对于output.path的路径) inject: true, minify: { removeComments: true, // collapseWhitespace: true, removeAttributeQuotes: true } }), new HtmlWebpackPlugin({ chunks: [ 'vendor', 'admin' ], hashValue: '[hash]', filename: '../../WEB-INF/views/backend.html', templateContent: generateTemplate(''), inject: true, minify: { removeComments: true, // collapseWhitespace: true, removeAttributeQuotes: true } }) ] })

 

posted on 2019-03-18 16:57  natsu07  阅读(158)  评论(0编辑  收藏  举报