webpack学习

webpack主要部分:
1.入口;
2.出口;
3.模块;
4.加载器,例:(sass/less);
5.解析其他文件

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',  // 入口文件
  output: {                // 输出文件
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {                // 定义模块
    rules: [
      {
        test: /\.css$/,
        use: [             // 加载顺序,右往左
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        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: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {    // 别名
      'vue$': 'vue/dist/vue.esm.js'
    },            // 拓展名
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {    // 开发服务
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {  // 性能
    hints: false      // 提示 取消
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html

  // 插件配置
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,    // 映射调试
      compress: {
        warnings: false     // 去除警告
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true      // 最小化
    })
  ])
}

你可能感兴趣的:(webpack学习)