首页 > Web开发 > 详细

webapck打包上线

时间:2021-02-24 15:21:53      阅读:29      评论:0      收藏:0      [点我收藏+]

webapck打包上线

 

npm i webpack -g 

npm i webpack-cli -g

 

 

const webpack = require(‘webpack‘);
const HtmlWebpackPlugin= require(‘html-webpack-plugin‘); //1.引入插件
const {CleanWebpackPlugin} = require(‘clean-webpack-plugin‘);
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require(‘path‘);
module.exports = {
  mode: "development", //模式
  entry: "./src/index.js", //入口
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dists")
  },
  module: {
    rules: [
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          //"style-loader", //把css模块插入到页面
          "css-loader", //把css文件转换为webpack可以识别的js模块
          "sass-loader" //把scss 转换 css
        ]
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/, //icon
        use: ["file-loader"]
      },
      // {
      //   test: /\.(jpg|jpeg|gif)$/, //打包图片
      //   use: ["file-loader"],

      // }
      {
        test: /\.(jpg|jpeg|gif)$/, //打包图片
        use: [
          {
            loader: "file-loader",
            options: {
              //[name] 占位符
              name: "[name].[ext]"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html" //以index.html为模板创建html
    }), //2.调用插件
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin()
  ]
};
 
 
pageack.json
{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "webpack": "webpack --config webpack.config.js --progress --display-modules ---colors"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.4.2",
    "style-loader": "^1.1.3"
  },
  "devDependencies": {
    "file-loader": "^5.1.0",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.1",
    "sass-loader": "^8.0.2",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12"
  }
}
 
 
 
终端运行代码
  "webpack": "webpack --config webpack.config.js --progress --display-modules ---colors"
npm run webapck
 

(1)问题1: webpack-cli不存在

解决方案,执行命令:npm install webpack-cli -g

webapck打包上线

原文:https://www.cnblogs.com/fyh0912/p/14441217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!