webpack升级(4.3.0)

vue-webpack4+

一.项目搭建

// 为节省安装时间,使用阿里定制的 cnpm 命令行工具代替默认的 npm
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
// 检测cnpm版本,如果安装成功可以看到cnpm的基本信息。
$ cnpm -v
// 安装时使用 cnpm install 即可
$ cnpm install 包的名称
// 当前电脑 node 和 npm 版本
cnpm@6.0.0 
npm@6.9.0
node@10.15.3 
  1. 创建 vue-webpack4 文件夹

  2. cmd 打开 vue-webpack4 文件夹

  3. 初始化 npm,然后在本地安装 webpack 四件套,

    npm init -y

    npm install webpack webpack-cli webpack-dev-server webpack-merge --save-dev

    • webpack@4.30.0

    • webpack-cli@3.3.1

    • webpack-dev-server@3.3.1

    • webpack-merge@4.2.1

  4. 创建相应文件

二. 基本 webpack 配置

  1. webpack.base.js

    /**
     * author: gleasonBian
     * date: 2019/4/30
     * 生产环境(prod)和开发环境(dev)通用配置
     */
    const webpack = require('webpack');
    ​
    module.exports = {
      entry: './src/main.js', //入口
      module: {
        rules: []
      },
      plugins: [
        // 解决vender后面的hash每次都改变
        new webpack.HashedModuleIdsPlugin(),
      ],
    };

     

  2. webpack.dev.js

    /**
     * author: gleasonBian
     * date: 2019/4/30
     * 开发环境配置
     */
    const merge = require('webpack-merge');
    const common = require('./webpack.base.js');
    const path = require('path');
    ​
    module.exports = merge(common, {
      devtool: 'inline-source-map',
      devServer: { // 开发服务器
        contentBase: '../dist'
      },
      output: { // 输出
        filename: 'js/[name].[hash].js', // 每次保存 hash 都变化
        path: path.resolve(__dirname, '../dist')
      },
      module: {},
      mode: 'development',
    });

     

  3. webpack.prod.js

    /**
     * author: gleasonBian
     * date: 2019/4/30
     * 生产环境配置
     */
    const path = require('path');
    // 合并配置文件
    const merge = require('webpack-merge');
    const common = require('./webpack.base.js');
    ​
    module.exports = merge(common, {
      module: {},
      plugins: [],
      mode: 'production',
      output: {
        filename: 'bundle/[name].[contenthash].js', //contenthash 若文件内容无变化,则contenthash 名称不变
        path: path.resolve(__dirname, '../dist')
      },
    });

     

三. 基本 vue 配置

  1. 安装 vue cli

    npm install -g @vue/cli

  2. 安装 vue

    npm install vue

    • vue@2.6.10

  3. 安装 vue 核心解析插件

    npm install vue-loader vue-template-compiler --save-dev

    • vue-loader@15.7.0

    • vue-template-compiler@2.6.10

  4. 安装 html 模板解析插件

    npm i html-webpack-plugin --save-dev

    • html-webpack-plugin@3.2.0

  5. 设置 入口文件 project/src/main.js

    import Vue from 'vue'
    ​
    import App from './App.vue'
    ​
    new Vue({
      el: '#app',
      render: h => h(App),
    });
    ​

     

  6. 配置主组件 project/src/App.vue

    <template>
      <div id="app">
        hello world
      </div>
    </template>
    ​
    <script>
    export default {
      name: 'app'
    }
    </script>
    ​
    <style scoped>
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
      transform: rotate(0deg);
    }
    </style>

     

  7. indexhtml

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Suporka Vue App</title>
      </head>
      <body>
        <div id="app"></div>
      </body>
    </html>

     

  8. 由于在开发和生产环境中都需要对vue文件的解析, 因此 解析 vue 配置在 webpack.base.js 中

    /**
     * author: gleasonBian
     * date: 2019/4/30
     * 生产环境(prod)和开发环境(dev)通用配置
     */
    const webpack = require('webpack');
    const path = require('path')
    // vue 核心解析插件
    const VueLoaderPlugin = require('vue-loader/lib/plugin');
    ​
    // html 模板解析插件
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    module.exports = {
      entry: './src/main.js', //入口
      module: {
        rules: [
          {
            test: /\.vue$/,
            loader: 'vue-loader'
          }
        ]
      },
      plugins: [
        // 解决vender后面的hash每次都改变
        new webpack.HashedModuleIdsPlugin(),
        // 请确保引入这个插件来施展魔法
        new VueLoaderPlugin(),
        // html 模板解析 插件
        new HtmlWebpackPlugin({
          template: path.resolve(__dirname, '../index.html'),
        })
      ],
    };

     

  9. package.json

code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: compression-webpack-plugin@3.1.0 npm ERR! Found: webpack@3.12.0 npm ERR! node_modules/webpack npm ERR! peer webpack@"^2.0.0 || ^3.0.0 || ^4.0.0" from @soda/friendly-errors-webpack-plugin@1.7.1 npm ERR! node_modules/@soda/friendly-errors-webpack-plugin npm ERR! @soda/friendly-errors-webpack-plugin@"^1.7.1" from @vue/cli-service@3.12.1 npm ERR! node_modules/@vue/cli-service npm ERR! dev @vue/cli-service@"^3.3.0" from the root project npm ERR! peer webpack@"2 || 3 || 4" from babel-loader@7.1.5 npm ERR! node_modules/babel-loader npm ERR! babel-loader@"^7.1.2" from vue-photo-preview@1.1.3 npm ERR! node_modules/vue-photo-preview npm ERR! vue-photo-preview@"^1.1.3" from the root project npm ERR! 11 more (eslint-loader, extract-text-webpack-plugin, ...) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer webpack@"^4.3.0 || ^5.0.0" from compression-webpack-plugin@3.1.0 npm ERR! node_modules/compression-webpack-plugin npm ERR! dev compression-webpack-plugin@"^3.1.0" from the root project npm ERR! npm ERR! Conflicting peer dependency: webpack@5.86.0 npm ERR! node_modules/webpack npm ERR! peer webpack@"^4.3.0 || ^5.0.0" from compression-webpack-plugin@3.1.0 npm ERR! node_modules/compression-webpack-plugin npm ERR! dev compression-webpack-plugin@"^3.1.0" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\zcybi\AppData\Local\npm-cache\_logs\2023-06-08T02_41_32_750Z-eresolve-report.txt
06-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gleason.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值