typescript 2.9.x +webpack 4.16.x + react 16.4.x

typescript 2.9.x +webpack 4.16.x + react 16.4.x 从零开始,配置简单可自由更改,按需加载antd 3.8的组件,包括样式
前言:
由于网上找不到typescript结合react,并按需加载antd的教程,于是开始踩坑。
该过程中发现
1 “cannot resolve module ‘antd’” (tsconfig 中设置moduleResolution: “node”,告诉webpack去相应的node_modules目录下面寻找 antd,如果不这么做就得手动指定:
import Layout from ‘../../../node_modules/antd/lib/layout/index’;
一个个指定,但我就想要
import { Layout, Menu, Icon } from “antd”;
把组件和样式一起导入
) ;
2 无法解析 less (less 版本太高,降低版本即可,版本参考antd的package.json中使用的版本)
3 16.4.x版本的react缺少 prop-types 和 csstype( npm install -D prop-types @types/prop-types csstype )
(csstype 是css属性的集合,用typescript的角度来看,就是一堆接口的集合,可以用这些接口来做类型检查 )

项目地址:https://github.com/hachimei/t…

此项目很简单,没有加入过多的功能,目前只提供 打包成一个文件、使用ts-import-plugin按需加载antd组件及其样式、热更新、解析less和css文件、url-loader图片,更多功能请自己添加。
1 安装依赖: npm install ; 推荐使用 cnpm install
2 package.json如下:

{
  "name": "play1",
  "version": "1.0.0",
  "description": "play react with ts!",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node ./scripts/dev.js",
    "build": "node ./scripts/build.js"
  },
  "keywords": [
    "typescript",
    "react",
    "webpack"
  ],
  "author": "hachimei",
  "license": "MIT",
  "dependencies": {
    "react": "^16.4.2",
    "react-dom": "^16.4.2"
  },
  "devDependencies": {
    "@types/antd": "^1.0.0",
    "@types/glob": "^5.0.35",
    "@types/prop-types": "^15.5.5",
    "@types/react": "^16.4.11",
    "@types/react-dom": "^16.0.7",
    "@types/rimraf": "^2.0.2",
    "@types/shelljs": "^0.8.0",
    "antd": "^3.8.2",
    "awesome-typescript-loader": "^5.2.0",
    "css-loader": "^1.0.0",
    "csstype": "^2.5.6",
    "less": "^2.7.3",
    "less-loader": "^4.1.0",
    "prop-types": "^15.6.2",
    "shelljs": "^0.8.2",
    "source-map-loader": "^0.2.3",
    "style-loader": "^0.22.1",
    "ts-import-plugin": "^1.5.4",
    "ts-loader": "^4.4.2",
    "url-loader": "^1.1.1",
    "webpack": "^4.16.5",
    "webpack-dev-server": "^3.1.5"
  }
}

3 tsconfig文件如下:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "es2015",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    "jsx": "react",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    // "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */

    /* Source Map Options */
    // "sourceRoot": "./",                    /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "./",                       /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}
    原文作者:hachimei
    原文地址: https://segmentfault.com/a/1190000016092787
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞