Skip to content

There was an error when i used it on the web (registerTranslation) #427

@thienphuccr

Description

@thienphuccr

Current behaviour

import { registerTranslation, enGB } from 'react-native-paper-dates';
registerTranslation('en-GB', enGB);

Uncaught ReferenceError: require is not defined
at eval (index.js:2:28)
at ./node_modules/react-native-paper-dates/lib/module/index.js (react.native.web.bundle.js:11193:1)
at webpack_require (react.native.web.bundle.js:11277:33)
at fn (react.native.web.bundle.js:11488:21)
at eval (date-range.tsx:1:829)
at ./src/components/Date/date-range.tsx (react.native.web.bundle.js:9239:1)
at webpack_require (react.native.web.bundle.js:11277:33)
at fn (react.native.web.bundle.js:11488:21)
at eval (SaleOrder.tsx:1:3099)
at ./src/screens/SaleOrder.tsx (react.native.web.bundle.js:9419:1)

Webpack config

// node > 20.18

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 = [
    '@react-native-async-storage/async-storage',
    '@react-navigation/drawer',
    '@react-navigation/native',
    '@react-navigation/stack',
    '@reduxjs/toolkit',
    'moment',
    'numeral',
    'process',
    'react',
    'react-native',
    'react-native-dotenv',
    'react-native-gesture-handler',
    'react-native-paper',
    'react-native-paper-dates',
    'react-native-paper-select',
    'react-native-reanimated',
    'react-native-safe-area-context',
    'react-native-swipe-list-view',
    'react-native-toast-message',
    'react-native-vector-icons',
    'react-native-web',
    'react-redux',
    'redux-persist',
    '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'),
    path.resolve(__dirname, 'src/index.tsx'),
    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 imageLoaderConfiguration = {
  test: /\.(png|jpe?g|gif|svg)$/i,
  type: 'asset',
  parser: {
    dataUrlCondition: {
      maxSize: 8 * 1024,
    },
  },
  generator: {
    filename: 'imgs/[name][hash][ext]',
  },
};

const ttfLoaderConfiguration = {
  test: /\.(woff|woff2|eot|ttf|otf)$/,
  loader: 'url-loader',
  include: [
    path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
  ],
};

module.exports = {
  target: ['web', 'es5'],
  entry: {
    app: path.join(__dirname, 'index.web.js'),
  },
  output: {
    path: path.resolve(appDirectory, 'dist'),
    publicPath: '/',
    filename: 'react.native.web.bundle.js',
  },
  resolve: {
    extensions: ['.web.tsx', '.web.ts', '.web.js', '.tsx', '.ts', '.js', 'jsx'],
    alias: {
      'react-native$': 'react-native-web',
    },
    fallback: {
    },
  },
  devServer: {
    historyApiFallback: true,
  },
  module: {
    rules: [
      babelLoaderConfiguration,
      imageLoaderConfiguration,
      svgLoaderConfiguration,
      ttfLoaderConfiguration,
    ],
  },
  plugins: [
    new webpack.ProvidePlugin({
      process: 'process/browser',
    }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'index.html'),
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      __DEV__: JSON.stringify(true),
    }),
  ],
};

Your Environment

software version
react-native 0.75.4
react-native-paper ^5.12.5
node 20.18.0
yarn 4.4.1
"react-native-web": "^0.19.12"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds reproNeeds reproducible examplestale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions