React:React+Webpack

2017-10-17  本文已影响0人  蚊小文

搭建react+webpack项目所需安装插件

webpack webpack-dev-server -g 全局依赖;

搭建项目基础架构用webpack2

npm ini --y

配置webpack.config.js文件

const path = require('path');
const webpack = require('webpack');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: 'style-loader',
                    publicPath: './dist',
                    use: 'css-loader'
                })
            }
        ]
    },
    plugins: [
        new ExtractTextWebpackPlugin('styles.css'),
        new HtmlWebPackPlugin({
            template: './src/index.html'
        })
    ]
};

更快捷的执行打包任务

{
  "name": "react-pro",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server --progress --colors --port 3000 --inline",
    "build": "webpack -p"
  },
  "dependencies": {
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-router": "^3.2.0"
  }

在终端输入指令 npm start 即可跑起来了。

上一篇下一篇

猜你喜欢

热点阅读