一、首先咱们先建立一个react项目,react官网也有react项目搭建的命令css
npx create-react-app my-app
cd my-app
二、安装咱们项目须要的样式依赖,这个项目我用的是scsshtml
npm install node-sass -D
三、安装typescript的依赖命令node
npm install typescript @types/node @types/react @types/react-dom @types/jest
四、安装sass-loader和node-sass依赖react
npm install sass-loader node-sass --save-dev
五、打开react的webpack配置webpack
在node_modules下找到这个文件node_modules/react-scripts/config/webpack.config.dev.js 找到module下的rules,而后找到最后一个配置,修改为以下的样子
原来的web
{ loader: require.resolve('file-loader'), // Exclude `js` files to keep "css" loader working as it injects // its runtime that would otherwise be processed through "file" loader. // Also exclude `html` and `json` extensions so they get processed // by webpacks internal loaders. exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/], options: { name: 'static/media/[name].[hash:8].[ext]', }, }
改以后的typescript
{ exclude: [/\.js$/,/\.html$/,/\.json$/,/\.scss$/], loader: require.resolve('file-loader'), options: { name: 'static/media/[name].[hash:8].[ext]', }, }, { test:/\.scss$/, loaders:['style-loader','css-loader','sass-loader'] },
六、将src里面的文件改成这样,并将App.js改成App.tsxnpm
index.js代码以下:json
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render(<App />, document.getElementById('root'));
七、在App.tsx里面写一些简单的ts代码就能够run了sass
import React, { Component } from 'react';
interface Props { } interface State { list: string, } class App extends Component<Props, State> { constructor(props: Props) { super(props) this.state = { list: 'hello world!!!' } } render() { return (
); } } export default App;
七、App.scss代码以下
.content{ width: 500px; height: 500px; background-color: pink; margin: 0 auto; text-align: center; line-height: 500px; .bth{ color: blue; } }
七、运行项目执行命令: npm start //切记 改node_modules里面的文件 要重启一下项目