手动搭建webpack5 + ts + vue3项目
第一步 环境初始化
npm init -ynpm i webpack webpack-cli -Dnpm i webpack-dev-server -D
第二步 创建config目录
在根目录下创建config文件夹,
在config中创建三个文件:
webpack.config.js 公共文件
webpack.development.js 开发环境下的配置
webpack.production.js 生产环境下配置
第三步 创建测试 ts 文件,创建模板HTML
在根目录下创建src文件夹,在src下创建index.ts文件,如下
在根目录下创建public文件夹,在public下创建index.html文件, 如下
// src/index.ts文件
const num: number = 10
console.log(num)
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><!--public/index.html模板文件-->
</body>
</html>
结构如下:
第四步 安装babel,修改配置文件
//安装babelnpm i babel-loader @babel/core @babel/preset-env @babel/preset-typescript -Dnpm i html-webpack-plugin -D
然后根目录下创建 .babelrc文件,内容如下:
{"presets": ["@babel/preset-env","@babel/preset-typescript"]
}
修改package.json 中的scripts
{..."scripts": {"dev": "webpack serve --config ./config/webpack.config.js"},...
}
第五步 编辑 webpack.config.js
编辑config下的webpack.config.js文件,内容如下
const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {entry: './src/index.ts',output:{path: resolve(__dirname, '../dist/')},module: {rules: [{test: /\.ts$/,use: ['babel-loader']}]},plugins:[new HtmlWebpackPlugin({template: './public/index.html'})],mode: 'development',devServer: {port: 8000,open: true,hot: true},
}
运行 npm run dev 看是否能运行。有报错莫慌,看看报什么错,处理报错在往下
第六步 区分环境
区分环境
//minimist解析npm后面传递的参数
npm i minimist webpack-merge -D
修改 package.json
{..."scripts": {"dev": "webpack serve --config ./config/webpack.config.js --mode development","build": "webpack serve --config ./config/webpack.config.js --mode production"}...
}
修改webpack.config.js
const { resolve } = require('path');
const { merge } = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const argv = require('minimist')(process.argv.slice(2));
console.log(argv)
const { mode } = argv
const config = require(`./webpack.${mode}`);
const baseConfig = {entry: './src/index.ts',output:{path: resolve(__dirname, '../dist/')},module: {rules: [{test: /\.ts$/,use: ['babel-loader']}]},plugins:[new HtmlWebpackPlugin({template: './public/index.html'})],mode
}module.exports = merge(baseConfig, config)
修改 webpack.development.js
module.exports = {devServer: {port: 8000,open: true,hot: true},
}
webpack.production.js改为
module.exports = {}
再次 npm run dev 测试
第七步 配置vue
npm i vue@next vue-loader@next
npm i @vue/compiler-sfc -D
src下创建App.vue
<template><div id="App">App...</div>
</template>
挂载
修改src下的index.ts
import { createApp } from 'vue'
import App from './App.vue'const app = createApp(App)
app.mount('#root')
src下新创建shims-vue.d.ts文件
declare module '*.vue' {import { DefineComponent } from 'vue';const component: DefineComponent<{}, {}, any>;export default component;
}declare module '*.png';
根目录下创建typescript配置文件 tsconfig.json
{"compilerOptions": {"target": "esnext","module": "esnext","moduleResolution": "node","strict": false,"jsx": "preserve","sourceMap": true,"resolveJsonModule": true,"esModuleInterop": true,"lib": ["esnext", "dom"],},"include": ["src/**/*.ts","src/**/*.d.ts","src/**/*.tsx","src/**/*.vue"]
}
//日志显示运行时间
npm install friendly-errors-webpack-plugin --save-dev
配置 webpack,development.js
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
module.exports = {devServer: {port: 8000,open: true,hot: true},plugins: [new FriendlyErrorsWebpackPlugin()]
}
//打包进度条
npm i webpackbar -D
webpack.config.js修改
const { resolve } = require('path');
const { merge } = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const argv = require('minimist')(process.argv.slice(2));
console.log(argv)
const { mode } = argv
const config = require(`./webpack.${mode}`);
const WebpackBar = require('webpackbar')
const baseConfig = {entry: './src/index.ts',output:{path: resolve(__dirname, '../dist/')},cache: {type: 'filesystem'},module: {rules: [{test: /\.ts$/,use: ['babel-loader']},{test: /\.vue$/,use: ['vue-loader']}]},plugins:[new HtmlWebpackPlugin({template: './public/index.html'}),new VueLoaderPlugin(),new WebpackBar()],mode
}module.exports = merge(baseConfig, config)
npm run dev
到此结束
有问题评论区。。。
手动搭建webpack5 + ts + vue3项目相关推荐
- 手动搭建一个Spring Boot项目
Spring Boot如果在电脑联网的情况下可以使用在线插件进行自动搭建,没有插件的情况可以手动搭建. 以下是eclispe手动搭建的流程: Eclipse→File→New→Maven Projec ...
- 搭建react项目 搭建ts react项目 使用vite搭建react项目
创建react 项目 注意: 网上有一些生成react 的方法,但是也有一些是过时的. 使用官方脚手架creact-react-app 全局安装 creact-react-app 这个脚手架 这个脚手 ...
- 如何搭建一个完整的Vue3.0 + ts 的项目
如何搭建一个完整的Vue3.0 + ts 的项目 相信9月18日尤大大的关于Vue3.0的发表演讲大家一定有所关注,现在Vue3.0 也已经进入RC阶段(最终产品的候选版本,如果没有问题则可发布成为正 ...
- vite+vue3+ts+element-plus项目搭建--超详细
vite+vue3+ts+element-plus项目搭建–超详细 vite 作用 快速的冷启动:不需要等待打包操作: 即时的热模块更新:替换性能和模块数量的解耦让更新飞起: 真正的按需编译:不再等待 ...
- 【Parcel 2 + Vue 3】从0到1搭建一款极快,零配置的Vue3项目构建工具
前言 一周时间,没见了,大家有没有想我啊!哈哈!我知道肯定会有的.言归正传,我们切入正题.上一篇文章中我主要介绍了使用Vite2+Vue3+Ts如何更快的入手项目.那么,今天我将会带领大家认识一个新的 ...
- vue3 +vite+ts实战项目添加 eslint + prettier + lint-staged 踩坑指南
初始化项目 // 创建一个空的 vue3-ts 项目, yarn create vite my-vue-app --template vue-ts // 安装依赖 cd my-vue-app & ...
- 教你使用 koa2 + vite + ts + vue3 + pinia 构建前端 SSR 企业级项目
大厂技术 高级前端 Node进阶 点击上方 程序员成长指北,关注公众号 回复1,加入高级Node交流群 前言 大家好,我是 易[1],在上一篇文章中,我们有讲到<如何使用 vite+vue3 ...
- Vue3项目搭建学习笔记
Vue3项目搭建学习笔记 参考王红元老师vue课程 创建项目 vue create vue3-ts-cms 项目搭建规范 集成editorconfig配置 VSCode需要安装一个插件:EditorC ...
- Vue3项目搭建全过程
目录 一.前言 二.搭建准备 三.搭建项目 四.启动项目 一.前言 在2020年的9月19日,万众期待的Vue3终于发布了正式版,命名为"One Piece". 它也带来了很多新的 ...
最新文章
- python爬虫用到的一些东西
- python调用zabbixapi接口_python3 + zabbix api 的使用
- while循环打印*菱形
- MongoDB数据库设计中6条重要的经验法则
- html获取一条一条的li,html – 在一个li中定位一个span
- mysql乱码utfmb4_MySQL乱码问题以及utf8mb4字符集
- Tomcat服务器安装配置与web服务器介绍
- 初级第四旬06— 回向与发愿试题
- python培训班-Python培训班哪家好?
- T^T找数字(搜索+二进制枚举)
- JAVA在线考试系统(本科毕业设计)
- 数字光栅投影技术——相移轮廓术(PSP)
- 小米手机是什么系统?
- 能否用一种语言来培养编程思维以及养成世界观
- 程序员必备:常见的安卓开发工具推荐
- 高清精美壁纸:2013年4月桌面日历壁纸免费下载
- STARK中的FRI代码解析
- Android开发从零开始
- linux安全之系统安全
- 【vue3 + vite】: Invalid VNode type: undefined (undefined)