让前端飞前端攻城狮程序员

webpack学习笔记

2018-02-13  本文已影响16人  tiancai啊呆

最近学习了webpack,并使用webpack搭建了一个自己的react脚手架,现在就理一下自己的学习过程,旨在让读者对webpack有一个大概的了解,具体细节还需查看官方文档。

let htmlWebpackPlugin = require("html-webpack-plugin");
let path = require('path')

let base = {
  entry: __dirname + "/src/index.js",
  output: {
    path: __dirname + "/build",
    filename: "[name]-[hash].js"
  },
  plugins: [
    new htmlWebpackPlugin({
      template: "src/index.html"
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader", "postcss-loader"],
        include: path.resolve(__dirname,"src")
      },
      {
        test: /\.scss$/,
        use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"],
        include: path.resolve(__dirname,"src")
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "postcss-loader", "less-loader"],
        include: path.resolve(__dirname,"src")
      },
      {
        test: /\.(png|jpg|svg|gif|jpeg)$/i,
        use: ["file-loader?name=[name]-[hash].[ext]"],
        include: path.resolve(__dirname,"src")
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: ["file-loader?name=[name]-[hash].[ext]"],
        include: path.resolve(__dirname,"src")
      },
      {
        test: /\.(jsx|js)$/,
        use: ["babel-loader"],
        include: path.resolve(__dirname,"src")
      }
    ]
  }
 
};
module.exports = base;

webpack.config.dev.js

let base = require("./webpack.config");
let webpack = require("webpack");
module.exports = Object.assign({},base,{
    devServer: {
        contentBase: __dirname + "/build",
        compress: true
    },
    devtool: "inline-source-map",
    plugins: base.plugins.concat([
        new webpack.HotModuleReplacementPlugin()
    ])
})

webpack.config.prod.js

let base = require("./webpack.config");
let webpack = require("webpack");
let cleanWebpackPlugin = require("clean-webpack-plugin");
module.exports = Object.assign({},base,{
   plugins: base.plugins.concat([
       new cleanWebpackPlugin("build/*.js"),
       new webpack.optimize.UglifyJsPlugin()
   ])
})
上一篇下一篇

猜你喜欢

热点阅读