Type script中webpack文件配置

news/2024/5/20 5:08:11 标签: type-script, TS, react

文章目录

      • 一、Ts编译方式
      • 二、package.json
      • 三、webpack.config,js
      • 四、tsconfig.json

一、Ts编译方式

手动编译:

$ tsc 文件名.ts,回车编译。

监听编译:

$ tsc -w

设置自动编译:
找到项目文件(*.csproj的文件),编辑打开,找到节点,在里面添加如下节点信息:

<Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\1.0\tsc&quot;    @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

<Exec Command="tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

二、package.json

{
  "name": "ts",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack serve --open 'google chrome'"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/preset-env": "^7.15.0",
    "babel-loader": "^8.2.2",
    "clean-webpack-plugin": "^4.0.0-alpha.0",
    "core-js": "^3.16.1",
    "html-webpack-plugin": "^5.3.2",
    "ts-loader": "^9.2.5",
    "typescript": "^4.3.5",
    "webpack": "^5.49.0",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^3.11.2"
  }
}

三、webpack.config,js

//引入一个包
const path = require("path");
//引入html插件
const HTMLWebpackPlugin = require('html-webpack-plugin')
//引入clean插件
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

//webpack中的所有配置信息都应该写在module.exports中
module.exports = {
  //指定入口文件
  entry: "./src/index.ts",
  output: {
    //指定打包文件的目录
    path: path.resolve(__dirname, "dist"),
    //打包后文件的文件
    filename: "bundle.js",
    //告诉webpack不适用箭头函数
    environment: {
      arrowFunction:false
    }
  },
  //指定webpack 打包时要使用的模块
  module: {
    //指定要加载的规则
    rules: [
      {
        //test指定的是规则生效的文件
        test: /\.ts$/,
        //要使用的loader
        use: [
          //配置babel
          {
            //指定加载器
            loader: "babel-loader",
            //设置babel
            options: {
              //设置预定义的环境
              presets: [
                [
                  //指定环境插件
                  "@babel/preset-env",
                  //配置信息
                  {
                    //要兼容的模板浏览器
                    targets: {
                      "chrome":"55",
                      "ie":"11",
                    },
                    //指定corejs的版本
                    "corejs": "3",
                    //使用corejs的方式,"usage"表示按需加载
                    "useBuiltIns":"usage"
                  }
                ]
              ]
            }
          },
          "ts-loader"
        ],
        //要排除的文件
        exclude: /node-modules/,
      },
    ],
  },
  //开发模式编译
  // mode:'development',
  //配置webpack插件
  plugins: [
    new CleanWebpackPlugin(),
    new HTMLWebpackPlugin({
      // title:'这是一个自定义的title'
      template:'./src/index.html'
    })
  ],
  // plugins: [
  //   new HTMLWebpackPlugin()
  // ]
  resolve: {
    extensions:['.ts','.js']
  }
};

四、tsconfig.json

终端执行

$ tsc --init

会在自动生成tsconfig.json文件

{
  /*
    tsconfig.json是ts编译器的配置文件,ts编译器可以根据它的信息来对代码进行编译
  */
  //'include' 用来指定哪些ts文件需要被编译
  // "include":[
  //   // "./src/**/*"
  // ],
  //'exclude' 用来指定哪些ts文件不需要被编译
  // "exclude":[
  //   // "./hello/**/*"
  // ],
  //'compilerOptions' 编辑器的选项
  "compilerOptions": {
    //用来指定ts编译为ES6版本的js文件 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'esnext'
    "target": "ES6",
    //指定要使用ES6模块化的规范 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'esnext'
    "module": "ES6",
    //用来指定项目中要使用的库,一般情况不用改,当不是浏览器环境可以写
    // "lib":["ES6"],
    //用来指定编译后文件所在的目录
    "outDir": "./dist",
    //将代码编译后合并为一个js文件放在指定目录,用了模块化合并不了,除非把module换成system
    // "outFile":"./dist/app.js",
    // 是否对js文件进行编译,默认是false
    "allowJs": false,
    //是否检查js的代码是否符合语法规范,默认值是false
    "checkJs": false,
    //编译js是否移除ts中注释
    "removeComments": true,
    //不生成编译后的文件,默认值是false
    "noEmit": false,
    //当有错误时不生成编译后的文件
    "noEmitOnError": false,

    //所有严格检查的总开关,如果为true,下面的检查模式全部打开
    "strict": false,
    //用来设置编译后的文件是否使用严格模式,编译后的js文件上显示''use strict'
    "alwaysStrict": false,
    //不允许隐式的any类型,如果有则报错
    "noImplicitAny": false,
    //不允许不明确类型的this,比如没有指定函数中的this
    "noImplicitThis": false,
    //严格检查空值
    "strictNullChecks": false,
  }
}

http://www.niftyadmin.cn/n/1527197.html

相关文章

Type Script抽象类(abstract)

以abstract开头的类是抽象类抽象类和其他类区别不大&#xff0c;唯一的区别就是不能创建对象抽象类是专门需要被继承的类 abstract class Animal{name:stringcunstructor(name:string){this.name name}//定义一个抽象方法//抽象方法以abstract开头&#xff0c;没有方法体//抽…

k8s: pod的服务级别怎么确定

k8s, v1.17.4: kubernetes/pkg/apis/core/v1/helper/qos/qos.go // GetPodQOS returns the QoS class of a pod. // A pod is besteffort if none of its containers have specified any requests or limits. 如果pod 里的container 没有设置资源的请求量和限制。 // A pod is…

Ant Design表单的使用

文章目录一、基本介绍二、this.props.form 属性提供的 API1、getFieldDecorator2、getFieldValue3、setFieldsValue4、validateFields三、格式限制验证1、输入框不能为空限制2、输入框字符限制3、自定义校验4、whitespace空格报错5、pattern正则验证四、表单的回显设置1、普通i…

go: goclipse

https://github.com/GoClipse/goclipse/tree/latest 错误&#xff1a;GOROOT no path specified 问题解决&#xff1a;需要在eclipse->window->preferences->go 页面设置Go installation的目录&#xff0c;然后再设置 Tools 下的 godef/gocode/guru 出现原因&#xff…

k8s: kubectl: 总结

文章目录kubectl get nodeskubectl cluster-info错误error: unable to upgrade connection: pod does not existkubectl get nodes [rootparity-cnsbc-all-in-one-01 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION parity-cnsbc-all-in-one-01 Ready 17d v1.15.4 kub…

Type Script接口(interface)

接口用来定义一个类结构,用来定义一个类中包含哪些属性和方法同时接口也可以当成类型声明去使用 //可以使用多个interface来限制同一个myInterface interface myInterface {name: string;age: number; }interface myInterface {gender: string; }const obj1: myInterface {na…

docker dump goroutine

通过/cluster/gce/gci/health-monitor.sh while true; doif ! timeout 60 ${healthcheck_command} > /dev/null; thenecho "Container runtime ${container_runtime_name} failed!"if [[ "$container_runtime_name" "docker" ]]; then# Dump…

Type Script属性封装(public、private、protected)

文章目录publicprivatejs使用getter和setter的方法&#xff1a;TS中get和set语法糖&#xff1a;protected简写方法&#xff1a;public TS可以在属性前添加属性的修饰符public修饰的属性可以在任意位置访问(修改) 默认值 (function () {//定义一个表示人物的类class Person {n…