【发布时间】:2022-01-13 17:23:03
【问题描述】:
我们有一个使用 typescript、react-navigation、react-native-gesture-handler、redux/toolkit 作为主要包实现的 react-native 项目
最近我们将 react-native-web 集成到我们的项目中,但它运行不正确。
我们的项目有几个问题:
-
我们在导入自定义模块时无法加载它们。例如:
import MyCustomComponent from './components/MyCustomComponent' <View style={{flex: 1, backgroundColor: 'green'}}> <MyCustomComponent/> <--- does not show up, event when it contains a simple View component, we will see a blank screen </View>但是当我在当前文件中定义
MyCustomComponent时,它显示没有问题:function MyCustomComponent() { return( <View style={{flex:1, backgroundColor: 'yellow'}}></View> ) } export default function MyMainComponent() { return ( <View style={{flex:1, backgroundColor: 'green'}}> <MyCustomComponent/> <---- this shows up </View> ) } -
redux
Provider中的任何内容都不会再显示了。
我认为我们的webpack 配置是错误的,但由于我不是网络开发专家,我需要一些帮助来找出问题所在。这是我们的 webpack 配置:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const appDirectory = path.resolve(__dirname);
const {presets} = require(`${appDirectory}/../babel.config.js`);
const compileNodeModules = [
// Add every react-native package that needs compiling
'react-native-gesture-handler',
'react-redux',
'react-native-reanimated',
'react-native-confirmation-code-field',
'react-native-calendars',
'@react-native-google-signin/google-signin',
'react-native-compressor',
'react-native-swipe-gestures',
'@react-native-async-storage',
'react-native-shared-element',
'@react-navigation',
'react-native-material-menu',
'@reduxjs/toolkit',
'react-navigation-shared-element',
'react-native-collapsible-tab-view',
'react-native-image-crop-picker',
'@react-native-community',
'react-nativbe-safe-area-context/lib',
'react-native-screens',
].map(moduleName =>
path.resolve(appDirectory, `../node_modules/${moduleName}`),
);
const babelLoaderConfiguration = {
test: /\.js$|tsx?$/,
// Add every directory that needs to be compiled by Babel during the build.
include: [
path.resolve(__dirname, '../index.web.js'), // Entry to your application
path.resolve(__dirname, '../src/index.web.tsx'), // Change this to your main App file
path.resolve(__dirname, '../src'),
...compileNodeModules,
],
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets,
plugins: ['react-native-web'],
},
},
};
const svgLoaderConfiguration = {
test: /\.svg$/,
use: [
{
loader: '@svgr/webpack',
},
],
};
const tsLoaderConfiguration = {
test: /\.(ts|tsx|web.ts|web.tsx)?$/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
],
};
const imageLoaderConfiguration = {
test: /\.(gif|jpe?g|png)$/,
use: {
loader: 'url-loader',
options: {
name: '[name].[ext]',
esModule: false,
},
},
};
module.exports = {
entry: {
app: path.join(__dirname, '../index.web.js'),
},
output: {
path: path.resolve(appDirectory, 'dist'),
publicPath: '/',
filename: 'arcelor.bundle.js',
},
resolve: {
extensions: ['.web.tsx', '.web.ts', '.tsx', '.ts', '.web.js', '.js'],
alias: {
'react-native$': 'react-native-web',
},
},
module: {
rules: [
babelLoaderConfiguration,
imageLoaderConfiguration,
svgLoaderConfiguration,
tsLoaderConfiguration,
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, './index.html'),
filename: 'index.html',
inject: 'body',
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
// See: https://github.com/necolas/react-native-web/issues/349
// __DEV__: JSON.stringify(true),
__DEV__: process.env.NODE_ENV !== 'production',
process: {env: {}},
}),
],
};
我正在使用webpack@^5.65.0
谁能帮我找出问题所在以及如何让react-native-web 与我们的项目一起工作?谢谢
【问题讨论】:
标签: reactjs react-native webpack redux-toolkit react-native-web