Webpack - 搭建简单的React + Typescript开发环境
新建项目文件夹:
mkdir react-setup
cd react-setup
mkdir public src
touch public/index.html
touch src/app.js
配置及依赖:
npm init -y
npm install webpack webpack-cli --save-dev
npm install clean-webpack-plugin --save-dev
npm install babel-plugin-transform-class-properties --save-dev
npm install @babel/core @babel/preset-env @babel/preset-react babel-loader --save-dev
npm install typescript react react-dom --save--dev
npm install @types/react @types/react-dom @babel/preset-typescript --save-dev
配置webpack.config.js
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');
module.exports = {
entry: './src/app.tsx',
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
],
},
}
}
// {
// test: /\.tsx?$/,
// use: 'ts-loader',
// exclude: [/node_modules/, /tests/],
// },
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
}
};
babel配置文件 .babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
修改package.json
"start": "webpack --mode=development",
"build": "webpack --mode=production"