wepback搭建react开发环境

2019-06-21  本文已影响0人  duans_

版本介绍

webpack: 4.x

babel: 7.x

package.json

  "name": "react-demo",
  "version": "1.0.0",
  "description": "react实现评论功能",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --hot --open --port 8888",
    "build": "webpack --mode production",
    "start": "npm run dev"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "@babel/runtime": "^7.4.5",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.0.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "url-loader": "^2.0.0",
    "webpack": "^4.34.0",
    "webpack-cli": "^3.3.4",
    "webpack-dev-server": "^3.7.1"
  },
  "dependencies": {
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  }
}

webpack.config.js

// 引入path路径模块
const path = require('path');
// 引入html-webpack-plugin插件(需要先执行命令`npm i html-webpack-plugin -D`进行安装)
const HtmlWebpackPlugin = require("html-webpack-plugin");
// 创建插件实例对象
const htmlPlugin = new HtmlWebpackPlugin({
    // 指定模板文件
    template: path.join(__dirname, './src/index.html'),
    // 指定需要在内存中生成的html文件
    filename: "index.html"
});
module.exports = {
    mode: 'development',
    plugins: [
        htmlPlugin
    ],
    resolve: {
        extensions: ['.js', '.jsx', '.json'],
        alias: { '@': path.join(__dirname, './src') }
    },
    module: {
        rules: [
            {
                test: /\.js|jsx$/,
                use: {
                    loader: 'babel-loader'
                },
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                localIdentName: '[path][name]-[local]-[hash:5]'
                            }
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
                loader: 'url-loader',
                options: {
                    limit: 10000
                }
            }
        ]
    }
}

.babelrc

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        "@babel/plugin-transform-runtime",
        "@babel/plugin-proposal-class-properties"
    ]
}
上一篇下一篇

猜你喜欢

热点阅读