webpack学习

webpack从0开始手动搭建react + typeScrip

2019-10-08  本文已影响0人  xp_a5d1

1. 初始化

yarn init

2. 安装依赖

react

yarn add react react-dom

React 类型库

yarn add --dev @types/react @types/react-dom

typeScript 和 ts-loader

yarn add --dev typescript ts-loader

Webpack

yarn add --dev webpack webpack-cli webpack-dev-server webpack-merge

Webpack插件

yarn add --dev source-map-loader html-webpack-plugin clean-webpack-plugin uglifyjs-webpack-plugin

3. webpack配置

4. tsconfig.json文件配置

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true, 
    "jsx": "react",
    "lib": ["es6", "dom"],
    "module": "esnext",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "rootDir": "src",
    "sourceMap": true,
    "strict": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "build"
  ]
}

allowSyntheticDefaultImports: 允许合成默认导出

import * as React from 'react'
=>
import React from 'react'

5. 入口文件

6. package 启动脚本

{
  ...,
  "scripts": {
    "start": "webpack-dev-server --config ./build/webpack.dev.conf.js",
    "build": "webpack --config ./build/webpack.prod.conf.js"
  },
  ...
}

参考

上一篇下一篇

猜你喜欢

热点阅读