先创建一个空目录,在该目录打开命令行,执行 npm init 命令创建一个项目(无法执行 npm 命令?需要先安装 Node),这个过程会提示输入一些内容,随意输入就行,完成后会自动生成一个 package.json 文件,里面包含刚才输入的内容

创建一个 index.html 页面,由于使用的是 Vue 开发单页应用,所以通常一个 html 文件就够了,内容也很简单,就一个 div#app

project

  1. project-name
  2. + |- index.html
  3. |- package.json

index.html

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>这是标题</title>
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. </body>
  12. </html>

project

  1. project-name
  2. |- index.html
  3. + |- index.js
  4. |- package.json
  5. + |- webpack.config.js

创建一个 index.js 作为项目的主入口,创建一个 webpack.config.js 文件作为 Webpack 的配置文件,内容如下

webpack.config.js

  1. 'use strict'
  2. const path = require('path')
  3. module.exports = {
  4. mode: 'development',
  5. entry: './index.js',
  6. output: {
  7. filename: 'index.js',
  8. path: path.resolve(__dirname, 'dist')
  9. }
  10. }

执行 npm install --save-dev webpack-cli 安装 Webpack

在 package.json 文件对应的 scripts 处写入命令

package.json

  1. {
  2. "scripts": {
  3. + "build": "webpack"
  4. }
  5. }

执行 npm run build 即可完成打包,打包成功后的文件放在 dist 目录里面(这是由配置文件自定义的),目前打包出来的只有一个 index.js 文件

使用 webpack-dev-server 来启动本地服务,方便开发以及本地调试

执行 npm install --save-dev webpack webpack-dev-server

在 package.json 文件对应的 scripts 处写入命令

package.json

  1. {
  2. "scripts": {
  3. + "dev": "webpack-dev-server",
  4. "build": "webpack"
  5. }
  6. }

执行 npm run dev 即可启动本地服务,访问 localhost:8080 即可,8080 是默认的端口号,修改端口号配置如下

webpack.config.js

  1. module.exports = {
  2. // ...
  3. devServer: {
  4. compress: true,
  5. port: 8080
  6. }
  7. }

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

执行 npm install --save-dev html-webpack-plugin

在 webpack.config.js 配置文件中添加

webpack.config.js

  1. const HtmlWebpackPlugin = require('html-webpack-plugin')
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. new HtmlWebpackPlugin({
  6. filename: 'index.html',
  7. template: './index.html'
  8. })
  9. ]
  10. }

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

执行 npm install --save vue vue-router

在 webpack.config.js 中配置 vue-loader 用于引入 .vue 类型文件

webpack.config.js

  1. const VueLoaderPlugin = require('vue-loader/lib/plugin')
  2. module.exports = {
  3. // ...
  4. module: {
  5. rules: [
  6. {
  7. test: /\.vue$/,
  8. use: [
  9. {
  10. loader: 'vue-loader'
  11. }
  12. ]
  13. }
  14. ]
  15. },
  16. plugins: [
  17. new VueLoaderPlugin()
  18. ]
  19. }

新建一个 app.vue 文件作为路由组件的容器

project

  1. project-name
  2. + |- app.vue
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. |- webpack.config.js

app.vue

  1. <template>
  2. <router-view></router-view>
  3. </template>
  4. <script>
  5. export default {}
  6. </script>

index.js

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import appView from 'app.vue'
  4. Vue.use(VueRouter)
  5. const router = new VueRouter({
  6. routes: [
  7. {
  8. path: '/',
  9. component: require('./index.vue').default
  10. }
  11. ]
  12. })
  13. new Vue({
  14. el: '#app',
  15. router,
  16. render(h) { return h(appView) }
  17. })

新建一个 index.vue 文件作为首页

project

  1. project-name
  2. |- app.vue
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. + |- index.vue
  7. |- webpack.config.js

index.vue

  1. <template>
  2. <div>
  3. <h1>这是首页</h1>
  4. </div>
  5. </template>
  6. <script>
  7. export default {}
  8. </script>

添加一个 about.vue 文件作为关于页

project

  1. project-name
  2. + |- about.vue
  3. |- app.vue
  4. |- index.html
  5. |- index.js
  6. |- package.json
  7. |- index.vue
  8. |- webpack.config.js

about.vue

  1. <template>
  2. <div>
  3. <h1>这是关于页</h1>
  4. </div>
  5. </template>
  6. <script>
  7. export default {}
  8. </script>

配置关于页的路由

index.js

  1. // ...
  2. const router = new VueRouter({
  3. routes: [
  4. {
  5. path: '/',
  6. component: require('./index.vue').default
  7. },
  8. {
  9. path: '/about',
  10. component: require('./about.vue').default
  11. },
  12. ]
  13. })

访问 http://localhost:8080/#/about 即可显示关于页

随着页面的增加,vue 文件将会越来越多,放在项目根目录下面并不科学,在当前目录创建一个 src 目录用来放置开发源文件

在 src 目录中创建一个 pages 目录用来放置 vue 页面文件,将 app.vue、index.vue、about.vue 文件移入 pages 目录中,同时修改对应的引用路径

project

  1. project-name
  2. - |- about.vue
  3. - |- app.vue
  4. |- index.html
  5. |- index.js
  6. |- package.json
  7. - |- index.vue
  8. |- webpack.config.js
  9. + |- /src
  10. + |- /pages
  11. + |- about.vue
  12. + |- app.vue
  13. + |- index.vue

index.js

  1. // ...
  2. import appView from './src/pages/app.vue'
  3. const router = new VueRouter({
  4. routes: [
  5. {
  6. path: '/',
  7. component: require('./src/pages/index.vue').default
  8. },
  9. {
  10. path: '/about',
  11. component: require('./src/pages/about.vue').default
  12. },
  13. ]
  14. })

./src/pages/index.vue 这种长路径写起比较麻烦,在 webpack.config.js 中配置一个 alias 参数

webpack.config.js

  1. module.exports = {
  2. // ...
  3. resolve: {
  4. alias: {
  5. '@': path.join(__dirname, 'src')
  6. }
  7. }
  8. }

上面的页面路径可以再次改写

index.js

  1. // ...
  2. import appView from '@/pages/app.vue'
  3. const router = new VueRouter({
  4. routes: [
  5. {
  6. path: '/',
  7. component: require('@/pages/index.vue').default
  8. },
  9. {
  10. path: '/about',
  11. component: require('@/pages/about.vue').default
  12. },
  13. ]
  14. })

同时,将路由配置单独提取出来,新建一个 routes.js 文件放在 src/js 目录中(js 目录需要新建)

project

  1. project-name
  2. |- index.html
  3. |- index.js
  4. |- package.json
  5. |- webpack.config.js
  6. |- /src
  7. + |- /js
  8. + |- routes.js
  9. |- /pages
  10. |- about.vue
  11. |- app.vue
  12. |- index.vue

routes.js

  1. module.exports = [
  2. {
  3. path: '/',
  4. component: require('@/pages/index.vue').default
  5. },
  6. {
  7. path: '/about',
  8. component: require('@/pages/about.vue').default
  9. },
  10. ]

index.js

  1. // ...
  2. import routes from '@/js/routes'
  3. const router = new VueRouter({
  4. routes
  5. })

由于前面的代码使用了 ES2015 的语法,为了使项目兼容更多浏览器,需要用 Babel 对代码进行转换

执行 npm install --save-dev @babel/core @babel/preset-env babel-loader

webpack.config.js

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. exclude: /node_modules/,
  8. use: [
  9. {
  10. loader: 'babel-loader'
  11. }
  12. ]
  13. }
  14. ]
  15. }
  16. }

创建一个 .babelrc 文件(不知道怎么创建?可以直接从该项目中复制)

project

  1. project-name
  2. + |- .babelrc
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. |- webpack.config.js
  7. ...

.babelrc

  1. {
  2. "presets": ["@babel/preset-env"]
  3. }

项目中肯定会用到 CSS,首先新建一个 style.css 样式文件,项目中的样式就可以写在这里面

project

  1. project-name
  2. |- .babelrc
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. + |- style.css
  7. |- webpack.config.js
  8. ...

然后安装 Normalize.css 用于使各种浏览器呈现一致的效果,这只是一种样式初始化方案,是可选的,另外也可以选择 Bootstrap 或者 Bulma 等包含更多样式的样式库来作为开发的基础

执行 npm install --save normalize.css

直接在 index.js 里面引用

index.js

  1. import 'normalize.css'
  2. import './style.css'
  3. // ...

由于这里直接在 js 文件中引用了 css 文件,所以需要 css-loader 来处理

执行 npm install --save-dev css-loader style-loader

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /\.css$/,
  6. use: [
  7. {
  8. loader: 'style-loader'
  9. },
  10. {
  11. loader: 'css-loader'
  12. }
  13. ]
  14. }
  15. ]
  16. }
  17. }

另外也可以在 vue 文件里面写 CSS

index.vue

  1. <template>
  2. <div>
  3. <h1>这是首页</h1>
  4. </div>
  5. </template>
  6. <script>
  7. export default {}
  8. </script>
  9. <style>
  10. h1 {
  11. text-align: center;
  12. }
  13. </style>

两种写样式的方式可以根据具体需求选择使用

上面引入 css 的方式最终打包之后 CSS 代码都在 js 里面,为了网站的性能需要将 CSS 单独提取出来,使用 mini-css-extract-plugin 插件来提取 CSS

执行 npm install --save-dev mini-css-extract-plugin

webpack.config.js

  1. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  2. module.exports = {
  3. // ...
  4. module: {
  5. rules: [
  6. {
  7. test: /\.css$/,
  8. use: [
  9. {
  10. loader: MiniCssExtractPlugin.loader // 代替 style-loader
  11. },
  12. {
  13. loader: 'css-loader'
  14. }
  15. ]
  16. }
  17. ]
  18. },
  19. plugins: [
  20. new MiniCssExtractPlugin({
  21. filename: `[name].css`
  22. })
  23. ]
  24. }

项目中如果有用到图片需要 file-loader 来处理

执行 npm install --save-dev file-loader

webpack.config.js

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.(png|jpe?g|gif)$/i,
  7. loader: 'file-loader'
  8. }
  9. ]
  10. }
  11. }

准备一张图片 logo.gif 放在 src/images 目录中(images 目录需要新建,这张图片是用来测试的)

project

  1. project-name
  2. |- .babelrc
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. |- style.css
  7. |- webpack.config.js
  8. |- /src
  9. + |- /images
  10. + |- logo.gif
  11. |- /js
  12. |- routes.js
  13. |- /pages
  14. |- about.vue
  15. |- app.vue
  16. |- index.vue

index.vue

  1. <template>
  2. <div>
  3. <h1>这是首页</h1>
  4. <img src="@/images/logo.gif">
  5. </div>
  6. </template>
  7. <script>
  8. export default {}
  9. </script>
  10. <style>
  11. h1 {
  12. text-align: center;
  13. }
  14. </style>

执行 npm run build 打包后发现图片已经成功打包进来了,但是图片的名称改变了,如果不希望改变图片名称,可以给 file-loader 配置参数

webpack.config.js

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.(png|jpe?g|gif)$/i,
  7. loader: 'file-loader',
  8. options: {
  9. name: 'images/[name].[ext]'
  10. }
  11. }
  12. ]
  13. }
  14. }

使用 cssnano 压缩 CSS,该插件属于 PostCSS 生态系统,所以需要同时安装 postcss-loader

执行 npm install --save-dev cssnano postcss-loader

创建一个 postcss.config.js 文件,这是 PostCSS 的配置文件,相关配置都写在这里面

project

  1. project-name
  2. |- .babelrc
  3. |- index.html
  4. |- index.js
  5. |- package.json
  6. + |- postcss.config.js
  7. |- style.css
  8. |- webpack.config.js
  9. ...

postcss.config.js

  1. module.exports = {
  2. plugins: {
  3. 'cssnano': {
  4. safe: true
  5. }
  6. }
  7. }

webpack.config.js

  1. module.exports = {
  2. // ...
  3. module: {
  4. rules: [
  5. {
  6. test: /\.css$/,
  7. use: [
  8. {
  9. loader: MiniCssExtractPlugin.loader
  10. },
  11. {
  12. loader: 'css-loader'
  13. },
  14. {
  15. loader: 'postcss-loader'
  16. }
  17. ]
  18. }
  19. ]
  20. }
  21. }

这里使用 postcss-preset-env 来预处理 CSS(也可以选择使用 Sass 或者 Less 等)

执行 npm install --save-dev postcss-preset-env

该插件也属于 PostCSS 生态系统,直接在 postcss.config.js 里增加配置即可

postcss.config.js

  1. module.exports = {
  2. plugins: {
  3. + 'postcss-preset-env': {},
  4. 'cssnano': {
  5. + autoprefixer: false, // 这里两个插件都包含了 autoprefixer,只执行其中一个就行
  6. safe: true
  7. }
  8. }
  9. }

使用 Axios 发送 HTTP 请求,Axios 基于 Promise,所以同时安装 es6-promise polyfill

执行 npm install --save axios es6-promise

index.js

  1. + import 'es6-promise/auto'
  2. + import axios from 'axios'
  3. // ...

在项目中发送一个请求

index.js

  1. import 'es6-promise/auto'
  2. import axios from 'axios'
  3. + axios.post('/login')
  4. // ...

运行后这个请求明显会返回一个 404,那么如何让它返回有效的数据呢,在 webpack.config.js 里配置 devServer 参数

webpack.config.js

  1. module.exports = {
  2. // ...
  3. devServer: {
  4. + before(app, server) {
  5. + app.post('/login', (req, res) => {
  6. + res.json({success: true})
  7. + })
  8. + },
  9. compress: true,
  10. port: 8080
  11. }
  12. }

重新启动后,就可以看到请求 /login 地址返回了数据 {"success": true},这样就可以在本地调试接口了

当然,所有接口都这样写未免麻烦,可以用 proxy 参数将请求接口代理到其它地址去

webpack.config.js

  1. module.exports = {
  2. // ...
  3. devServer: {
  4. before(app, server) {
  5. app.post('/login', (req, res) => {
  6. res.json({success: true})
  7. })
  8. },
  9. + proxy: {
  10. + '/api': {
  11. + target: 'http://localhost:3000'
  12. + }
  13. + },
  14. compress: true,
  15. port: 8080
  16. }
  17. }

这时,例如请求 /api/posts 实际上会被代理到 http://localhost:3000/api/posts

配置 mode 参数

webpack.config.js

  1. module.exports = {
  2. mode: 'production'
  3. // ...
  4. }

productiondevelopment 两种 mode 参数很明显,production 用于发布,development 用于开发,具体有什么区别,看这里 Click here

执行 npm run build 即可打包,打包后生成的文件都在 dist 目录中

版权声明:本文为xyzhanjiang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xyzhanjiang/p/11590778.html