reactwebGL

react+webpack+webstorm开发环境搭建

2016-07-17  本文已影响16413人  清歌笑染红尘

需要安装的软件

以上两个包的下载地址如下:

https://nodejs.org/dist/v6.3.0/node-v6.3.0.pkg

由于6.3.0版本之后会自带npm的包管理所以不需要单独的安装npm

更换npm库的源

由于国内的网络原因,推荐只用阿里的npm源地址

npm config set registry https://registry.npm.taobao.org

构建项目

npm install webpack webpack-dev-server babel --save-dev

npm install react react-dom babel-loader less-loader css-loader style-loader url-loader file-loader babel-preset-es2015 babel-preset-react react-hot-loader jquery eslint eslint-plugin-react --save-dev


--save-dev 会把下载包的相关信息写到package.json的devDependencies里面方便以后发布,其他人使用的时候只需要npm install就可以把相关的依赖下载到当前的项目里面。

在package.json里面包的版本之前的^表示可以安装类似2.x.x版本的组件但是不能安装3.x.x版本的软件
在package.json里面包的版本之前的~表示可以安装2.1.x的软件不能安装2.3.x的软件

/**
 * Created by Diablo on 16/7/16.
 */
'use strict';
var webpack = require('webpack');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var path = require('path');

module.exports = {
    devtool: 'eval',
    entry: [
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
         './src/js/entry.js'
    ],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/static/'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        commonsPlugin
    ],

    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader'
            },
            {
                test: /\.jsx?$/,
                loaders: ['react-hot', 'babel'],
                exclude: /node_modules/,

            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            },
            {
                test: /\.less$/,
                loader: 'style-loader!css-loader!less-loader'
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "url-loader?limit=10000&mimetype=application/font-woff"
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "file-loader"
            }
        ]
    },
    resolve: {
        root:'',
        extensions: ['', '.js', '.json', '.less']
    }
};
{
  "presets": ["es2015", "stage-0", "react"]
}

上面的是babel插件的相关信息

{
  "ecmaFeatures": {
    "jsx": true,
    "modules": true
  },
  "env": {
    "browser": true,
    "node": true
  },
  "parser": "babel-eslint",
  "rules": {
    "quotes": [2, "single"],
    "strict": [2, "never"],
    "react/jsx-uses-react": 2,
    "react/jsx-uses-vars": 2,
    "react/react-in-jsx-scope": 2
  },
  "plugins": [
    "react"
  ]
}

这个配置是设置js的代码相关规范的

/**
 * Created by Diablo on 16/7/16.
 */
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    hot: true,
    inline:true,
    historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
    if (err) {
        return console.log(err);
    }

    console.log('Listening at http://localhost:3000/');
});
"scripts": {
    "start": "node server.js",
    "lint": "eslint src"
    }
import React, { Component } from 'react';

export default class Hello extends Component {
    render() {
        return (
            <div>
                <h1>Hello, world.</h1>
                <p>this is a simple section</p>
                <p>终于搭建成功了</p>
            </div>

        );
    }
}
import React from 'react';
import ReactDOM from 'react-dom';
import Hello from './helloworld';

ReactDOM.render(<Hello />, document.getElementById('root'));
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="/static/common.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/bundle.js" charset="utf-8"></script>
</body>
</html>

完成之后的目录结构

上一篇下一篇

猜你喜欢

热点阅读