webpack如何构建一个TS+Vue环境的项目?

一、整体思路

在这里插入图片描述
在这里插入图片描述

二、效果展示

(一)、package.json配置文件

在这里插入图片描述

(二)、package.json文件


{
  "name": "03TS-Project1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node_modules\\.bin\\webpack --mode production",
    "serve": "node_modules\\.bin\\webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^6.2.2",
    "file-loader": "^3.0.1",
    "url-loader": "^1.1.2",
    "css-loader": "^2.1.1",
    "style-loader": "^0.23.1",
    "vue": "^2.6.10",
    "vue-template-compiler": "^2.6.10",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11"
  },
  "dependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "babel-loader": "^8.1.0",
    "html-webpack-plugin": "^4.0.4",
    "typescript": "^3.8.3",
    "vue-class-component": "^7.2.3",
    "vue-loader": "^15.9.1",
    "vue-property-decorator": "^8.4.1",
    "webpack-dev-server": "^3.10.3"
  }
}
 

三、其它

在这里插入图片描述

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node_modules\\.bin\\webpack --mode production",
    "serve": "node_modules\\.bin\\webpack-dev-server"
  },

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "strict": true,
        "module": "es2015",
        "moduleResolution": "node",
        "target": "es5",
        "skipLibCheck": true,
        "esModuleInterop": true,
        "experimentalDecorators": true
    },
    "include": [
        "./src/**/*"
    ]
}

  • 配置webpackage
const path = require('path'); //nodejs内置模块
const HtmlWebpackPlugin = require('html-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const resolve = dir => path.resolve(__dirname, dir);
 
module.exports = {
  entry: './src/main.ts',
  output: {
    path: path.resolve(__dirname, 'dist'), // d://webpakc/dist
    filename: 'bundle.js' // d://webpakc/dist/bundle.js
  },
   //本地服务
   devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 9000,
    historyApiFallback: true, //不跳转
    inline: true //实时刷新
  },
  resolve: {
    // Add `.ts` and `.tsx` as a resolvable extension.
    extensions: [".ts", ".tsx", ".js",".json",".vue"],
    // 设置别名
    alias: {
      '@': resolve('src') // 这样配置后 @ 可以指向 src 目录
    }
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: path.resolve(__dirname, 'node_modules'),
        include: path.resolve(__dirname, 'src'),
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 12192
          }
        }]
      },
      {
        test: /\.tsx?$/,
        loader: "ts-loader",
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
 
  //插件
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: __dirname + "/public/index.html"
    }),
    // 请确保引入这个插件!
    new VueLoaderPlugin()
  ]
};

在这里插入图片描述

declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值