TypeScript基础篇 - Vue-TS-Webpack 环境实战

目录

[Webpack+Vue+TS 环境配置](#Webpack+Vue+TS 环境配置)

scripts/webpack.config.js

src/Hello.tsx

src/SfcDemo.vue

src/main.tsx

src/shims-vue.d.ts

package.json


Webpack+Vue+TS 环境配置

scripts/webpack.config.js

javascript 复制代码
const path = require('path')
// 安装插件 npm install webpack webpack-cli @babel/core @babel/preset-env @babel/preset-typescript
// 安装vue3 npm install vue@next 
// 安装vue3对应的vue-loader // npm install vue-loader@next 
// 安装 npm install babel-loader -D
// npm install @vue/compiler-sfc -D // 用于编译文件组件用的
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
  entry: path.resolve(__dirname, "../src/main.tsx"),  // 入口文件
  mode: "development", // 开发模式
  module: {
    rules: [// 转义规则
      {
        test: /\.tsx?$/,
        loader: 'babel-loader',
        options: {
          presets: [
            "@babel/preset-env", // 转义es
            "@babel/preset-typescript"// 转义ts
          ],
          plugins: [
            "@vue/babel-plugin-jsx" // 用于转义vue jsx
          ],
        },
        // 排除
        exclude: /node_modules/
      },
      {
        // .vue的文件
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  resolve: { // import支持的扩展名
    extensions: [".tsx", ".ts", ".js", '.vue']
  },
  output: {// 打包后信息配置
    filename: 'bundle.js',
    path: path.resolve(__dirname, "../build")
  },
  devServer: {
    static: {// 打包静态文件配置
      directory: path.resolve(__dirname, "../build"),
    },
    port: 3020// 端口
  },
  plugins: [
    new HtmlWebpackPlugin({// 生成html文件模板
      template: path.resolve(__dirname, '../template.html')
    }),
    new VueLoaderPlugin()
  ]
}

src/Hello.tsx

javascript 复制代码
export default () => {
  return <div>Hello JSX</div>
}

src/SfcDemo.vue

html 复制代码
<template>
<div>Hello SFC</div>
</template>
<script>
export default {
  setup(){
    return {
    }
  }
}
</script>
<style>
</style>

src/main.tsx

javascript 复制代码
import { createApp } from 'vue'
// 让ts认.vue;需要一个垫片文件shims-vue.d.ts
import SfcDemo from './SfcDemo.vue'
import Hello from './Hello'
const App = () => {
  return <div>
    <SfcDemo />
    <Hello />
  </div>
}
createApp(App).mount("#root")

src/shims-vue.d.ts

javascript 复制代码
declare module '*.vue' { // 告诉ts 按【vue-component】 按下面的类型执行
  import { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

package.json

javascript 复制代码
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config scripts/webpack.config.js",
    "serve": "webpack serve --config scripts/webpack.config.js"
  },
相关推荐
掰头战士9 小时前
三道保险丝,最后只能放弃治疗? 一文聊聊我的agent是怎么做死循环检测的
typescript·llm·agent
2301_32241428049 小时前
活力孕康复APP 47737- 原创(免费领源码+部署教程+开发环境)
java·vue.js·spring boot·mysql·微信小程序·idea·微信开发者工具
专业程序开发源10 小时前
springbootLivehouse票务系统-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·课程设计·pygame
掰头战士10 小时前
AgentLoop: 从 while(true) 到生产级循环
typescript·llm·agent
leoZ23110 小时前
2026-09-10-静态扫描连错三次-用运行环境当裁判
前端·javascript·vue.js·人工智能·opencv·机器学习·数据挖掘
卓怡学长10 小时前
w233基于vue和springboot的宠物管理系统的设计与实现
java·vue.js·spring boot·spring·intellij-idea
梨涡泥窝13 小时前
JavaWeb——基于 Spring Boot + Vue 的图书管理系统的设计与实现
vue.js·spring boot·后端
Eric_见嘉13 小时前
打开这个 VSC 设置「组件引用位置」和数量就都清楚了
前端·typescript·visual studio code
guwentian13 小时前
Rolldown vs esbuild vs Webpack:Rust 打包器大战我们该怎么选
前端·webpack·rust·esbuild
今日无bug14 小时前
从 0 到 1 搭建端侧 AI 项目:DeepSeek-R1 + WebGPU + React + TS + Tailwind 全栈笔记
前端·react.js·typescript