Webpack3

var path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const webpack = require('webpack')
const ExtractPlugin = require('extract-text-webpack-plugin')
const isDev = process.env.NODE_ENV ==='development'
// const cleanWebpackPlugin = require('clean-webpack-plugin')
console.log(isDev)
const config = {
    target:'web',
    entry:path.join(__dirname,'src/main.js'),
    output:{
        filename:'bundle.[hash:8].js',
        path:path.join(__dirname,'dist')
    },
    module:{
        rules:[
            {
                test:/\.vue$/,
                loader:'vue-loader'
            },
            {
                test:/\.jsx$/,
                loader:'babel-loader'
            },
            {
                test:/\.css$/,
                use:[
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test:/\.(gif|jpg|jpeg|png|svg)$/,
                use:[
                    {
                        loader:'url-loader',
                        options:{
                            limit:1024,
                            name:'[name]-aaa.[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new htmlWebpackPlugin(),
        new webpack.DefinePlugin({
            'process.env':{
                NODE_ENV:isDev?'"development"':'"production"'
            }
        }),
        // new cleanWebpackPlugin()
    ],
}
if(isDev){
    config.module.rules.push({
        test:/\.styl$/,
        use:[
            'style-loader',
            'css-loader',
            {
                loader:'postcss-loader',
                options:{
                    sourceMap:true
                }
            },
            'stylus-loader'
        ]
    })
    config.devtool='#cheap-module-eval-source-map'
    config.devServer = {
        port:8000,
        host:'0.0.0.0',
        overlay:{
            errors:true
        },
        hot:true
        // historyFallback:{},
        // open:true
    }
    config.plugins.push(
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    )
}else{
    config.entry = {
        app:path.join(__dirname,'src/main.js'),
        vendor:['vue']
    }
    config.output.filename = '[name].[chunkhash:8].js'
    config.module.rules.push({
        test:/\.styl$/,
        use:ExtractPlugin.extract({
            fallback:'style-loader',
            use:[
                'css-loader',
                {
                    loader:'postcss-loader',
                    options:{
                        sourceMap:true
                    }
                },
                'stylus-loader'
            ]
        })
    })
    config.plugins.push(
        new ExtractPlugin('style.[chunkhash:8].css'),
        // new webpack.optimize.CommonsChunkPlugin({
        //     name:'vendor'
        // }),
        // new webpack.optimize.CommonsChunkPlugin({
        //     name:'runtime'
        // })
    )
    config.optimization={
        splitChunks: {
            name: "vendor"
        }
    }
    config.optimization={
        splitChunks: {
            name: "runtime"
        }
    }
}

module.exports = config

Webpack2

const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const HTMLPlugin = require('html-webpack-plugin')
const ExtractPlugin = require('extract-text-webpack-plugin')

const webpack = require('webpack')
const config = {
  target: 'web',
  entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.[hash:8].js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      // {
      //   test: /\.css$/,
      //   use: [
      //     'style-loader',
      //     'css-loader'
      //   ]
      // },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.jsx$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024,
              name: '[name].urloader.[ext]'
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: isDev ? '"development"' : '"production"'
      }
    }),
    new HTMLPlugin()
  ]
}

if (isDev) {
  config.module.rules.push({
    test: /\.styl$/,
    use: [
      'style-loader',
      'css-loader',
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true
        }
      },
      'stylus-loader'
    ]
  })
  config.devtool = '#cheap-module-eval-source-map'
  config.devServer = {
    port: 8000,
    host: '0.0.0.0',
    overlay: {
      errors: true
    },
    hot: true,
  }
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
} else {
  config.entry = {
    app: path.join(__dirname, 'src/index.js'),
    vendor: ['vue']
  }
  config.output.filename = '[name].[chunkhash:8].js'
  config.module.rules.push({
    test: /\.styl$/,
    use: ExtractPlugin.extract({
      fallback: 'style-loader',
      use: [
        'css-loader',
        {
          loader: 'postcss-loader',
          options: {
            sourceMap: true
          }
        },
        'stylus-loader'
      ]
    })
  })
  config.plugins.push(
    new ExtractPlugin('style.[contentHash:8].css'),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor'
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'runtime'
    })
  )
}

module.exports = config

Webpack1

var path = require('path')
var htmlWebpackPlugin = require('html-webpack-plugin');
var htmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin')
module.exports = {
    // entry:['./src/script/main.js','./src/script/a.js'],
    entry:'./src/app.js',
    output:{
        path:path.resolve(__dirname,'./dist'),
        filename:'js/[name]-[chunkhash].js',
        publicPath:'http://www.qinimai.com/'
    },
    module:{
        loaders:[
            {
                test:/\.js$/,
                loader:'babel-loader',
                exclude:path.resolve(__dirname,'./node_modules'),
                include:path.resolve(__dirname,'./src/'),
                query:{
                    presets:['latest']
                }
            },
            {
                test:/\.css/,
                use:[
                    'style-loader',
                    'css-loader?importLoaders:1',
                     'postcss-loader'
                ]
            },
            {
                test:/\.less$/,
                use:[
                    'style-loader',
                    'css-loader',
                    'postcss-loader',
                    'less-loader'
                ]
            },
            {
                test:/\.html$/,
                loader:'html-loader'
            },
            {
                test:/\.(pgn|jpg|gif|svg)/i,
                loaders:[
                    'file-loader',
                    'image-webpack-loader'
                ]
            }
        ]
    },
    postcss:[
        require('autoprefixer')({
            browsers:['last 5 versions']
        })
    ],
    plugins:[
        new  htmlWebpackPlugin({
            filename:'index.html',
            template:'index.html',
            inject:'body',
        })
    ]
}
var path = require('path')
var htmlWebpackPlugin = require('html-webpack-plugin');
var htmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
module.exports = {
    // entry:['./src/script/main.js','./src/script/a.js'],
    entry:{
        main:'./src/script/main.js',
        a:'./src/script/a.js',
        b:'./src/script/b.js',
        c:'./src/script/c.js'
    },
    output:{
        path:path.resolve(__dirname,'./dist'),
        filename:'js/[name]-[chunkhash].js',
        publicPath:'http://www.qinimai.com/'
    },
    plugins:[
        // new  htmlWebpackPlugin({
        //     filename:'index-[hash].html',
        //     template:'index.html',
        //     inject:'head',
        //     title:'a is good',
        //     minify:{
        //         removeComments:true,
        //         collapseWhitespace:true
        //     }
        // }),
        new  htmlWebpackPlugin({
            filename:'a.html',
            template:'index.html',
            inject:'head',
            title:'a is good',
            minify:{
                removeComments:true,
                collapseWhitespace:true
            },
            chunks:["main","a"],
            inlineSource:'.(main)'
        }),
        new  htmlWebpackPlugin({
            filename:'b.html',
            template:'index.html',
            inject:'head',
            title:'b is good',
            minify:{
                removeComments:true,
                collapseWhitespace:true
            },
            chunks:["main","b"],
            inlineSource:'.(main)'
        }),
        new  htmlWebpackPlugin({
            filename:'c.html',
            template:'index.html',
            inject:'head',
            title:'c is good',
            minify:{
                removeComments:true,
                collapseWhitespace:true
            },
            chunks:["main","c"],
            excludeChunks:["a","b"],
            inlineSource:'.(main)'
        }),
        new htmlWebpackInlineSourcePlugin()
    ]
}


上一篇:VUE学习笔记十六:高级路由

下一篇:VUE学习笔记十八:Vue配置二级目录