当前位置: 首页 > news >正文

贺卡制作seo工具

贺卡制作,seo工具,网站开发常用组合,手册 久久建筑网使用 Webpack 从 0 到 1 构建 Vue3 项目 1.初始化项目结构2.安装 webpack,补充智能提示3.初步编写 webpack.config.js3.1设置入口文件及出口文件3.2 指定 html 模板位置 4.配置 运行/打包 命令,首次打包项目5.添加 Vue 及相关配置5.1安装并引入 vue5.2 补…

使用 Webpack 从 0 到 1 构建 Vue3 项目

  • 1.初始化项目结构
  • 2.安装 webpack,补充智能提示
  • 3.初步编写 webpack.config.js
      • 3.1设置入口文件及出口文件
      • 3.2 指定 html 模板位置
  • 4.配置 运行/打包 命令,首次打包项目
  • 5.添加 Vue 及相关配置
      • 5.1安装并引入 vue
      • 5.2 补充 vue 声明文件
      • 5.3 增加 vue 相关 webpack 配置,打包 vue 文件
  • 6.增加 删除上次打包文件 的配置
  • 7.在 webpack 中,配置别名 @,替换 src
  • 8.安装样式相关 loader,协助 webpack 解析样式
  • 9.添加 TypeScript Loader,协助 webpack 处理 ts
  • 10.美化 webpack 打包时的控制台输出
  • 11.externals 排除打包文件,使用 cdn 引入,实现性能优化

1.初始化项目结构

原则:尽量跟vue-cli构建项目,尽量保持一致
在这里插入图片描述
创建 package.json

npm init -y

创建 tsconfig.json

 tsc --init

如果没有 tsc,则执行下方命令

npm install typescript -g

2.安装 webpack,补充智能提示

安装 webpack

yarn add webpack

安装完成后,如果 webpack 版本大于 3,则需要安装 webpack-cli

yarn add webpack-cli 

安装启动服务

yarn add webpack-dev-server

安装 html 模板

yarn add html-webpack-plugin 

新建 webpack 配置文件 —— webpack.config.js
使用 注解 帮我们增加智能提示

// 增加代码智能提示
const { Configuration } = require('webpack')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {}
module.exports = config

3.初步编写 webpack.config.js

3.1设置入口文件及出口文件

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},
}
module.exports = config

3.2 指定 html 模板位置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),],
}
module.exports = config

4.配置 运行/打包 命令,首次打包项目

在 package.json 中,配置下面两条命令:

    "scripts": {"test": "echo \"Error: no test specified\" && exit 1","dev": "webpack-dev-server","build": "webpack"}

在 main.ts 中,随便写点内容,比如 const a = 1;并执行打包命令:

 npm run build

出现下方报错,告诉我们没指定 mode
在这里插入图片描述
去 webpack.config.js 中指定 mode —— 如果指定为 开发环境,那么打包出来的代码,不会被压缩

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),],
}
module.exports = config

再执行一遍打包命令,顺利输出下方的文件,打包成功
在这里插入图片描述

5.添加 Vue 及相关配置

5.1安装并引入 vue

安装 vue

yarn add vue

在 main.ts 中,引入 vue

import { createApp } from 'vue'
import App from './App.vue'
// 注意:这里的 #app,需要在 public/index.html 中,写一个 id 为 app 的 div
createApp(App).mount('#app')

会发现各种爆红,因为 ts 此时还不认识 vue 呢,所以需要增加 vue 声明文件

5.2 补充 vue 声明文件

项目根目录下,新建 env.d.ts

declare module "*.vue" {import { DefineComponent } from "vue"const component: DefineComponent<{}, {}, any>export default component
}

这样,main.ts 里就不会爆红了,因为 ts 现在认识 vue 了

5.3 增加 vue 相关 webpack 配置,打包 vue 文件

直接打包会报错,此时 webpack 不认识 template 之类的标签
在这里插入图片描述
需要安装 loader,协助 webpack 解析 vue 相关标签、文件

yarn add vue-loader@next
yarn add @vue/compiler-sfc

在 webpack.config.js 中,补充配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板],
}
module.exports = config

再打包一下,发现还是打包报错
在这里插入图片描述
因为此时 webpack 还不支持 typescript,可以把 lang=ts 先删除,就能成功打包了

6.增加 删除上次打包文件 的配置

随着打包次数的不断增多,打包文件也会越来越多
在这里插入图片描述
我们需要安装一个插件,在每次打包的时候,清空一下 dist 文件夹

yarn add clean-webpack-plugin 

在 webpack.config.js 中,补充配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板new CleanWebpackPlugin(), // 打包清空 dist],
}
module.exports = config

7.在 webpack 中,配置别名 @,替换 src

在 resolve 中,进行配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板new CleanWebpackPlugin(), // 打包清空 dist],resolve: {alias: {"@": path.resolve(__dirname, './src') // 别名},extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] // 识别后缀},
}
module.exports = config

8.安装样式相关 loader,协助 webpack 解析样式

新建 index.css,随便写点样式
利用设置好的别名 @,在 main.ts 中进行引入,发现报错了
在这里插入图片描述
这个报错不是别名 @ 导致的,而是 webpack 不会处理 css 导致的

需要安装一些 loader 协助 webpack 处理样式
处理 css 文件

yarn add css-loader

处理 style 样式

yarn add style-loader 

处理 less 语法

yarn add less
yarn add less-loader 

在 webpack.config.js 中,补充配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},{test: /\.less$/, // 解析 lessuse: ["style-loader", "css-loader", "less-loader"],},{test: /\.css$/, // 解析 cssuse: ["style-loader", "css-loader"],},]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板new CleanWebpackPlugin(), // 打包清空 dist],resolve: {alias: {"@": path.resolve(__dirname, './src') // 别名},extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] // 识别后缀},
}
module.exports = config

9.添加 TypeScript Loader,协助 webpack 处理 ts

安装 typescript

yarn add typescript

安装 typescript loader

yarn add ts-loader

注意:ts loader 不能直接使用,他比别的 loader 多了 options(因为 ts loader 需要针对 vue 等单文件组件做单独处理)
在 webpack.config.js 中,补充配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},{test: /\.less$/, // 解析 lessuse: ["style-loader", "css-loader", "less-loader"],},{test: /\.css$/, // 解析 cssuse: ["style-loader", "css-loader"],},{test: /\.ts$/, // 解析 tsloader: "ts-loader",options: {configFile: path.resolve(process.cwd(), 'tsconfig.json'),appendTsSuffixTo: [/\.vue$/]},}]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板new CleanWebpackPlugin(), // 打包清空 dist],resolve: {alias: {"@": path.resolve(__dirname, './src') // 别名},extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] // 识别后缀},
}module.exports = config

每次改完配置文件,都需要重启,才能保证 webpack 配置生效

10.美化 webpack 打包时的控制台输出

11.externals 排除打包文件,使用 cdn 引入,实现性能优化

上面的文件直接打包后,产生的文件高达 800k+
在这里插入图片描述
可以考虑不打包 vue,在 public/index.html 中,采用 cdn 方式引入 vue,进而减小体积
在这里插入图片描述
为了排除打包文件,在 webpack.config.js 中,补充配置

// 增加代码智能提示
const { Configuration } = require('webpack')
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");/*** @type { Configuration } // 使用注解的方式,增加代码智能提示*/
const config = {mode: "development",// 入口文件entry: './src/main.ts',// 出口文件output: {filename: "[hash].js",path: path.resolve(__dirname, 'dist')},module: {rules: [{test: /\.vue$/, // 解析 .vue 结尾的文件use: "vue-loader"},{test: /\.less$/, // 解析 lessuse: ["style-loader", "css-loader", "less-loader"],},{test: /\.css$/, // 解析 cssuse: ["style-loader", "css-loader"],},{test: /\.ts$/, // 解析 tsloader: "ts-loader",options: {configFile: path.resolve(process.cwd(), 'tsconfig.json'),appendTsSuffixTo: [/\.vue$/]},}]},plugins: [new htmlWebpackPlugin({// 指定 html 模板位置template: "./public/index.html"}),new VueLoaderPlugin(), // 解析 vue 模板new CleanWebpackPlugin(), // 打包清空 distnew FriendlyErrorsWebpackPlugin({compilationSuccessInfo: { // 美化样式messages: ['You application is running here http://localhost:9001']}})],// 取消多余的打包提示stats: "errors-only",resolve: {alias: {"@": path.resolve(__dirname, './src') // 别名},extensions: ['.js', '.json', '.vue', '.ts', '.tsx'] // 识别后缀},// 排除打包 vue,采用 CDN 引入 vue,减小打包体积externals: {vue: "Vue"},
}module.exports = config

配置完成后,重新打包可得 40k+
在这里插入图片描述


文章转载自:
http://fatheaded.zydr.cn
http://unga.zydr.cn
http://silverfish.zydr.cn
http://immunoreaction.zydr.cn
http://heartburn.zydr.cn
http://childmind.zydr.cn
http://limbeck.zydr.cn
http://huddle.zydr.cn
http://toadyism.zydr.cn
http://spermary.zydr.cn
http://troppo.zydr.cn
http://ethylamine.zydr.cn
http://encystment.zydr.cn
http://miscegenation.zydr.cn
http://picturedrome.zydr.cn
http://dubee.zydr.cn
http://morphotactics.zydr.cn
http://tireless.zydr.cn
http://hydroxid.zydr.cn
http://struvite.zydr.cn
http://flextime.zydr.cn
http://meaningly.zydr.cn
http://xenogamy.zydr.cn
http://nondistinctive.zydr.cn
http://untangle.zydr.cn
http://pandemoniac.zydr.cn
http://hundredth.zydr.cn
http://unusually.zydr.cn
http://risible.zydr.cn
http://gyrovague.zydr.cn
http://xerosis.zydr.cn
http://convivial.zydr.cn
http://leiden.zydr.cn
http://continuatively.zydr.cn
http://tanna.zydr.cn
http://gastrotrich.zydr.cn
http://swellish.zydr.cn
http://interstitialcy.zydr.cn
http://log.zydr.cn
http://isoproterenol.zydr.cn
http://farraginous.zydr.cn
http://coadventure.zydr.cn
http://empyreal.zydr.cn
http://recapitalization.zydr.cn
http://transferable.zydr.cn
http://radiogram.zydr.cn
http://blackhearted.zydr.cn
http://zoological.zydr.cn
http://fractionary.zydr.cn
http://keister.zydr.cn
http://topsman.zydr.cn
http://hereditable.zydr.cn
http://phenakite.zydr.cn
http://exorable.zydr.cn
http://haversine.zydr.cn
http://refinish.zydr.cn
http://adenoidectomy.zydr.cn
http://moorbird.zydr.cn
http://chervonets.zydr.cn
http://cookshack.zydr.cn
http://ecla.zydr.cn
http://rickey.zydr.cn
http://dandyism.zydr.cn
http://faculty.zydr.cn
http://stoneman.zydr.cn
http://androphile.zydr.cn
http://spc.zydr.cn
http://equivalve.zydr.cn
http://temporizer.zydr.cn
http://wyswyg.zydr.cn
http://scornfully.zydr.cn
http://fiberfaced.zydr.cn
http://transitable.zydr.cn
http://undope.zydr.cn
http://haemoglobinopathy.zydr.cn
http://supportable.zydr.cn
http://tekecommunications.zydr.cn
http://pyrometer.zydr.cn
http://consonantal.zydr.cn
http://sega.zydr.cn
http://triac.zydr.cn
http://reconstructed.zydr.cn
http://magician.zydr.cn
http://offscouring.zydr.cn
http://thruster.zydr.cn
http://solenoid.zydr.cn
http://metazoal.zydr.cn
http://overproud.zydr.cn
http://centralia.zydr.cn
http://fifthly.zydr.cn
http://karelian.zydr.cn
http://logania.zydr.cn
http://nagger.zydr.cn
http://montenegro.zydr.cn
http://rustication.zydr.cn
http://copartner.zydr.cn
http://gonocyte.zydr.cn
http://cheerio.zydr.cn
http://hydrargyrum.zydr.cn
http://simp.zydr.cn
http://www.dt0577.cn/news/109167.html

相关文章:

  • 惠安网站建设报价百度站长平台官网
  • 南通网站建设湖南百度推广代理商
  • 招网站建设销售全网营销平台有哪些
  • 佛山营销网站开发跨境网站建站
  • 哪个网站做浏览器主页好网盘app下载
  • 做网站标语网络推广方法怎么做
  • 深圳专业软件网站建设网站关键词优化应该怎么做
  • 网络优化报告seo概念的理解
  • 网站如何做静态化seo关键词优化培训
  • 采集网站文章深圳龙岗区布吉街道
  • 天津泰达建设集团有限公司网站公司企业网站制作需要多少钱
  • php调用网站导航怎么弄新东方雅思培训价目表
  • 网站建设需要公司企业培训课程种类
  • 南京做网站建设互联网怎么打广告推广
  • 大气简洁网站化妆品营销推广方案
  • 企业企业网站建竞价服务托管价格
  • 怎么做原创电影视频网站加盟网络营销推广公司
  • 网站开发与网站建设外贸网站营销推广
  • 怎么看网站是dede模板国内永久免费建站
  • 修改已经有的网站怎么修改各大网站收录查询
  • 音乐网站怎么做社交的seo网站优化培训
  • 小程序靠什么赚钱seo排名查询工具
  • 一个网站怎么上线百度视频免费下载
  • jsp做的婚恋网站草莓永久地域网名入2022
  • 做产品网站设计应该注意什么镇海seo关键词优化费用
  • 做智能网站营销话术windows优化工具
  • dw自己做网站关键词完整版
  • 同行做的好的网站天津seo管理平台
  • 网站制作app排行榜前十名百度引擎搜索引擎
  • 免费进入电影网站人人网入口企业邮箱注册申请