React native 将react本机web应用嵌入现有网站
我想嵌入到现有的网站应用程序,目前正在寻找如何做到这一点的选项 应用程序应该是一个非常简单的调查问卷,需要嵌入Elementor创建的网站中。我的想法是使用并以某种方式插入我的应用程序,但不幸的是,我不知道如何做到这一点 我有一点开发React Native(RN)应用程序的经验,但我对web开发非常陌生,因此我认为使用RN和React Native web库会更容易 到目前为止,我已经使用React native 将react本机web应用嵌入现有网站,react-native,webpack,elementor,react-native-web,React Native,Webpack,Elementor,React Native Web,我想嵌入到现有的网站应用程序,目前正在寻找如何做到这一点的选项 应用程序应该是一个非常简单的调查问卷,需要嵌入Elementor创建的网站中。我的想法是使用并以某种方式插入我的应用程序,但不幸的是,我不知道如何做到这一点 我有一点开发React Native(RN)应用程序的经验,但我对web开发非常陌生,因此我认为使用RN和React Native web库会更容易 到目前为止,我已经使用npx react native init WebApp创建了一个RN项目,从react native w
npx react native init WebApp
创建了一个RN项目,从react native web复制了App.js、index.js和package.json文件,删除了node_modules文件夹并运行npm install
。然后,我能够使用package.json中的脚本启动并构建这个示例web应用程序
现在我的问题是,如何使用build目录的输出并将其嵌入html标记中
我还尝试使用来自的配置来捆绑应用程序,但我总是在修复最后一个错误后立即收到新错误。是否可以将RN应用程序捆绑到一个JS文件中,然后将其插入网站
期待任何建议
马可我用下面的网页配置解决了这个问题。创建的bundle.web.js内容被放入脚本标记(
…
)中。这可以嵌入到HTML小部件中
// web/webpack.config.js
const path = require('path');
const webpack = require('webpack');
const appDirectory = path.resolve(__dirname, '');
// This is needed for webpack to compile JavaScript.
// Many OSS React Native packages are not compiled to ES5 before being
// published. If you depend on uncompiled packages they may cause webpack build
// errors. To fix this webpack can be configured to compile to the necessary
// `node_module`.
const babelLoaderConfiguration = {
test: /\.js$/,
// Add every directory that needs to be compiled by Babel during the build.
include: [
path.resolve(appDirectory, 'index.web.js'),
path.resolve(appDirectory, 'src'),
path.resolve(appDirectory, 'node_modules/react-native-uncompiled'),
],
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
// The 'metro-react-native-babel-preset' preset is recommended to match React Native's packager
presets: ['module:metro-react-native-babel-preset'],
// Re-write paths to import only the modules needed by the app
plugins: ['react-native-web'],
},
},
};
// This is needed for webpack to import static images in JavaScript files.
const imageLoaderConfiguration = {
test: /\.(gif|jpe?g|png|svg)$/,
use: {
loader: 'url-loader',
options: {
name: '[name].[ext]',
},
},
};
module.exports = {
entry: [
// load any web API polyfills
// path.resolve(appDirectory, 'polyfills-web.js'),
// your web-specific entry file
path.resolve(appDirectory, 'src/index.js'),
],
// configures where the build ends up
output: {
filename: 'bundle.web.js',
path: path.resolve(appDirectory, 'dist'),
},
// ...the rest of your config
module: {
rules: [babelLoaderConfiguration, imageLoaderConfiguration],
},
resolve: {
// This will only alias the exact import "react-native"
alias: {
'react-native$': 'react-native-web',
},
// If you're working on a multi-platform React Native app, web-specific
// module implementations should be written in files using the extension
// `.web.js`.
extensions: ['.web.js', '.js'],
},
};