项目作者: pinkbunny1

项目描述 :
address app to add edit delete address entry
高级语言: TypeScript
项目地址: git://github.com/pinkbunny1/addressApp.git
创建时间: 2019-01-10T15:13:39Z
项目社区:https://github.com/pinkbunny1/addressApp

开源协议:

下载


Address book app

Refactoring code on my old side-project.
In hope to learn and be more comfortable with TypeScripts and DUCKs redux file structure and feature-first file structure to build scalable project, I have started to refactor this simple addressBookApp which is the conventional functionality-first approach React app.

Author: Jin Lee

Pre-installations

Installations

  1. npm install
  2. npm run start:dev
  3. npm start
  4. (to test) npm test

How to add Typescript to existing React App

In order to convert existing react app to Typesciprt, it requires 2 changes:

  1. Add TypeSciprt Compiler (tsc) to building pipeline
  2. Change JS —> TS files

1. Add TypeSciprt Compiler (tsc) to building pipeline

  1. Install dependencies:

    If you have CRA (react-scripts@2.1.0 or higher):
    Facebook Guide source

    1. $ npm install --save typescript @types/node @types/react @types/react-dom @types/jest
    2. or
    3. $ npm install --save typescript @types/node @types/react @types/react-dom @types/jest

    scripts@2.1.0) or higher" class="reference-link">How to upgrade CRA(react-scripts@2.1.0) or higher

    1. Go to package.json and find react-scripts:"....". If its version is lower than 2.1.0 then manually add “2.1.0” or higher.
    2. $ npm install to reinstall.

OR
MS guide source

$ npm install --save-dev typescript awesome-typescript-loader source-map-loader

  1. - awesome-typescript-loader : webpack plugin to compile ts into js (like babel loader for babel)
  2. - source-map-loader : adds source map support for debugging

$ npm install --save @types/react @types/react-dom

  1. - installs type declaration files (.d.ts files) from @types for any library in use.
  1. Configure TypeScript
    Create a typescript config file to configure TypesScript (tsconfig.json in the root folder of the app)

    1. <!-- tsconfig.json file -->
    2. {
    3. "compilerOptions": {
    4. "outDir": "./dist/", // path to output directory
    5. "sourceMap": true, // allow sourcemap support
    6. "strictNullChecks": true, // enable strict null checks as a best practice
    7. "module": "es6", // specify module code generation
    8. "jsx": "react", // use typescript to transpile jsx to js
    9. "target": "es5", // specify ECMAScript target version
    10. "allowJs": true, // allow a partial TypeScript and JavaScript codebase
    11. "moduleResolution": "node" // fixes csstype bug
    12. },
    13. "include": [
    14. "./src/**/*" // where ts files that needs to be compiled to js reside
    15. ]
    16. }
  2. Setup Build pipeline

    1. Modify webpack.config.js file in order to add TypeScript compilation as a part of build process.
      Required changes are:

      1. To handle .ts & .tsx files
      2. Replace loader from babel-loader to awesome-typescript-loader
      3. Add source-map-loader
      4. Modify entry file from App.js to App.ts [optional].
      1. <!-- webpack.config.js file -->
      2. module.exports = {
      3. // change to .tsx if necessary
      4. entry: './src/app.jsx',
      5. output: {
      6. filename: './dist/bundle.js'
      7. },
      8. resolve: {
      9. // changed from extensions: [".js", ".jsx"]
      10. extensions: [".ts", ".tsx", ".js", ".jsx"]
      11. },
      12. module: {
      13. rules: [
      14. // changed from { test: /\.jsx?$/, use: { loader: 'babel-loader' } },
      15. { test: /\.(t|j)sx?$/, use: { loader: 'awesome-typescript-loader' } },
      16. // addition - add source-map support
      17. { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
      18. ]
      19. },
      20. externals: {
      21. "react": "React",
      22. "react-dom": "ReactDOM",
      23. },
      24. // addition - add source-map support
      25. devtool: "source-map"
      26. }
    2. Delete .babelrc and other Babel dependencies from package.json
    3. Above actions correctly set up build pipeline with TypeScript for handling transpilation. Build the app with this command —> $ npx webpack (installation of npx is required $ npm install -g npx)

      2. Change JS —> TS files

  3. Min conversion
    1. Change .js —> .ts or .jsx --> .tsx extensions in files
    2. Import React from react —> import * as React from react
    3. Add types to class declararion of React.Component by the end of eract.Component<any, any>.
  4. Add types
    1. Add types for properties and state for the Component
    2. Add types to params
  5. Convert the entire codebase

    After each step always bundle the app by running $ npx webpack

To do

  • Add TypeScripts
  • Change from functionality-first to feature-first
  • Separate one file into Container and Presentational Component
  • Apply DUCKs for Redux
  • Testing in Typescript
  • Add optimisation
    • Code Splitting
    • SSR