运行react-weui例子步骤

根据react-weui工程readme安装package

初始化工程

npm init

安装react和weui

npm install --save react react-dom
npm install --save weui react-weui

安装webpack

npm install --save-dev webpack

Let’s install the babel-loader and babel-core packages that we’ll use to work with Webpack,
as well as the ES2015 and React presets for loading the code that we’ll write.

npm install --save-dev babel-loader babel-core babel-preset-es2015 babel-preset-react

css-loader

npm install --save-dev css-loader

style-loader

npm install --save-dev style-loader

app.js:

// app.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

//import using commonJS Module *Require Plugins
//import { Button } from 'react-weui'

//import Using ES6 syntax
import WeUI from 'react-weui';

//import styles
import 'weui';
import 'react-weui/lib/react-weui.min.css';

const {Button} = WeUI;

class App extends Component {
    render() {
        return (
            <Button>hello wechat</Button>
        );
    }
}

ReactDOM.render((
    <App/>
), document.getElementById('container'));

index.html

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello weui</title>
  </head>
  <body>
    <div id="container"></div>
    <script src="dist/bundle.js"></script>
  </body>
</html>

webpack.config.js

const path = require('path');

module.exports = {
    entry: './app.js',
    output:  {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['es2015', 'react']
                  }
                }
            }
        ]
    }
}

使用webpack-dev-server运行

./node_modules/.bin/webpack-dev-server --progress --colors

浏览器访问:http://localhost:8080/webpack-dev-server/

如果把css额外打包

用到:

npm install --save-dev extract-text-webpack-plugin

代码修改,index.html

   <head>
     <meta charset="UTF-8">
     <title>Hello weui</title>
+    <link rel="stylesheet" href="dist/styles.css" />
   </head>

webpack.config.js

--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,4 +1,5 @@
 const path = require('path');
+const ExtractTextPlugin = require('extract-text-webpack-plugin')

 module.exports = {
     entry: './app.js',
@@ -20,8 +21,14 @@ module.exports = {
             },
             {
                 test: /\.css$/,
-                use: [ 'style-loader', 'css-loader' ]
+                use: ExtractTextPlugin.extract({
+                    fallback: 'style-loader',
+                    use: 'css-loader'
+                })
             }
         ]
-    }
+    },
+    plugins: [
+        new ExtractTextPlugin('styles.css')
+    ]
 }

相关文档:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 无意中看到zhangwnag大佬分享的webpack教程感觉受益匪浅,特此分享以备自己日后查看,也希望更多的人看到...
    小小字符阅读 8,278评论 7 35
  • webpack 介绍 webpack 是什么 为什么引入新的打包工具 webpack 核心思想 webpack 安...
    yxsGert阅读 6,548评论 2 71
  • 构建一个小项目——FlyBird,学习webpack和react。(本文成文于2017/2/25) 从webpac...
    布蕾布蕾阅读 16,956评论 31 98
  • 学习流程 参考文档:入门Webpack,看这篇就够了Webpack for React 一. 简单使用webpac...
    Jason_Zeng阅读 3,200评论 2 16
  • 连读了两天,翻过最后一页,忽然发现这本书已经看完了。 之所以选择这本书,多半是被它暧昧的名字所吸引,好像看...
    琉璃韵舫阅读 481评论 0 1