react-native项目跑在web上(react-web)

来源:互联网 发布:香港电影讽刺中央知乎 编辑:程序博客网 时间:2024/06/13 06:48

1.项目搭建
版本:
“dependencies”: {
“react”: “15.0.0”,
“react-native”: “0.48.2”,
“react-dom”: “15.3.x”,
“react-web”: “0.4.6”
},
“devDependencies”: {
“babel-jest”: “21.2.0”,
“babel-preset-react-native”: “4.0.0”,
“jest”: “21.2.1”,
“react-test-renderer”: “16.0.0-alpha.12”,
“babel-loader”: “^6.2.5”,
“babel-preset-stage-1”: “^6.16.0”,
“haste-resolver-webpack-plugin”: “^0.2.2”,
“json-loader”: “^0.5.4”,
“react-hot-loader”: “^1.3.0”,
“webpack”: “^1.13.2”,
“webpack-dev-server”: “^1.16.1”,
“webpack-html-plugin”: “^0.1.1”
},
2.安装:
安装命令行工具react-web-cli: npm install react-web-cli -g
package.json 中配置版本 npm install
项目目录下创建web文件夹, 里面创建webpack.config.js文件, 内容如下:

‘use strict’;

var path = require(‘path’);
var webpack = require(‘webpack’);
var HtmlPlugin = require(‘webpack-html-plugin’);
var HasteResolverPlugin = require(‘haste-resolver-webpack-plugin’);

var IP = ‘0.0.0.0’;
var PORT = 3000;
var NODE_ENV = process.env.NODE_ENV;
var ROOT_PATH = path.resolve(__dirname, ‘..’);
var PROD = ‘production’;
var DEV = ‘development’;
let isProd = NODE_ENV === ‘production’;

var config = {
paths: {
src: path.join(ROOT_PATH, ‘.’),
index: path.join(ROOT_PATH, ‘index.web’),
},
};

var webpackConfig = {
ip: IP,
port: PORT,
devtool: ‘cheap-module-eval-source-map’,
resolve: {
alias: {
‘react-native’: ‘ReactWeb’,
},
extensions: [”, ‘.js’, ‘.web.js’, ‘.ios.js’, ‘.android.js’, ‘.native.js’, ‘.jsx’],
},
entry: isProd ? [
config.paths.index
] : [
‘webpack-dev-server/client?http://’ + IP + ‘:’ + PORT,
‘webpack/hot/only-dev-server’,
config.paths.index,
],
output: {
path: path.join(__dirname, ‘output’),
filename: ‘bundle.js’
},
plugins: [
new HasteResolverPlugin({
platform: ‘web’,
nodeModules: [‘react-web’]
}),
new webpack.DefinePlugin({
‘process.env’: {
‘NODE_ENV’: JSON.stringify(isProd ? PROD : DEV),
}
}),
isProd ? new webpack.ProvidePlugin({
React: ‘react’
}) : new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlPlugin(),
],
module: {
loaders: [{
test: /.json/,  
      loader: ‘json’,  
    }, {  
      test: /.jsx?
/,
loader: ‘react-hot’,
include: [config.paths.src],
exclude: [/node_modules/]
}, {
test: /.jsx?$/,
loader: ‘babel’,
query: {
presets: [‘react-native’, ‘stage-1’]
},
include: [config.paths.src],
exclude: [path.sep === ‘/’ ? /(node_modules\/(?!react-))/ : /(node_modules\(?!react-))/]
}]
}
};
webpackConfig.resolve.alias[path.basename(ROOT_PATH, ‘.’)] = path.join(ROOT_PATH, ‘.’);
module.exports = webpackConfig;

3.项目目录下创建index.web.js文件, 把index.ios.js内容copy过来, 在最后加上以下代码:

if (Platform.OS == ‘web’) {
var app = document.createElement(‘div’);
document.body.appendChild(app);
AppRegistry.runApplication(‘Helloworld’, {
rootTag: app
});
}
Platform 导入

4.react-web start 浏览器打开http://localhost:3000就能看到了.

5.错误处理:

1/:ERROR in Path must be a string. Received undefined
这个错误是 你的node 版本太高 降到5
2/:Error: Cannot resolve module ‘react/lib/ReactMount’
这个是因为ReactMount这个文件最新版本已经移动react-dom中了,
你可以把react 降到 15.0.0 就好了

原创粉丝点击