首先了解一下为什么需要手动搭建,其实我们在搭建普通的react脚手架以及ts-react脚手架的时候,按照react以及ts官网搭建,基本上只需要敲命令而已。但是也有一个问题,在react15版本之后,start以及build的关于webpack的配置,都在node_modual里面。直接使用当然没有问题,但关键是,你得知道webpack的配置究竟都是些什么,这就是另外一回事了。所以,这里主要是我面对我们工作业务的时候,搭建的react脚手架
1.创建一个初始项目
1 2 3 4 5
| mkdir my-project
cd my-project
npm init
|
2.安装一些需要的依赖包
安装React框架以及Antd
1
| npm install react react-dom antd --save
|
安装 React 类型库 (帮助 Typescript 识别 React 库中定义的类型)
1 2
| npm install @types/react @types/react-dom --save-dev
|
安装 Typescript 和 ts-loader
1 2
| npm install typescript ts-loader --save-dev
|
安装 Webpack
1 2
| npm install webpack webpack-cli webpack-dev-server webpack-merge --save-dev
|
安装 Webpack 插件
1 2 3 4 5 6 7 8 9 10 11 12 13
| npm install source-map-loader html-webpack-plugin clean-webpack-plugin uglifyjs-webpack-plugin cross-env --save-dev
可以生成创建html入口文件,比如单页面可以生成一个html文件入口,配置N个html-webpack-plugin可以生成N个页面入口
|
3.配置 webpack
定义配置中所需要用到的文件路径./config/paths.js
1 2 3 4 5 6 7 8 9 10 11 12
| const path = require('path') const fs = require('fs')
const appDirectory = fs.realpathSync(process.cwd()) const resolveApp = relativePath => path.resolve(appDirectory, relativePath)
module.exports = { appBuild: resolveApp('build'), appPublic: resolveApp('public'), appIndex: resolveApp('src/index.tsx'), appHtml: resolveApp('public/index.html'), }
|
定义公用的 webpack 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 在此目录建立./config/webpack/webpack.config.common.js
const HtmlWebpackPlugin = require('html-webpack-plugin')
const paths = require('../paths')
module.exports = { resolve: { extensions: ['.tsx', '.ts', '.js', '.json'] }, devtool: 'source-map', module: { rules: [ { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }, { test: /\.(ts|tsx)$/, loader: "ts-loader", }, ], }, plugins: [ <!--创建一个在内存中生成的html插件--> new HtmlWebpackPlugin({ title: 'React + Typescript Project', template: paths.appHtml, inject: false, }), ], }
|
定义 development 环境 webpack 配置./config/webpack/webpack.config.dev.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| const webpack = require('webpack') const merge = require('webpack-merge')
const paths = require('../paths') const commonConfig = require('./webpack.config.common')
module.exports = merge.smart(commonConfig, { mode: 'development', entry: paths.appIndex, output: { filename: 'static/js/[name]-bundle-[hash:8].js', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NamedModulesPlugin() ], devServer: { historyApiFallback: true, disableHostCheck: true, host: '0.0.0.0', port: '8000', inline: true, hot: true, proxy: { '/api': { target: 'http://backend.api.host', changeOrigin: true, } } } })
|
定义 production 环境 webpack 配置./config/webpack/webpack.config.prod.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const merge = require('webpack-merge') const CleanWebpackPlugin = require('clean-webpack-plugin') const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const paths = require('../paths') const commonConfig = require('./webpack.config.common')
module.exports = merge.smart(commonConfig, { mode: 'production', entry: paths.appIndex, output: { path: paths.appBuild, filename: 'static/js/[name]-[hash:8].js', }, optimization: { minimizer: [ new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: true }), ], splitChunks: { chunks: 'all', }, }, plugins: [ new CleanWebpackPlugin([paths.appBuild], { root: process.cwd() }), ], })
|
4.配置 tsconfig
./tsconfig.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { "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: 允许合成默认导出
1 2 3
| import * as React from 'react' => import React from 'react'
|
5.创建入口文件
./public/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title><%= htmlWebpackPlugin.options.title %></title> <% for (let css in htmlWebpackPlugin.files.css) { %> <link href="/<%=htmlWebpackPlugin.files.css[css] %>" rel="stylesheet"> <% } %> </head> <body> <noscript> You need to enable JavaScript to run this app. </noscript> <div id="root"></div> <% for (let chunk in htmlWebpackPlugin.files.chunks) { %> <script type="text/javascript" src="/<%=htmlWebpackPlugin.files.chunks[chunk].entry %>"></script> <% } %> </body> </html>
|
./src/index.tsx
1 2 3 4 5 6 7
| import React from 'react' import ReactDOM from 'react-dom'
ReactDOM.render( <div>Hello React</div>, document.getElementById('root'), )
|
6.配置 package 启动脚本
./package.json
1 2 3 4 5 6 7 8
| { ..., "scripts": { "start": "cross-env NODE_ENV=development webpack-dev-server --open --colors --config config/webpack/webpack.config.dev.js", "build": "cross-env NODE_ENV=production webpack --progress --colors --config config/webpack/webpack.config.prod.js" }, ... }
|
7.验收成果
运行下面的命令,就可以在浏览器里看到 “Hello React” 欢迎页啦。
8.使用npm-build
由于clean-webpack-plugin已经升级的关系,所有配置方面我们需要修改一下,打开webpack.config.prod.js
1 2 3 4 5 6 7 8 9 10
| plugins: [ new CleanWebpackPlugin([paths.appBuild], { root: process.cwd() }) ] 改为 plugins: [ new HtmlWebpackPlugin() ]
|
9.打包