zoukankan      html  css  js  c++  java
  • react+babel+webpack初试

      在上一篇,我们简单学习了webpack学习,现在这里我们简单学习一下react+babel+webpack,进行编译react语法jsx以及结合es6写法。

      这里我就简单的直接上demo:

      package.json文件:

    {
      "name": "react-sample",
      "version": "1.0.0",
      "description": "webpack demo",
      "main": "./src/entry.js",
      "scripts": {
        "build": "webpack --display-error-details"
      },
      "repository": {
        "type": "git",
        "url": "https://github.com/dwqs/react_practice.git"
      },
      "keywords": [
        "react",
        "webpack"
      ],
      "author": "pomy",
      "devDependencies": {
        "react": "^0.13.3",
        "webpack": "^1.12.2",
        "babel-core": "^5.8.25",
        "babel-loader": "^5.3.2"
      }
    }
    

      webpack.config.js配置文件:

    var path = require('path');
    
    module.exports = {
        entry: path.resolve(__dirname, './src/entry.js'),
        output: {
            path: path.resolve(__dirname, './assets'),
            filename: 'bundle.js'
        },
    
        module: {
            //加载器配置
            loaders: [
                { test: /.js?$/, loaders: ['babel'], exclude: /node_modules/ },
                { test: /.js$/, loader: 'babel-loader', exclude: /node_modules/}
            ]
        },
    
        resolve:{
            //自动扩展文件后缀名,意味着我们require模块可以省略不写后缀名
            extensions:['','.js','.json']
        },
    };
    

      entry.js入口文件:

    import React from 'react';
    import { render } from 'react-dom';
    //import Hello from './hello';
    
    var HelloMessage = React.createClass({
    render: function() {
      return <h1>Hello {this.props.name}</h1>;
    }
    });
    
    render(
    <HelloMessage name="John" />,
    document.getElementById('example')
    );
    

      index.js:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>React Sample</title>
    </head>
    <body>
     <div id="example"></div>
    <script src="./assets/bundle.js"></script>
    </body>
    </html>
    

       当我们文件夹有以上文件时,我们就可以执行,npm  install (简写npm i)以及npm build 就可以编译es6写的jsx文件,打开页面就可以看到效果了。

    相关资料来源:

      react-dom

      详解前端模块化工具-Webpack

      ECMAScript 6 介绍

  • 相关阅读:
    linux 添加secondary ip
    最大传输单元(MTU)
    【PSY】 [歌詞] 父親
    make clean与make distclean的区别
    flex就是b/s中的c/s,要装插件flash player
    as中的replace只写了一个参数
    6个简单的解决方案解决Internet Explorer中的透明度问题
    9个优秀网上免费标签云生成工具
    2012年网页设计趋势
    11个非常漂亮动物为主题的高品质图标集
  • 原文地址:https://www.cnblogs.com/pingfan1990/p/5083074.html
Copyright © 2011-2022 走看看