webpack dev & prod & common

webpack.dev.js

const webpack = require('webpack')
const merge = require('webpack-merge')
const commonConfig = require('./webpack.common')

const devConfig = {
  mode: "development",
  devtool: 'cheap-module-eval-source-map',//development模式推荐
  devServer: {
    contentBase: './dist',
    open: true,
    hot: true,
    hotOnly: true,
    port: 8888
  },
  optimization: {
    usedExports: true //模块中只引入使用了的方法(函数)
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ]
}

module.exports = merge(commonConfig, devConfig)

webpack.prod.js

const merge = require('webpack-merge')
const commonConfig = require('./webpack.common')
const prodConfig = {
  mode: "production",
  devtool: 'cheap-module-source-map',//development模式推荐
}

module.exports = merge(commonConfig, prodConfig)

webpack.common.js

var path = require("path")
var HtmlWebpackPlugin = require("html-webpack-plugin")
var { CleanWebpackPlugin } = require("clean-webpack-plugin")
module.exports = {
  entry: {
    main: "./src/index.js"
  },
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 2
              // modules: true 模块化
            }
          },
          "less-loader",
          "postcss-loader"
        ]
      },
      {
        test: /\.css$/,
        use: [
          "style-loader",
          "css-loader",
          "postcss-loader"
        ]
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  output: {
    filename: "[name].js", //以entry的key值作为打包生成的文件名
    path: path.resolve(__dirname, "dist")
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "my html template", //自定义title 默认为webpack app
      template: "src/index.html" //以此为模板创建html
    }),
    new CleanWebpackPlugin(),
  ]
}

你可能感兴趣的:(webpack dev & prod & common)