美文网首页
记录一次webpack下开发多页面h5

记录一次webpack下开发多页面h5

作者: huanghaodong | 来源:发表于2019-09-30 17:15 被阅读0次

项目目录

WX20190930-171117@2x.png

src/static文件存放无需打包,在html直接引用的静态资源(eg. zepot.js)

package.json

{
  "name": "webPay",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/plugin-transform-runtime": "^7.6.2",
    "@babel/preset-env": "^7.6.2",
    "babel-loader": "^8.0.6",
    "clean-webpack-plugin": "^3.0.0",
    "copy-webpack-plugin": "^5.0.3",
    "css-loader": "^3.1.0",
    "file-loader": "^4.2.0",
    "html-webpack-plugin": "^3.2.0",
    "html-withimg-loader": "^0.1.16",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.12.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "sass-loader": "^8.0.0",
    "url-loader": "^2.1.0",
    "webpack": "^4.41.0",
    "webpack-cli": "^3.3.9",
    "webpack-dev-server": "^3.8.1"
  },
  "dependencies": {}
}

webpack.config.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  entry: {
    // 多入口文件
    index: ['./src/pages/index/index.js',],
    authorize: './src/pages/authorize/index.js',
  },
  output: {
    path: resolve('dist'),
    filename: 'static/[name].[chunkhash].js',
    publicPath: '',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'], //兼容普通es语法
            plugins: ["@babel/plugin-transform-runtime"] //兼容更多新es语法
          }
        }
      },
      {
      test: /\.(htm|html)$/i,
      loader: 'html-withimg-loader'
      },
      {
        test:/\.(jpg|png|gif|svg)$/,
        //小于1024的图片都用base64的方式加载
        loader: 'url-loader',
        options: {
            limit: 1024,
              outputPath:'images/'
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          'css-loader',
        ],
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          'css-loader',
          'sass-loader'
        ],
      },
    ],
  },
  plugins: [
    //打包css到指定文件
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // all options are optional
      filename: 'static/[name].[hash].css',
      chunkFilename: 'static/[id].[hash].css',
      ignoreOrder: false, // Enable to remove warnings about conflicting order
    }),
    //压缩css
    new OptimizeCSSPlugin({
      cssProcessorOptions: {
        safe: true
      }
    }),
    //html打包
    new HtmlWebpackPlugin({
      template: './src/pages/index/index.html',
      filename: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      chunks:['index'],
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    new HtmlWebpackPlugin({
      template: './src/pages/authorize/index.html',
      filename: 'authorize.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      chunks:['authorize'],
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, 'src/static'),
        to: 'static',
        ignore: ['.*']
      }
    ]),
    //删除dist文件夹
    new CleanWebpackPlugin(),
  ]
}

相关文章

网友评论

      本文标题:记录一次webpack下开发多页面h5

      本文链接:https://www.haomeiwen.com/subject/uylhpctx.html