vue3+ts复习第二天:使用webpack打包TS

## 下载依赖

yarn add -D typescript
yarn add -D webpack webpack-cli
yarn add -D webpack-dev-server
yarn add -D html-webpack-plugin clean-webpack-plugin
yarn add -D ts-loader
yarn add -D cross-env


## 入口JS: src/main.ts

```javascript
// import './01_helloworld'

document.write('Hello Webpack TS!')

index页面: public/index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>webpack & TS</title>
</head>
<body>
  
</body>
</html>

build/webpack.config.js

const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')

const isProd = process.env.NODE_ENV === 'production' // 是否生产环境

function resolve (dir) {
  return path.resolve(__dirname, '..', dir)
}

module.exports = {
  mode: isProd ? 'production' : 'development',
  entry: {
    app: './src/main.ts'
  },

  output: {
    path: resolve('dist'),
    filename: '[name].[contenthash:8].js'
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        include: [resolve('src')]
      }
    ]
  },

  plugins: [
    new CleanWebpackPlugin({
    }),

    new HtmlWebpackPlugin({
      template: './public/index.html'
    })
  ],

  resolve: {
    extensions: ['.ts', '.tsx', '.js']
  },

  devtool: isProd ? 'cheap-module-source-map' : 'cheap-module-eval-source-map',

  devServer: {
    host: 'localhost', // 主机名
    stats: 'errors-only', // 打包日志输出输出错误信息
    port: 8081,
    open: true
  },
}

配置打包命令

"dev": "cross-env NODE_ENV=development webpack-dev-server --config build/webpack.config.js",
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"

运行与打包

yarn dev
yarn build
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Vue 3项目中使用TypeScript和Webpack,并且正确地生成.d.ts声明文件,你需要按照以下步骤进行配置: 1. 安装依赖: ```bash npm install --save-dev typescript ts-loader vue-loader@next @types/vue @vue/compiler-sfc ``` 2. 创建`tsconfig.json`文件: 在项目根目录下创建一个名为`tsconfig.json`的文件,并添加以下内容: ```json { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "moduleResolution": "node", "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"], "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"] } ``` 3. 配置Webpack: 在Webpack配置文件中,添加对TypeScript和Vue文件的处理规则。例如,如果你使用的是`webpack.config.js`文件,可以按照以下示例进行配置: ```javascript const path = require('path'); module.exports = { // ...其他配置 resolve: { extensions: ['.js', '.ts', '.vue'], alias: { '@': path.resolve(__dirname, 'src') } }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/] } }, { test: /\.vue$/, loader: 'vue-loader' } ] }, // ...其他配置 }; ``` 4. 添加Vue文件声明: 在项目的`src`目录中,为Vue文件添加一个`.d.ts`的同名文件。例如,对于`HelloWorld.vue`组件,创建一个名为`HelloWorld.vue.d.ts`的文件,并添加以下内容: ```typescript declare module '*.vue' { import { ComponentOptions } from 'vue'; const componentOptions: ComponentOptions; export default componentOptions; } ``` 5. 构建项目: 运行Webpack构建命令,以启动TypeScript编译和生成声明文件: ```bash npm run build ``` 如果一切顺利,你的Vue 3 + TypeScript项目应该可以成功编译,并且在构建过程中会生成相应的.d.ts声明文件。 希望以上步骤可以帮助你配置Vue 3 + TypeScript + Webpack项目的.d.ts声明文件。如果有任何问题,请随时追问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值