从零开始使用Webpack搭建vue项目(保姆级)

一、创建项目 

  1.新建项目文件夹,这里使用project-name代替
project-name
  2.在项目文件夹中执行npm init 生成 package.json
pnpm init  ##这里使用pnpm纯属个人习惯
{
  "name": "webpack-vue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
name         ##表示项目的名称 
version      ##表示项目的版本号
description  ##对项目的描述
main         ##项目的入口文件
script       ##脚本命令配置
author       ##作者(可以使用项目作者的名字)
license      ##证书
  3.新建 index.html webpack.confug.js文件

 目录结构:

 project-name
+ |- index.html
+ |- index.js
  |- package.json
+ |- webpack.config.js
 index.html作为入口文件进行使用,
<!-- index.html -->
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
        <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
        
        <script></script>
        <div id="app"></div>
    </body>
</html>
 在webpack.config.js文件中写入以下配置
// webpack.config.js
'use strict'

const path = require('path')

module.exports = {
  mode: 'development',
  // 配置入口文件 入口的值可以是一个字符串 也可以是一个对象
  entry: './index.js',
  output: {
    filename: 'index.js'     // 打包生成的js文件的名称
    publicPath: '/',         // 公共资源引入的路径
    path: path.resolve(__dirname, 'dist'), // 将打包生成的文件夹解析到dist目录中
  }
}

4.安装Webpack、Webpack-cli依赖包
pnpm install --save-dev webpack webpack-cli
5.修改package.json中的运行命令
{
  "name": "webpack-vue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
+   "build": "webpack --config webpack.config.js"
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

二、启动服务

使用 webpack-dev-server 来启动本地服务

1.安装webpack-dev-server
pnpm install --save-dev webpack-dev-server
2.修改 package.json
{
  "name": "webpack-vue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
+   "dev": "webpack serve",
    "build": "webpack --config webpack.config.js"
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
3.在webpack.config.js文件中添加运行端口号(默认端口号8080)
module.exports = {
 // ...
 devServer: {
   compress: true,
   port: 8080,
 },
};

三、生成HTML文件

使用 html-webpack-plugin 来生成 HTML 文件

1.安装html-webpack-plugin
pnpm install --save-dev html-webpack-plugin
2.在webpack.config.js文件中添加HtmlWebpackPlugin配置
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './index.html'
    })
  ]
}

四、安装vue相关依赖包

vue-loader、vue-template-compiler、

1.安装vue-loader、vue-template-compiler 
pnpm install --save-dev vue-loader vue-template-compiler

  也可以配置vue-router

pnpm install --save vue vue-router
2.在 webpack.config.js 中配置 vue-loader 用于引入 .vue 类型文件
const {VueLoaderPlugin} = require('vue-loader')

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: [
          {
            loader: 'vue-loader'
          }
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin()
  ]
}
3.路由组件容器、路由表
  新建一个 app.vue 文件作为路由组件的容器 
<!--  app.vue -->
<template>
    <router-view></router-view>
</template>

<script>
    export default {
        
    }
</script>
  创建路由表 index.js文件
// index.js
import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from "vue-router";

import { createApp } from "vue";
import App from './app.vue'


const routes =  [
  {
     path: "/",
     name: "index",
     component:  () => import('./index.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  mode: "hash",
  routes: routes
});
createApp(App).use(router).mount("#app");


4.新建一个 index.vue 文件作为首页
<template>
    <div>
      <h1>这是首页</h1>
    </div>
</template>

<script>
    export default {}
</script>

五、配置Babel

1.安装 @babel/core @babel/preset-env babel-loader
pnpm install --save-dev @babel/core @babel/preset-env babel-loader
2.在 webpack.config.js 中配置 babel-loader
module.exports = {
  // ...
  module: {
    //...
    rules: [
      // ...
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      }
    ]
  }
}
3.在根目录创建一个.babelrc 文件
// .babelrc
{
  "presets": ["@babel/preset-env"]
}

   参考文章链接:bebel中的preset-env

4. 运行项目
pnpm run dev

出现以下内容即可: 

 5.打包项目
pnpm run build

 出现以下内容即可:


 

以上就是基本的项目运行和配置

接下来webpack的优化以及处理css style sass请看:Webpack的打包大小和CSS style sass配置即优化


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于使用Webpack搭建Vue2项目,你可以按照以下步骤进行操作: 1. 确保你已经安装了Node.js和npm。你可以在终端中运行以下命令来验证它们是否已安装: ``` node -v npm -v ``` 如果没有安装,可以前往 Node.js 官方网站(https://nodejs.org/)下载并安装最新的稳定版本。 2. 创建一个新目录,作为你的项目根目录,并进入该目录。 3. 初始化一个新的npm项目,运行以下命令并按照提示进行操作: ``` npm init ``` 4. 在项目根目录下安装所需的依赖,包括VueVue Loader、Vue Router等。运行以下命令: ``` npm install vue@2 vue-loader@15 vue-router@3 webpack webpack-cli --save-dev ``` 5. 在项目根目录下创建一个`webpack.config.js`文件,并配置Webpack的基本设置。可以参考以下示例: ```javascript const path = require('path'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/, }, ], }, resolve: { alias: { vue$: 'vue/dist/vue.esm.js', }, extensions: ['*', '.js', '.vue', '.json'], }, }; ``` 6. 在项目根目录下创建一个`src`目录,并在其中创建一个`main.js`文件作为Vue应用的入口文件。 7. 在`src`目录下创建一个`components`目录,并在其中创建你的Vue组件。 8. 创建一个简单的Vue组件,并在`main.js`中引入该组件: ```javascript import Vue from 'vue'; import App from './components/App.vue'; new Vue({ render: (h) => h(App), }).$mount('#app'); ``` 9. 创建一个HTML文件作为你的应用的模板,例如`index.html`: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue App</title> </head> <body> <div id="app"></div> <script src="dist/bundle.js"></script> </body> </html> ``` 10. 在`package.json`文件中添加以下脚本命令: ```json "scripts": { "build": "webpack" } ``` 这将允许你在终端中运行`npm run build`命令来构建你的Vue项目。 11. 运行以下命令来构建和打包你的Vue项目: ``` npm run build ``` 这将生成一个`dist`目录,并将打包后的Vue应用文件输出到其中。 12. 打开你的HTML文件,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蜡笔小开心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值