Webpack 搭建 Vue + ts + tsx

webpack5 + vue2.7

主要依赖包

  • webpack@5.74.0
  • webpack-cli@4.10.0
  • webpack-dev-server@4.11.0
  • vue@2.7.10
  • typescript@4.8.3
  • vue-loader@15.10.0 
  • @babel/core@7.17.5
  • @babel/preset-env@7.16.11
  • babel-loader@8.2.3
  • html-webpack-plugin@5.5.0
  • mini-css-extract-plugin@2.5.3
  • less@2.13.1
  • less-loader@7.3.0
  • css-loader@6.6.0

下面列出主要的配置。

Babel.config.js

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    chrome: 84
                }
            },
        ],
    ]
}

webpack.config.js

...
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },{
                test: /\.(j|t)sx?$/,
                exclude: /node_modules/,
                use:[
                    'babel-loader',
                    {
                        loader: 'ts-loader',
                        options: {
                            appendTsSuffixTo: [/\.vue$/],// 重要
                            transpileOnly: true,// 重要
                            happyPackMode: false,
                        }
                    }
                ]
            },
        ]
    },
    resolve: {
        extensions: ['.ts','.tsx', '.js','.vue'],
        ...
    }
...

tsconfig.json (同vue-cli 生成的,修改jsx:"react")

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "react", // preserve会报错,不知道vue-cli怎么做的
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "useDefineForClassFields": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

小结

主要是ts 和tsx 的处理。不用ts的情况下, 网上说用babel 的transform-vue-jsx 等插件完成jsx解析。

由于ts-loader 会处理jsx语法,在tsconfig.json 中配置 compilerOptions.jsx = 'react' 即可,设preserve会报错。要注意ts-loader 配置,appendTsSuffixTo 和 transpileOnly,不配置的话会报错。

因此,这里 babel 的配置很干净,没有处理有关 jsx 的 preset 和 plugin。

若报错webpack-env的类型问题,则npm i @types/webpack-env即可。

后续

使用swc 替换ts-loader 加速构建webpack-vue-tsx项目_JA+的博客-CSDN博客

其他

有的文章说:“ts-loader编译太慢!我不用 ts-loader!” 可以用 babel 配合 @babel/preset-typescript 预设来解析ts即可。(试了一下没成功)

通过

vue inspect > output.js

 命令导出vue-cli 的webpack配置,用于参考。

为了在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、付费专栏及课程。

余额充值