這次帶給大家React全家桶環境搭建程式碼解析,React全家桶環境搭建的注意事項有哪些,下面就是實戰案例,一起來看一下。
環境建置
##1.從零開始建立webpack react開發環境2.引入Typescript安裝依賴npm i -S @types/react @types/react-dom npm i -D typescript awesome-typescript-loader source-map-loader新tsconfig.json
{ "compilerOptions": { "outDir": "./dist/", "sourceMap": true, "noImplicitAny": true, "module": "commonjs", "target": "es5", "jsx": "react" }, "include": [ "./src/**/*" ] }修改webpack.config.js
// webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack'); module.exports = { entry: { index:'./src/index.js', }, output: { filename: 'bundle.js', path: path.resolve(dirname, 'dist') }, devtool: "source-map", // Add '.ts' and '.tsx' as resolvable extensions. resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx'] }, module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'] }, { test: /\.(png|svg|jpg|gif)$/, use: ['url-loader'] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: ['url-loader'] }, { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader' } }, // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'. { test: /\.tsx?$/, loader: "awesome-typescript-loader" }, ] }, plugins: [ new HtmlWebpackPlugin({ title: 'production', template: './index.html' }), new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: './dist', hot: true }, };3.引入less並支援import less modules安裝依賴
npm i -D less less-loader npm i -D typings-for-css-modules-loadertips:typings-for-css-modules-loader打包時將樣式
模組化,我們可以透過import或require引入樣式,並且相互不衝突。
//demo.less -> demo.less.d.ts //.demo{color:red;} -> export const demo: string; import * as styles from 'demo.less' <DemoComponent className={styles.demo} />修改webpack.config.js
// webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const webpack = require('webpack'); module.exports = { entry: { index:'./src/index.js', }, output: { filename: 'bundle.js', path: path.resolve(dirname, 'dist') }, devtool: "source-map", //add .less resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx', '.less', '.css'] }, module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'] }, //import less modules,name:demodemo_hash { test: /\.less/, use: [ 'style-loader', 'typings-for-css-modules-loader?modules&importLoaders=1&localIdentName=[name][local]_[hash:base64:5]&namedExport&camelCase&less!less-loader' ] }, { test: /\.(png|svg|jpg|gif)$/, use: ['url-loader'] }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: ['url-loader'] }, { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader' } }, { test: /\.tsx?$/, loader: "awesome-typescript-loader" }, ] }, plugins: [ new HtmlWebpackPlugin({ title: 'production', template: './index.html' }), new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: './dist', hot: true }, };4.引入react-routerv4
npm i -S react-router-dom建立history
import { createHashHistory } from 'history'; export default createHashHistory();使用
import React from 'react'; import ReactDom from 'react-dom'; import * as styles from "./index.less"; import history from './helpers/history'; import {Router, Route, Switch, Redirect, Link} from 'react-router-dom'; import Hello from "./router/Hello"; import TodoList from "./router/TodoList"; const PrivateRoute = ({ component: Component , ...rest}) => { return ( <Route {...rest} render={props => ( <Component {...props}/> )}/> ); } ReactDom.render( <Router history={history} > <p className={styles.wrap}> <ul> <li><Link to="/">Homes</Link></li> <li><Link to="/todo">TodoList</Link></li> </ul> <Switch> <Route exact path="/" component={Hello}/> {/*<Route path="/demo" component={Demo}/>*/} <PrivateRoute path="/todo" component={TodoList} /> </Switch> </p> </Router>, document.getElementById('root') );...ES7語法報錯誤
npm i -S babel-preset-stage-2修改.babelrc
{ "presets": ["es2015", "react", "stage-2"], }5.引入mobx狀態管理
npm i -S mobx mobx-react使用裝飾器語法修改tsconfig.json
"compilerOptions": { "target":"es2017", //fix mobx.d.ts error "experimentalDecorators": true, "allowJs": true }
npm i -D babel-plugin-transform-decorators-legacy修改.babelrc
{ "presets": ["es2015", "react", "stage-2"], "plugins": ["transform-decorators-legacy"] }相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章! 推薦閱讀:
以上是React全家桶環境搭建程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!