3

I am trying to use React-Native web to convert an existing React Native App to Web App. The App is getting compiled successfully now, but the page appears blank and I get the following error on checking console.

It is working fine when we don't use the project files and make a simple screen with react native elements like View, Text etc.

rnw_blogpost.bundle.js:31234 Uncaught ReferenceError: exports is not defined
    at eval (extends.js:2)
    at Module../node_modules/react-redux/node_modules/@babel/runtime/helpers/esm/extends.js (rnw_blogpost.bundle.js:30586)
    at __webpack_require__ (rnw_blogpost.bundle.js:31231)
    at fn (rnw_blogpost.bundle.js:31457)
    at eval (connectAdvanced.js:1)
    at Object../node_modules/react-redux/es/components/connectAdvanced.js (rnw_blogpost.bundle.js:17465)
    at __webpack_require__ (rnw_blogpost.bundle.js:31231)
    at fn (rnw_blogpost.bundle.js:31457)
    at eval (index.js:1)
    at Object../node_modules/react-redux/es/index.js (rnw_blogpost.bundle.js:17585)

Here is my webpack.config.js

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 = [
  // Node Modues come here!
].map((moduleName) => path.resolve(appDirectory, `node_modules/${moduleName}`));


const babelLoaderConfiguration = {
  test: /\.js$/,
  // Add every directory that needs to be compiled by Babel during the build.
  include: [
    path.resolve(__dirname, 'node_modules/react-native-uncompiled'),
    path.resolve(__dirname, 'index.web.js'), // Entry to your application
    path.resolve(__dirname, 'App.web.js'), // Change this to your main App file
    path.resolve(__dirname, 'src'),
    ...compileNodeModules,
  ],
  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']
    }
  }
};

const svgLoaderConfiguration = {
  test: /\.svg$/,
  use: [{
    loader: '@svgr/webpack',
  }, ],
};

const imageLoaderConfiguration = {
  test: /\.(gif|jpe?g|png)$/,
  use: {
    loader: 'url-loader',
    options: {
      name: '[name].[ext]',
    },
  },
};

module.exports = {
  entry: {
    app: path.join(__dirname, 'index.web.js'),
  },
  output: {
    path: path.resolve(appDirectory, 'dist'),
    publicPath: '/',
    filename: 'rnw_blogpost.bundle.js',
    libraryTarget: 'umd'
  },
  resolve: {
    extensions: ['.web.tsx', '.web.ts', '.tsx', '.ts', '.web.js', '.js'],
    alias: {
      'react-native$': 'react-native-web',
      App: path.resolve("src"),
    },
  },
  module: {
    rules: [
      babelLoaderConfiguration,
      imageLoaderConfiguration,
      svgLoaderConfiguration,
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'index.html'),
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      // See: https://github.com/necolas/react-native-web/issues/349
      __DEV__: JSON.stringify(true),
    }),
  ],
  target: ['node'],
};

& here is the code of my babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};
Sva
  • 135
  • 1
  • 12

0 Answers0