Property 'require' doesn't exist in react native error - reactjs

I think this error from metro bundler,
here is the image for the error:
I try every solution on the net from the official react native GitHub issue.
also try deleting node modules
restart the cache
On the terminal, it didn't show any error but on the device, it threw an error. Any help?
babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset',
'#babel/preset-typescript','#babel/preset-env'],
plugins: [
['#babel/plugin-proposal-class-properties'],
['module:react-native-dotenv',
{
moduleName: '#env',
path: '.env',
},
],
'react-native-reanimated/plugin',
[
'module-resolver',
{
root: ['./src/'],
alias: {
'#assets': './src/assets',
'#components': './src/components',
'#constants': './src/constants',
'#navigation': './src/navigations',
'#screens': './src/screens',
'#utils': './src/utils',
'#styles': './src/styles',
'#redux': './src/redux',
'#config': './src/config',
},
},
],
],
};
metro.config.js
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
};
jestConfig.ts
import mockAsyncStorage from '#react-native-async-storage/async-storage/jest/async-storage-mock';
import 'react-native-gesture-handler/jestSetup';
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
jest.mock('react-native-keyboard-controller', () => ({}));
jest.mock('react-native-bootsplash', () => {
return {
hide: jest.fn(),
show: jest.fn(),
};
});
jest.mock('#notifee/react-native', () => ({}));
jest.mock('#react-native-async-storage/async-storage', () => mockAsyncStorage);
jest.mock('axios', () => {
return {
create: () => {
return {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
interceptors: {
request: {eject: jest.fn(), use: jest.fn()},
response: {eject: jest.fn(), use: jest.fn()},
},
};
},
};
});
export {};
package.json
{
"name": "name",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
},
"dependencies": {
"#notifee/react-native": "^7.2.0",
"#react-native-async-storage/async-storage": "^1.17.10",
"#react-native-masked-view/masked-view": "^0.2.8",
"#react-navigation/bottom-tabs": "^6.4.0",
"#react-navigation/native": "^6.0.13",
"#react-navigation/native-stack": "^6.9.1",
"#reduxjs/toolkit": "^1.8.6",
"#shopify/flash-list": "^1.3.1",
"#types/lodash": "^4.14.191",
"axios": "^1.2.1",
"babel-plugin-module-resolver": "^4.1.0",
"native-base": "^3.4.22",
"react": "18.1.0",
"react-native": "0.70.6",
"react-native-bootsplash": "^4.3.2",
"react-native-dotenv": "^3.4.2",
"react-native-fast-image": "^8.6.3",
"react-native-gesture-handler": "^2.7.1",
"react-native-keyboard-controller": "^1.4.2",
"react-native-modal": "^13.0.1",
"react-native-pager-view": "^6.1.2",
"react-native-pusher-push-notifications": "^2.5.1",
"react-native-reanimated": "^2.11.0",
"react-native-responsive-screen": "^1.4.2",
"react-native-safe-area-context": "3.3.2",
"react-native-screens": "^3.18.2",
"react-native-svg": "12.1.1",
"react-native-tab-view": "^3.3.4",
"react-native-vector-icons": "^9.2.0",
"react-native-webview": "^11.26.0",
"react-redux": "^8.0.4",
"redux": "^4.2.0",
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.4.2",
"ts-jest": "^29.0.3"
},
"devDependencies": {
"#babel/core": "^7.12.9",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"#babel/preset-typescript": "^7.18.6",
"#babel/runtime": "^7.12.5",
"#react-native-community/eslint-config": "^2.0.0",
"#tsconfig/react-native": "^2.0.2",
"#types/jest": "29.0.3",
"#types/react": "^18.0.21",
"#types/react-native": "^0.70.6",
"#types/react-native-dotenv": "^0.2.0",
"#types/react-native-vector-icons": "^6.4.12",
"#types/react-test-renderer": "^18.0.0",
"#types/redux-logger": "^3.0.9",
"#types/redux-persist": "^4.3.1",
"#typescript-eslint/eslint-plugin": "^5.37.0",
"#typescript-eslint/parser": "^5.37.0",
"babel-jest": "29.0.3",
"babel-plugin-transform-require-context": "^0.1.1",
"eslint": "^7.32.0",
"jest": "29.0.3",
"metro-react-native-babel-preset": "0.72.3",
"react-native-typescript-transformer": "^1.2.13",
"react-test-renderer": "18.1.0",
"typescript": "^4.8.3"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"setupFiles": [
"./jestConfig.ts"
],
"transformIgnorePatterns": [
"node_modules/node_modules/react-native-responsive-screen/index.js"
]
}
}

This line:
import 'react-native-gesture-handler/jestSetup';
Should probably be something like:
import jestSetup from 'react-native-gesture-handler/jestSetup';
Check the documentation of the library how that import should work.

Related

Jest test fails on import of .scss. Jest encountered an unexpected token. SyntaxError: Unexpected token '.'

I can't get my tests to run.
When I run 'yarn test' for my react app, I get the dreaded error:
Jest encountered an unexpected token. SyntaxError: Unexpected token '.'
Image: Jest Error
It is failing on the import of the .scss from logon.tsx: import styles from './_logon.scss';
I have tried solutions such as the moduleNameMapper with 'identity-object-proxy' or with a style-mock:
Syntax Error when test component with SASS file imported
What am I doing wrong? I tried copying some of the jest config from the create-react-app as its working there. I put it in jest.config.js instead of package.json. Is this correct?
jest.config.js:
module.exports = {
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.spec.{ts,tsx}', '!src/**/*.node.{ts,tsx}', '!src/typings/**/*'],
coverageDirectory: 'build/coverage',
projects: [
{
setupFiles: ['./jestSetup.js'],
displayName: 'dom',
testEnvironment: 'jsdom',
testMatch: ['**/__tests__/**/*.spec.ts?(x)'],
},
{
coverageDirectory: 'build/coverage',
setupFiles: ['./jestSetup.js'],
displayName: 'node',
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.node.ts?(x)'],
},
],
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
'^.+\\.(css|scss|sass)$': '<rootDir>/jest/cssTransform.js',
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '<rootDir>/jest/fileTransform.js',
},
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$', '^.+\\.(css|sass|scss)$'],
modulePaths: [],
moduleNameMapper: {
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
},
};
_logon.scss
.logon-page {
align-items: center;
display: flex;
height: 100%;
justify-content: center;
width: 100%;
}
.logon-container {
background-color: $ui-01;
color: $text-02;
padding: 16px;
width: 450px;
:global {
.bx--inline-notification__text-wrapper {
width: 300px;
}
.bx--inline-notification {
margin-bottom: 0;
margin-top: 0;
}
.bx--inline-notification__details {
margin-bottom: auto;
margin-top: auto;
}
}
}
.logon-container__button {
float: right;
font-size: 1.3rem;
height: 60px;
max-width: 100%;
width: 50%;
}
.logon-container__input {
background-color: $field-02;
}
.logon-container__hr {
border-bottom: 1px solid $ui-03;
margin-top: 0;
}
.logon-container__gap {
height: 68px;
padding-bottom: 10px;
padding-top: 10px;
}
package.json
{
"name": "graphui",
"private": true,
"scripts": {
"build": "cross-env NODE_ENV=production npm-run-all clean --parallel build:frontend build:server",
"build:server": "webpack --config webpack/webpack.server.babel.js",
"build:frontend": "cross-env IS_BROWSER=true webpack --config webpack/webpack.frontend.babel.js",
"clean": "npm-run-all -s clean:remove clean:recreate",
"clean:remove": "rimraf dist",
"clean:recreate": "mkdirp dist/web/static",
"commit": "cz",
"dev": "cross-env NODE_ENV=development npm-run-all clean --parallel dev:frontend dev:server",
"dev:server": "webpack -w --config webpack/webpack.server.babel.js",
"dev:frontend": "cross-env IS_BROWSER=true webpack-dev-server -d -w --config webpack/webpack.dev.babel.js",
"lint": "npm-run-all --parallel lint:js lint:css lint:commits lint:bundlesize",
"lint:js": "eslint --ext ts,tsx src ",
"lint:css": "stylelint src/**/*",
"lint:commits": "commitlint -e ./.git/COMMIT_EDITMSG",
"lint:bundlesize": "bundlesize",
"test": "jest",
"validate": "npm-run-all --parallel validate:lint validate:gbvalidator",
"validate:lint": "lint-staged --allow-empty",
"validate:gbvalidator": "gbvalidator --pattern \"^(feat|fix|release|refactor|test|docs|build|ci|perf|styles)/([a-z0-9_#-./]){3,40}\\$\" --message \"See CONTRIBUTING.md for examples of valid branch names\"",
"validated:translation": "node missingTranslations.js"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "yarn run validate",
"post-merge": "node postMerge.js"
}
},
"lint-staged": {
"src/**/*.{ts,tsx}": [
"eslint --fix",
"jest --bail --ci --silent --findRelatedTests"
],
"src/**/*.{css,scss}": [
"stylelint --fix"
]
},
"dependencies": {
"#carbon/grid": "^10.15.0",
"#carbon/icons-react": "^10.19.0",
"#carbon/themes": "^10.21.0",
"#carbon/type": "^10.16.0",
"#egjs/hammerjs": "^2.0.17",
"#emotion/cache": "^10.0.29",
"#emotion/core": "^10.0.35",
"#emotion/styled": "^10.0.27",
"#loadable/component": "^5.13.2",
"#loadable/server": "^5.13.2",
"#reduxjs/toolkit": "^1.4.0",
"blueimp-md5": "^2.18.0",
"body-parser": "^1.19.0",
"carbon-components": "^10.22.0",
"carbon-components-react": "^7.22.0",
"carbon-icons": "^7.0.7",
"chokidar": "^3.4.3",
"classnames": "^2.2.6",
"compression": "^1.7.4",
"cookie-parser": "^1.4.5",
"create-emotion-server": "^10.0.27",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"express-winston": "^4.0.5",
"gremlin": "^3.4.8",
"helmet": "^4.1.1",
"i18next": "^19.8.2",
"i18next-browser-languagedetector": "^6.0.1",
"i18next-fs-backend": "^1.0.7",
"i18next-http-backend": "^1.0.21",
"i18next-http-middleware": "^3.0.6",
"ibm_db": "^2.7.1",
"identity-obj-proxy": "^3.0.0",
"immer": "^7.0.9",
"java.io": "^2.4.0",
"js-yaml": "^3.14.0",
"jsonwebtoken": "^8.5.1",
"keycharm": "^0.4.0",
"lodash": "^4.17.20",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"prop-types": "^15.7.2",
"react": "^16.14.0",
"react-dnd": "^11.1.3",
"react-dnd-html5-backend": "^11.1.3",
"react-dom": "^16.14.0",
"react-error-boundary": "^3.0.2",
"react-hook-form": "^6.9.2",
"react-i18next": "^11.7.3",
"react-redux": "^7.2.1",
"react-router-dom": "^5.2.0",
"react-window": "^1.8.5",
"reactour": "^1.18.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"remove": "^0.1.5",
"serialize-javascript": "^5.0.1",
"serve-static": "^1.14.1",
"styled-components": "^5.2.0",
"uuid": "^8.3.1",
"vis-data": "^7.1.0",
"vis-network": "^8.4.0",
"vis-util": "^4.3.4",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0"
},
"devDependencies": {
"#babel/cli": "^7.12.1",
"#babel/core": "^7.12.1",
"#babel/node": "^7.12.1",
"#babel/plugin-proposal-class-properties": "^7.12.1",
"#babel/plugin-proposal-nullish-coalescing-operator": "^7.12.1",
"#babel/plugin-proposal-optional-chaining": "^7.12.1",
"#babel/plugin-proposal-private-methods": "^7.12.1",
"#babel/preset-env": "^7.12.1",
"#babel/preset-react": "^7.12.1",
"#babel/preset-typescript": "^7.12.1",
"#commitlint/cli": "^11.0.0",
"#commitlint/config-conventional": "^11.0.0",
"#hookform/devtools": "^2.2.1",
"#lcluber/gitbranchvalidator": "^1.3.0",
"#loadable/babel-plugin": "^5.13.2",
"#loadable/webpack-plugin": "^5.13.0",
"#pmmmwh/react-refresh-webpack-plugin": "^0.4.2",
"#testing-library/dom": "^7.26.0",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.8",
"#types/blueimp-md5": "^2.7.0",
"#types/body-parser": "^1.19.0",
"#types/carbon-components": "^10.15.0",
"#types/carbon-components-react": "^7.10.10",
"#types/carbon__icons-react": "^10.17.1",
"#types/carbon__type": "^0.0.1",
"#types/classnames": "^2",
"#types/compression": "^1.7.0",
"#types/cookie-parser": "^1.4.2",
"#types/copy-webpack-plugin": "^6.0.0",
"#types/eslint": "^7.2.4",
"#types/eslint-plugin-prettier": "^3.1.0",
"#types/express": "^4.17.8",
"#types/gremlin": "^3.4.6",
"#types/i18next-fs-backend": "^1.0.0",
"#types/ibm_db": "^2.0.6",
"#types/jest": "^26.0.14",
"#types/js-yaml": "^3.12.5",
"#types/jsonwebtoken": "^8.5.0",
"#types/loadable__component": "^5.13.1",
"#types/loadable__server": "^5.12.3",
"#types/loadable__webpack-plugin": "^5.7.1",
"#types/lodash": "^4",
"#types/mini-css-extract-plugin": "^1.0.0",
"#types/mkdirp": "^1.0.1",
"#types/node": "^14.11.8",
"#types/node-sass": "^4.11.1",
"#types/optimize-css-assets-webpack-plugin": "^5.0.1",
"#types/passport": "^1.0.4",
"#types/passport-jwt": "^3.0.3",
"#types/passport-local": "^1.0.33",
"#types/prettier": "^2.1.2",
"#types/prop-types": "^15.7.3",
"#types/react": "^16.9.52",
"#types/react-dom": "^16.9.8",
"#types/react-redux": "^7.1.9",
"#types/react-router-dom": "^5.1.6",
"#types/react-window": "^1.8.2",
"#types/reactour": "^1.17.1",
"#types/rimraf": "^3.0.0",
"#types/serialize-javascript": "^4.0.0",
"#types/serve-static": "^1.13.5",
"#types/styled-components": "^5.1.4",
"#types/supertest": "^2.0.10",
"#types/terser-webpack-plugin": "^4",
"#types/testing-library__jest-dom": "^5.9.4",
"#types/uuid": "^8.3.0",
"#types/webpack": "^4.41.22",
"#types/webpack-dev-server": "^3.11.0",
"#types/webpack-env": "^1.15.3",
"#types/webpack-node-externals": "^2.5.0",
"#types/workbox-webpack-plugin": "^5.1.5",
"#typescript-eslint/eslint-plugin": "^4.4.1",
"#typescript-eslint/parser": "^4.4.1",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.5.2",
"babel-loader": "^8.1.0",
"babel-plugin-emotion": "^10.0.33",
"babel-plugin-module-resolver": "^4.0.0",
"bundlesize": "^0.18.0",
"chalk": "^4.1.0",
"commitizen": "^4.2.1",
"commitlint": "^11.0.0",
"copy-webpack-plugin": "^6.2.1",
"cross-env": "^7.0.2",
"css-loader": "^5.0.0",
"cz-conventional-changelog": "^3.3.0",
"dotenv": "^8.2.0",
"eslint": "^7.11.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-airbnb-typescript": "^11.0.0",
"eslint-config-prettier": "^6.12.0",
"eslint-config-react": "^1.1.7",
"eslint-import-resolver-babel-module": "^5.1.2",
"eslint-import-resolver-node": "^0.3.4",
"eslint-plugin-generic-intl": "^1.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.4",
"eslint-plugin-react-hooks": "^4.1.2",
"husky": "^4.3.0",
"jest": "^26.5.3",
"jest-environment-jsdom-fourteen": "^1.0.1",
"jest-transform-css": "^2.1.0",
"lint-staged": "^10.4.0",
"mini-css-extract-plugin": "^1.0.0",
"mkdirp": "^1.0.4",
"msw": "^0.21.2",
"node-sass": "^4.14.1",
"nodemon": "^2.0.5",
"nodemon-webpack-plugin": "^4.3.2",
"npm-run-all": "^4.1.5",
"open": "^7.3.0",
"optimize-css-assets-webpack-plugin": "^5.0.4",
"postcss": "^8.1.1",
"postcss-loader": "^4.0.4",
"postcss-preset-env": "^6.7.0",
"preload-webpack-plugin": "^2.3.0",
"prettier": "^2.1.2",
"prettier-eslint": "^11.0.0",
"react-refresh": "^0.8.3",
"rimraf": "^3.0.2",
"sass-loader": "^10.0.3",
"style-loader": "^2.0.0",
"stylelint": "^13.7.2",
"stylelint-config-prettier": "^8.0.2",
"stylelint-config-sass-guidelines": "^7.1.0",
"stylelint-config-standard": "^20.0.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-prettier": "^1.1.2",
"supertest": "^5.0.0",
"terser-webpack-plugin": "^4.2.3",
"typescript": "^4.0.3",
"typescript-plugin-css-modules": "^2.7.0",
"unicoderegexp": "^0.4.1",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0",
"webpack-node-externals": "^2.5.2",
"workbox-webpack-plugin": "^5.1.4"
}
}
babel.config.js
/* eslint-disable #typescript-eslint/no-var-requires */
const path = require('path');
const browsers = ['last 2 Chrome versions', 'last 2 Safari versions', 'last 2 Edge versions', 'Firefox ESR'];
const { env } = process;
const plugins = [
'emotion',
'#loadable/babel-plugin',
'#babel/plugin-proposal-optional-chaining',
'#babel/plugin-proposal-nullish-coalescing-operator',
['#babel/plugin-proposal-class-properties', { loose: true }],
['#babel/plugin-proposal-private-methods', { loose: true }],
[
'babel-plugin-module-resolver',
{
alias: {
'#frontend': path.resolve(process.cwd(), path.join('src', 'frontend')),
'#server': path.resolve(process.cwd(), path.join('src', 'server')),
'#shared': path.resolve(process.cwd(), path.join('src', 'shared')),
'#typings': path.resolve(process.cwd(), path.join('src', 'typings')),
'#testhelpers': path.resolve(process.cwd(), path.join('src', 'testhelpers')),
},
extensions: ['.ts', '.tsx', '.json'],
},
],
];
const targets = {
browsers,
node: 'current',
};
if (env.IS_BROWSER) {
delete targets.node;
} else {
delete targets.browser;
}
const presets = [
[
'#babel/preset-react',
{
runtime: 'automatic',
},
],
[
'#babel/preset-env',
{
shippedProposals: true,
targets,
},
],
'#babel/preset-typescript',
];
if (env.NODE_ENV !== 'production' && env.IS_BROWSER) {
plugins.unshift(['react-refresh/babel', { skipEnvCheck: true }]);
}
const config = {
plugins,
presets,
env: {
debug: {
sourceMaps: 'inline',
retainLines: true,
},
development: {
compact: false,
},
},
};
module.exports = config;
webpack.base.js
import 'dotenv/config';
import path from 'path';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
const {
env: { NODE_ENV },
} = process;
const IS_PROD = NODE_ENV === 'production';
const rootDirectory = path.join(__dirname, '..');
const nodeModules = path.join(rootDirectory, 'node_modules');
const config = {
mode: IS_PROD ? 'production' : 'development',
performance: { hints: false },
devtool: IS_PROD ? 'cheap-module-source-map' : 'inline-source-map',
module: {
rules: [
{
test: /\.(s?)css$/,
sideEffects: true,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !IS_PROD,
},
},
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]--[hash:base64:5]',
exportLocalsConvention: 'dashesOnly',
},
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
additionalData: `
$feature-flags: (
enable-css-custom-properties: true,
grid-columns-16: true,
ui-shell: true,
);
#import 'carbon-components/scss/globals/scss/vars';
#import '#carbon/type/scss/styles';
`,
},
},
],
},
{
test: /node_modules\/vis-.*\.js$/,
use: [
{
loader: 'babel-loader',
},
],
},
{
exclude: {
test: /node_modules/,
exclude: [
path.resolve(__dirname, 'node_modules', 'vis-network'),
path.resolve(__dirname, 'node_modules', 'vis-data'),
path.resolve(__dirname, 'node_modules', 'vis-utils'),
],
},
test: /\.(t|j)sx?$/,
use: [
{
loader: 'babel-loader',
},
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: NODE_ENV !== 'production' ? '[name].css' : '[name].[contenthash].css',
chunkFilename: NODE_ENV !== 'production' ? '[name].[id].css' : '[name].[id].[contenthash].css',
}),
],
resolve: {
extensions: ['.js', '.tsx', '.ts', '.json', '.css', '.scss'],
modules: [path.resolve(nodeModules), path.resolve(rootDirectory)],
},
stats: {
cached: false,
colors: true,
entrypoints: false,
children: false,
},
};
export default config;

NPM package component gives an error if reexported in another library

I have a component library and one of our components uses react-toggle library. When I try to use our component in another app I get the error Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. I am using rollup as a bundler, here's the configuration.
import path from 'path';
import babel from '#rollup/plugin-babel';
import resolve from '#rollup/plugin-node-resolve';
import commonjs from '#rollup/plugin-commonjs';
import image from '#rollup/plugin-image';
import filesize from 'rollup-plugin-filesize';
import localResolve from 'rollup-plugin-local-resolve';
import { terser } from 'rollup-plugin-terser';
import copy from 'rollup-plugin-copy';
import pkg from './package.json';
const SOURCE_DIR = path.resolve(__dirname, 'src');
const INPUT_FILE_PATH = `${SOURCE_DIR}/index.ts`;
const OUTPUT_NAME = 'InPlayerUI';
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
const GLOBALS = {
react: 'React',
'react-dom': 'ReactDOM',
'styled-components': 'styled-components',
};
const PLUGINS = [
image(),
resolve({
mainFields: ['module', 'main', 'jsnext:main', 'browser'],
extensions,
}),
commonjs({ include: ['node_modules/**'] }),
babel({
include: ['src/**/*'],
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions,
}),
localResolve(),
terser(),
filesize(),
copy({
targets: [{ src: './index.d.ts', dest: 'dist' }],
}),
];
const EXTERNAL = ['react', 'react-dom', 'styled-components'];
const OUTPUT_DATA = [
{
file: pkg.browser,
format: 'umd',
},
{
file: pkg.main,
format: 'cjs',
},
{
file: pkg.module,
format: 'es',
},
];
const config = OUTPUT_DATA.map(({ file, format }) => ({
input: INPUT_FILE_PATH,
output: {
file,
format,
name: OUTPUT_NAME,
globals: GLOBALS,
},
external: EXTERNAL,
plugins: PLUGINS,
}));
export default config;
and the package.json
{
"name": "#inplayer-org/inplayer-ui",
"sideEffects": false,
"version": "2.0.4",
"author": "InPlayer",
"description": "InPlayer React UI Components",
"main": "dist/inplayer-ui.cjs.js",
"module": "dist/inplayer-ui.esm.js",
"browser": "dist/inplayer-ui.umd.js",
"jsnext:main": "dist/inplayer-ui.esm.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/inplayer-org/inplayer-ui"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/inplayer-org/inplayer-ui/issues"
},
"homepage": "https://github.com/inplayer-org/inplayer-ui#readme",
"scripts": {
"start": "yarn run storybook",
"storybook": "start-storybook -p 3001",
"build:storybook": "build-storybook -c .storybook -o docs",
"clean:dist": "rimraf ./dist",
"build": "yarn run clean:dist && npx rollup -c",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"lint:watch": "esw -w src/**",
"prepublishOnly": "yarn build",
"lint:style": "stylelint -- \"./src/**/*.js\""
},
"dependencies": {
"react-toggle": "^4.1.1"
},
"peerDependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"styled-components": "^5.1.1"
},
"devDependencies": {
"#babel/cli": "^7.10.5",
"#babel/core": "^7.10.5",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-proposal-private-methods": "^7.10.4",
"#babel/plugin-transform-classes": "^7.10.4",
"#babel/preset-env": "^7.10.4",
"#babel/preset-react": "^7.10.4",
"#babel/preset-typescript": "^7.10.4",
"#rollup/plugin-babel": "^5.1.0",
"#rollup/plugin-commonjs": "^14.0.0",
"#rollup/plugin-image": "^2.0.5",
"#rollup/plugin-node-resolve": "^8.4.0",
"#storybook/addon-docs": "^5.3.19",
"#storybook/addon-info": "^5.3.19",
"#storybook/addon-knobs": "^5.3.19",
"#storybook/react": "^5.3.19",
"#types/dompurify": "^2.0.2",
"#types/lodash": "^4.14.157",
"#types/react": "^16.9.43",
"#types/react-dates": "^17.1.13",
"#types/react-dom": "^16.9.8",
"#types/react-toggle": "^4.0.2",
"#types/styled-components": "^5.1.1",
"#typescript-eslint/eslint-plugin": "3.7.0",
"#typescript-eslint/parser": "3.7.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"dompurify": "^2.0.12",
"eslint": "^7.5.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.8",
"eslint-watch": "^7.0.0",
"html-react-parser": "^0.13.0",
"lodash-es": "^4.17.15",
"moment": "^2.27.0",
"polished": "^3.6.5",
"prettier": "^2.0.5",
"rc-progress": "^3.0.0",
"react": "^16.13.1",
"react-addons-shallow-compare": "^15.6.2",
"react-dates": "^21.8.0",
"react-dom": "^16.13.1",
"react-icons": "^3.10.0",
"react-paginate": "^6.3.2",
"rimraf": "^3.0.2",
"rollup-plugin-copy": "^3.3.0",
"rollup-plugin-filesize": "^9.0.2",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup-plugin-terser": "^6.1.0",
"styled-components": "^5.1.1",
"styled-components-modifiers": "^1.2.5",
"styled-tools": "^1.7.2",
"stylelint": "^13.6.1",
"stylelint-config-standard": "^20.0.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
"typescript": "^3.9.7"
}
}
This is the component
import React, { ChangeEvent } from 'react';
import styled from 'styled-components';
import Toggle from 'react-toggle';
import SwitchWrapper from './SwitchWrapper';
import Label from '../Label';
type SwitchProps = {
/**
* Whether the switch is checked or not
*/
checked: boolean;
/**
* Disable switch
*/
disabled?: boolean;
/**
* Id
*/
id: string;
/**
* Switch label
*/
label: string;
/**
* Function executed when the switch value changes
*/
onChange: (checked: boolean) => void;
};
const MarginLeftLabel = styled(Label)`
margin-right: 0.5rem;
`;
const Switch = ({ id, checked, label, disabled = false, onChange }: SwitchProps) => {
const onToggleChange = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target) {
onChange(e.target.checked);
}
};
return (
<SwitchWrapper>
<MarginLeftLabel disabled={disabled} htmlFor={id}>
{label}
</MarginLeftLabel>
<Toggle
id={id}
disabled={disabled}
checked={checked}
onChange={onToggleChange}
icons={false}
/>
</SwitchWrapper>
);
};
export default Switch;
I have seen some suggestions that the import could be wrong but I've made sure it isn't and if I use react-toggle directly in another app it works.
Note: I've tried using different npm packages and reexport components from them, it works fine, so I suppose it has something to do with react-toggle itself.

my react app is not rendering in internet explorer (because of arrow function)

My react+redux app is not working on Internet explorer 11. It is working fine on edge, Firefox and chrome.
I have already tried so many answers available on internet like this but didn't get any success, below attached error is I am getting in console
when I opened this error it shows like this
this file is generating when I am serving my app and can't change anything manually
package.json
{
"jest": {
"roots": [
"<rootDir>/"
],
"moduleFileExtensions": [
"js",
"jsx",
"json"
],
"testURL": "http://localhost",
"moduleDirectories": [
"node_modules",
"utils"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"text"
],
"collectCoverageFrom": [
"**/*.{js,jsx}",
"!**/node_modules/**",
"!**/flow-typed/**",
"!**/vendor/**",
"!./*"
],
"setupFilesAfterEnv": [
"./utils/jestUtils/setupTests.js"
],
"testEnvironment": "./utils/jestUtils/freezeDateEnvironment.js",
"moduleNameMapper": {
"\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/utils/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/utils/__mocks__/fileMock.js"
}
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#synapsestudios/react-drop-n-crop": "^0.2.0",
"attr-accept": "^1.1.0",
"axios": "^0.19.0",
"babel-polyfill": "^6.26.0",
"classnames": "^2.2.6",
"clone-deep": "^3.0.1",
"d3": "^4.9.1",
"deep-equal": "^1.0.1",
"draft-js": "0.10.5",
"draft-js-buttons": "2.0.1",
"draft-js-export-html": "1.2.0",
"file-saver": "^1.3.8",
"formsy-react": "^0.19.5",
"history": "^4.6.3",
"libphonenumber-js": "1.7.16",
"locate-path": "^2.0.0",
"mime": "^2.0.3",
"moment": "^2.18.1",
"nanoid": "^1.0.1",
"node": "^12.10.0",
"node-gyp": "^5.0.3",
"path-to-regexp": "^2.2.1",
"prettier": "^1.6.1",
"prop-types": "^15.5.10",
"qs": "6.7.0",
"query-string": "5.1.1",
"react": "16.7.0",
"react-app-polyfill": "^1.0.5",
"react-autofill": "^1.1.4",
"react-cropper": "^1.0.0",
"react-datepicker": "^1.4.1",
"react-dom": "16.7.0",
"react-dropzone": "^4.1.3",
"react-geosuggest": "2.12.0",
"react-html-parser": "^2.0.2",
"react-infinite-scroller": "^1.1.1",
"react-input-range": "^1.1.4",
"react-intl": "^2.4.0",
"react-intl-redux": "^0.7.0",
"react-media": "^1.8.0",
"react-modal": "^2.3.3",
"react-phone-number-input": "2.3.2",
"react-quill": "^1.0.0",
"react-redux": "^5.0.5",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.2",
"react-scripts": "1.0.10",
"react-select": "^1.0.0-rc.5",
"react-select-plus": "^1.1.0",
"react-tooltip": "^3.3.0",
"react-transition-group": "2.5.3",
"recompact": "^3.3.0",
"redux": "4.0.1",
"redux-form": "7.3.0",
"redux-thunk": "^2.2.0",
"reselect": "^3.0.1",
"resolve-url-loader": "^2.0.2",
"select2": "^4.0.3",
"snake-case": "^2.1.0",
"svg-inline-loader": "^0.7.1",
"svg-url-loader": "^2.0.2",
"validator": "^8.0.0"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"ie 11",
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "7.4.4",
"#babel/node": "7.4.5",
"#babel/plugin-proposal-class-properties": "7.4.4",
"#babel/plugin-proposal-object-rest-spread": "7.4.4",
"#babel/plugin-proposal-optional-chaining": "7.2.0",
"#babel/plugin-transform-arrow-functions": "7.2.0",
"#babel/plugin-transform-classes": "7.4.4",
"#babel/plugin-transform-runtime": "7.4.4",
"#babel/preset-env": "7.4.4",
"#babel/preset-flow": "7.0.0",
"#babel/preset-react": "7.0.0",
"autoprefixer": "^9.5.1",
"axios-mock-adapter": "^1.10.0",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.6",
"babel-plugin-react-intl": "3.0.1",
"babel-plugin-react-intl-auto": "1.7.0",
"colors": "1.3.3",
"cross-env": "^5.1.4",
"css-loader": "3.0.0",
"cssnano": "4.1.10",
"del-cli": "^1.1.0",
"enzyme": "3.8.0",
"enzyme-adapter-react-16": "1.7.1",
"eslint": "5.16.0",
"eslint-config-airbnb": "^15.1.0",
"eslint-import-resolver-webpack": "^0.10.1",
"eslint-loader": "^2.1.1",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-flowtype": "3.8.2",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jest": "22.1.2",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.1.0",
"file-loader": "3.0.1",
"flow-babel-webpack-plugin": "1.1.1",
"flow-bin": "0.102.0",
"flow-typed": "2.5.2",
"glob": "^7.1.2",
"jest": "24.8.0",
"mini-css-extract-plugin": "^0.6.0",
"node-sass": "^4.12.0",
"postcss-easy-import": "^3.0.0",
"postcss-loader": "^3.0.0",
"react-docgen": "^2.20.1",
"react-hot-loader": "^4.3.12",
"react-test-renderer": "^15.6.2",
"redux-devtools": "3.5.0",
"redux-devtools-dock-monitor": "1.1.3",
"redux-devtools-log-monitor": "1.4.0",
"redux-mock-store": "^1.4.0",
"sass-loader": "^6.0.6",
"uglifyjs-webpack-plugin": "^1.1.2",
"url-loader": "^2.1.0",
"webpack": "4.31.0",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "3.3.1"
}
}
.babelrc
{
"presets": [
["#babel/preset-env", { "targets": { "ie": "11", "esmodules": true }}],
"#babel/preset-react",
"#babel/preset-flow"
],
"plugins": [
"#babel/plugin-transform-runtime",
"#babel/plugin-proposal-object-rest-spread",
"#babel/plugin-proposal-class-properties",
"#babel/plugin-proposal-optional-chaining",
"react-hot-loader/babel",
"#babel/plugin-transform-classes",
"#babel/plugin-transform-arrow-functions"
],
"env": {
"lang":{
"plugins": ["#babel/plugin-syntax-object-rest-spread", ["react-intl", {
"messagesDir": "../assets/build-translations/messages/"
}]]
}
}
}
webpackconfig.js
const { DefinePlugin, NamedModulesPlugin } = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const FlowBabelWebpackPlugin = require('flow-babel-webpack-plugin');
const path = require('path');
require('babel-polyfill');
const webpackAlias = require('./webpack.config.alias');
const pagenames = require('./utils/pageNames');
const DEV_SERVER_PUBLIC_PATH = 'http://192.168.1.14:44444/webpack/static/';
const environment = process.env && process.env.NODE_ENV || 'production';
const PROD = environment === 'production';
const environmentValues = new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(environment)
});
const UglifyJSPluginInit = new UglifyJSPlugin({
uglifyOptions: {
beautify: false,
ecma: 6,
compress: true,
comments: false,
parallel: true,
sourceMap: true,
warnings: false,
}
});
const flowPlugin = environment === 'lang' ? [] : [new FlowBabelWebpackPlugin()];
const commonPlugins = [environmentValues, ...flowPlugin, new MiniCssExtractPlugin({
filename: '[name].css',
})];
const productionPlugins = [UglifyJSPluginInit];
const pluginsArr = PROD
? [...commonPlugins, ...productionPlugins]
: [...commonPlugins, new NamedModulesPlugin()];
const devtool = PROD ? false : 'cheap-source-map';
const imagePublicPath = PROD ? '/static/' : DEV_SERVER_PUBLIC_PATH;
const modeAlias = PROD ? 'production' : 'development';
const outputPath = path.resolve(__dirname, '../assets');
const entry = {
app: './utils/app.js',
'password-reset': './utils/scss/password-reset.scss',
[pagenames.pageNames1]: './fileurl.js',
[pagenames.pageNames2]: './fileurl.js',
[pagenames.pageNames3]: './fileurl.js',
[pagenames.pageNames4]: './fileurl.js',
};
const config = {
...webpackAlias,
mode: modeAlias,
optimization: {
splitChunks: {
cacheGroups: {
commons: {
minChunks: 13,
name: 'commons',
chunks: 'all',
},
},
}
},
context: __dirname,
entry,
devtool,
output: {
path: outputPath,
publicPath: '/webpack/static/',
filename: '[name].js',
},
devServer: {
contentBase: outputPath,
port: 44444,
host: '192.168.1.14',
publicPath: DEV_SERVER_PUBLIC_PATH,
stats: {
errors: true,
warnings: true,
assets: false,
chunks: true,
chunkGroups: true,
chunkModules: true,
chunkOrigins: true,
builtAt: false,
cached: false,
timings: false,
outputPath: false,
modules: true,
entrypoints: true,
},
headers: {
"Access-Control-Allow-Origin": "\*",
}
},
module: {
rules: [
{
test: /\.svg$/,
loader: 'svg-url-loader'
},
{
test: /\.js$/,
exclude: path.resolve(__dirname, "node_modules"),
include: __dirname,
use: [
{
loader: 'babel-loader',
},
{
loader: 'eslint-loader',
options: {
failOnWarning: false,
failOnError: true
}
}
]
},
{
test: /\.(png|jpg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000, // Convert images < 10kb to base64 strings
publicPath: imagePublicPath // Since the fallback is file-loader, We end up serving from /static/{file} or during development - wherever the server is located
}
}
]
},
{
test: /\.scss|css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: !PROD,
reloadAll: true,
},
},
{
loader: 'css-loader',
options: {
sourceMap: true,
}
},
{ loader: 'postcss-loader' },
{
loader: 'resolve-url-loader'
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
]
}
]
},
plugins: pluginsArr
};
console.log('process.env.NODE_ENV', environment);
module.exports = config;
Any help would be appreciated, Thanks in advance.
I can see on your .babelrc presets, under #babel/preset-env you are defining "esmodules": true.
Based on the #babel/preset-env documentation, if you set esmodules to true, browsers targets will be ignored.
Please note: when specifying the esmodules target, browsers targets will be ignored.
Can you try changing this option to see if you have any luck?
Also, I suggest you try a production build to see if you get the same error/behavior.
Try to delete the package-lock.json and the node_modules folder, then using the following package.json content (change the name to your application name. In this file, I changed some package version.) and execute the npm install commands to install the related package. I have created a sample using this file, it works well on my side.
{
"name": "<your application name>",
"version": "0.1.0",
"private": true,
"dependencies": {
"#synapsestudios/react-drop-n-crop": "^0.2.0",
"attr-accept": "^1.1.0",
"axios": "^0.19.0",
"babel-polyfill": "^6.26.0",
"classnames": "^2.2.6",
"clone-deep": "^3.0.1",
"d3": "^4.9.1",
"deep-equal": "^1.0.1",
"draft-js": "0.10.5",
"draft-js-buttons": "2.0.1",
"draft-js-export-html": "1.2.0",
"file-saver": "^1.3.8",
"formsy-react": "^0.19.5",
"history": "^4.6.3",
"libphonenumber-js": "1.7.16",
"locate-path": "^2.0.0",
"mime": "^2.0.3",
"moment": "^2.18.1",
"nanoid": "^1.0.1",
"node": "^12.10.0",
"node-gyp": "^5.0.3",
"path-to-regexp": "^2.2.1",
"prettier": "^1.6.1",
"prop-types": "^15.5.10",
"qs": "6.7.0",
"query-string": "5.1.1",
"raf": "^3.4.1",
"react": "^16.8.6",
"react-app-polyfill": "^1.0.1",
"react-autofill": "^1.1.4",
"react-cropper": "^1.0.0",
"react-datepicker": "^1.4.1",
"react-dom": "^16.8.6",
"react-dropzone": "^4.1.3",
"react-geosuggest": "2.12.0",
"react-html-parser": "^2.0.2",
"react-infinite-scroller": "^1.1.1",
"react-input-range": "^1.3.0",
"react-intl": "^2.4.0",
"react-intl-redux": "^0.7.0",
"react-media": "^1.8.0",
"react-modal": "^2.3.3",
"react-phone-number-input": "2.3.2",
"react-quill": "^1.0.0",
"react-redux": "^7.1.0",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.2",
"react-scripts": "3.0.1",
"react-select": "^1.0.0-rc.5",
"react-select-plus": "^1.1.0",
"react-tooltip": "^3.3.0",
"react-transition-group": "2.5.3",
"recompact": "^3.3.0",
"redux": "^4.0.4",
"redux-form": "^8.2.5",
"redux-thunk": "^2.2.0",
"reselect": "^3.0.1",
"resolve-url-loader": "^2.0.2",
"select2": "^4.0.3",
"snake-case": "^2.1.0",
"svg-inline-loader": "^0.7.1",
"svg-url-loader": "^2.0.2",
"validator": "^8.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"ie 11",
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "7.4.4",
"#babel/node": "7.4.5",
"#babel/plugin-proposal-class-properties": "7.4.4",
"#babel/plugin-proposal-object-rest-spread": "7.4.4",
"#babel/plugin-proposal-optional-chaining": "7.2.0",
"#babel/plugin-transform-arrow-functions": "7.2.0",
"#babel/plugin-transform-classes": "7.4.4",
"#babel/plugin-transform-runtime": "7.4.4",
"#babel/preset-env": "7.4.4",
"#babel/preset-flow": "7.0.0",
"#babel/preset-react": "7.0.0",
"autoprefixer": "^9.5.1",
"axios-mock-adapter": "^1.10.0",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.6",
"babel-plugin-react-intl": "3.0.1",
"babel-plugin-react-intl-auto": "1.7.0",
"colors": "1.3.3",
"cross-env": "^5.1.4",
"css-loader": "3.0.0",
"cssnano": "4.1.10",
"del-cli": "^1.1.0",
"enzyme": "3.8.0",
"enzyme-adapter-react-16": "1.7.1",
"eslint": "5.16.0",
"eslint-config-airbnb": "^15.1.0",
"eslint-import-resolver-webpack": "^0.10.1",
"eslint-loader": "^2.1.1",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-flowtype": "3.8.2",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jest": "22.1.2",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.1.0",
"file-loader": "3.0.1",
"flow-babel-webpack-plugin": "1.1.1",
"flow-bin": "0.102.0",
"flow-typed": "2.5.2",
"glob": "^7.1.2",
"jest": "24.8.0",
"mini-css-extract-plugin": "^0.6.0",
"node-sass": "^4.12.0",
"postcss-easy-import": "^3.0.0",
"postcss-loader": "^3.0.0",
"react-docgen": "^2.20.1",
"react-hot-loader": "^4.3.12",
"react-test-renderer": "^15.6.2",
"redux-devtools": "^3.5.0",
"redux-devtools-dock-monitor": "1.1.3",
"redux-devtools-log-monitor": "1.4.0",
"redux-mock-store": "^1.4.0",
"sass-loader": "^6.0.6",
"uglifyjs-webpack-plugin": "^1.1.2",
"url-loader": "^2.1.0",
"webpack": "4.31.0",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "3.3.1"
}
}
Unless you're supplying a .babelrc or babel.config.js (documentation) that you didn't include with your question, then your #babel/preset-env dependency may not be getting applied during transpilation.
The default behavior for #babel/preset-env is to transpile down to EMCAScript 2015+, which would then work for IE11.
Sidenote, if no targets are specified, #babel/preset-env will transform all ECMAScript 2015+ code by default.
I would suggest adding one of those. For example:
.babelrc
{
"presets": ["#babel/preset-env"]
}
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["#babel/preset-env"],
};
}
If the arrow function is ending up in the browser, then it doesn’t look like you are transpiling your code before serving it to the browser. I see your installing webpack and Babel, but their are no scripts in your package.json to run either. As you have not shared a webpack config, you give us little to go on without seeing how the rest of your project is setup
I would suggest moving your code into create-react-app and try running it from that.
Till now Internet Explorer 11 does not ECMAScript 6 (ES6) version of JavaScript.You have to write the function in old ways (curly brackets).Some thing like below:
var name = ""
function func1(filter){
if (filter instanceof RegExp === true)
return filter.text(name)
else{
return filter === name
}
}
function testParameter(name, filters){
name = this.name
return filters.some(func1);
}
Array.some() function is of ES5 which is supported in Internet Explorer.
Reference:
https://interoperability.blob.core.windows.net/web/MS-ES5EX/%5BMS-ES5EX%5D-180828.pdf

React: Typescript or Babel error - You may need an appropriate loader to handle this file type

I have a custom hook and it works great! However, I'm getting Typescript errors if I simply use React.useState and React.useEffect. To fix this, I import in React and use (React as any).useState and (React as any).useEffect and then I get the following error that doesn't let me compile.
Hook:
import * as React from 'react';
import imageApi from 'xcel-api-generator/dist/image';
export const useUserProfileImage = (size, userProfileImage) => {
const [userImage, setUserImage] = (React as any).useState(null);
(React as any).useEffect(() => {
const runEffect = async () => {
const userImageCall = await imageApi.getTypeByImageType( {"imageType": size} )
setUserImage(userImageCall.url);
};
runEffect();
}, [userProfileImage])
return [userImage]
}
Error:
../rsv8-components/src/hooks/userProfileImage.ts 5:43
Module parse failed: Unexpected token (5:43)
You may need an appropriate loader to handle this file type.
|
| export const useUserProfileImage = (size, userProfileImage) => {
> const [userImage, setUserImage] = (React as any).useState(null);
| (React as any).useEffect(() => {
| const runEffect = async () => {
I'm totally clueless when it comes to webpack/babel etc... Could someone help guide me through this! Thanks!
Here is my local package.json file:
{
"name": "rsv8-components",
"version": "0.3.11",
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
"dependencies": {
"#types/react-avatar-editor": "^10.3.5",
"react-avatar-editor": "^12.0.0-beta.0",
"react-scroll-to": "^1.2.2",
"react-slick": "^0.23.1",
"react-slick-deux": "https://github.com/aboorde/react-slick-deux#master",
"slick-carousel": "^1.8.1",
"tslib": "^1.9.3",
"xcel-observer": "*"
},
"peerDependencies": {
"react": "^16.8.0",
"react-bootstrap": "0.32.1",
"react-dom": "^16.8.0",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.2.2",
"react-toastify": "^4.1.0",
"recompose": "^0.28.1",
"styled-components": "^2.4.0",
"xcel-asset-service": "*",
"xcel-react-animations": "*",
"xcel-react-core": "*",
"xcel-redux-orm": "*",
"xcel-util": "*"
},
"scripts": {
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"fix-me-test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject",
"dist": "tsc --declaration",
"dist:watch": "tsc --declaration --watch",
"storybook": "start-storybook -p 6006 -c .storybook"
},
"devDependencies": {
"//": "peerDependencies",
"#storybook/addon-actions": "^4.0.0",
"#storybook/addon-knobs": "^4.0.0",
"#storybook/react": "^4.0.0",
"#types/jest": "^22.2.3",
"#types/node": "^8.9.1",
"#types/react": "^16.3.16",
"#types/react-dom": "^16.0.4",
"#types/react-router-dom": "^4.2.3",
"awesome-typescript-loader": "^3.4.1",
"enzyme-adapter-react-16": "^1.1.0",
"jest": "^23.6.0",
"jest-cli": "^23.4.1",
"jest-styled-components": "4.9.0",
"react": "^16.8.0",
"react-bootstrap": "0.32.1",
"react-dom": "^16.8.0",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.2.2",
"react-scripts-ts": "^3.1.0",
"react-toastify": "^4.1.0",
"recompose": "^0.28.1",
"styled-components": "^2.4.0",
"ts-jest": "^22.4.2",
"ts-node": "^5.0.1",
"typescript": "^2.9.0",
"xcel-asset-service": "*",
"xcel-react-animations": "*",
"xcel-react-core": "*",
"xcel-redux-orm": "*",
"xcel-util": "*"
},
"jest": {
"coverageReporters": [
"lcov",
"cobertura",
"text-summary"
],
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"testMatch": [
"<rootDir>/src/**/*.test.(ts|tsx)"
],
"collectCoverageFrom": [
"<rootDir>/src/**/*.(ts|tsx)",
"!<rootDir>/src/**/*.test.(ts|tsx)"
],
"transform": {
"\\.(ts|tsx)$": "ts-jest"
}
},
"publishConfig": {
"registry": "Blah blah removed"
}
}

How to polyfill IE11 in NextJS project?

I'm trying to polyfill the project for IE11 but no matter what I do browser logs these errors:
Dev mode:
SCRIPT1002: Syntax error in _app.js (49004,30)
49004 const Component = props => (react__WEBPACK_IMPORTED_MODULE_0__["createElement"](_injectIntl__WEBPACK_IMPORTED_MODULE_2__["Context"].Consumer, null, intl => {
Object(_utils__WEBPACK_IMPORTED_MODULE_1__["invariantIntlContext"])(intl);
const formattedValue = intl[name](props.value, props);
if (typeof props.children === 'function') {
return props.children(formattedValue);
}
const Text = intl.textComponent || react__WEBPACK_IMPORTED_MODULE_0__["Fragment"];
return react__WEBPACK_IMPORTED_MODULE_0__["createElement"](Text, null, formattedValue);
}));
Build mode:
SCRIPT1006: Expected ')'
...function G(e,t,n={})
NextJS doc says that framework polyfills code from the box but it'not enought. What is missing then?
Project files:
.babelrc
{
"env": {
"development": {
"plugins": [
[
"react-intl",
{
"messagesDir": "./lang/.messages/"
}
]
],
"presets": [
[
"next/babel",
{
"#babel/preset-env": {
"useBuiltIns": "usage",
"corejs": { "version": 3, "proposals": true }
}
}
]
]
},
"production": {
"plugins": [
[
"react-intl",
{
"messagesDir": "./lang/.messages/"
}
]
],
"presets": [
[
"next/babel",
{
"#babel/preset-env": {
"useBuiltIns": "usage",
"corejs": { "version": 3, "proposals": true }
}
}
]
]
},
"test": {
"presets": [
[
"next/babel",
{
"preset-env": {
"modules": "commonjs"
}
}
]
]
}
},
"presets": ["#zeit/next-typescript/babel"],
"plugins": [["styled-components", { "ssr": true }]]
}
package.json
{
"version": "0.1.0",
"scripts": {
"build": "yarn generate && yarn type-check && cross-env NODE_CONFIG_DIR=./root/etc next build ./src && yarn l10n",
"build-storybook": "build-storybook -s ./src/static-storybook",
"dev": "yarn generate && cross-env NODE_CONFIG_DIR=./root/etc nodemon",
"generate": "yarn generate:clean && graphql-codegen --config codegen.server.yml && graphql-codegen --config codegen.yml && apollo client:codegen --globalTypesFile=./src/__generated__/globalTypes.ts --addTypename --target=typescript --localSchemaFile=./src/schema/__generated__/schema.graphql --includes=\"./src/{components/**/*.{ts,tsx},schema/**/*.ts,queries/**/*.ts}\" --passthroughCustomScalars --tagName gql",
"generate:clean": "find . | grep -E \"__generated__\" | xargs rm -rf",
"l10n": "node ./root/bin/l10n/prepare-lang.js && node ./root/bin/l10n/push.js && node ./root/bin/l10n/pull.js",
"l10n:clean": "rm -rf lang/ node_modules/.cache/",
"lint:styles": "stylelint \"./src/**/*.ts\" \"./src/**/*.tsx\"",
"lint:ts": "eslint --ext .tsx,.ts src/",
"start": "cross-env NODE_ENV=stage NODE_CONFIG_DIR=./root/etc node .next/server/index.js",
"storybook": "start-storybook -p 6006 -s ./src/static-storybook,./src/static",
"svgr": "npx #svgr/cli -d ./src/svgr ./src/static/svgs --ext tsx",
"test": "jest",
"test:watch": "jest --watch",
"type-check": "tsc && tsc --project tsconfig.server.json"
},
"browserslist": [
"last 2 versions",
"not dead",
"IE 11"
],
"dependencies": {
"#babel/polyfill": "^7.6.0",
"#formatjs/intl-relativetimeformat": "^2.8.2",
"#formatjs/intl-utils": "^0.6.1",
"#tippy.js/react": "^2.2.0",
"#types/classnames": "^2.2.8",
"#types/react-input-mask": "^2.0.3",
"#types/react-select": "^3.0.4",
"#types/react-sidebar": "^3.0.0",
"#zeit/next-css": "^1.0.2-canary.2",
"#zeit/next-typescript": "^1.1.1",
"accepts": "^1.3.5",
"apollo-boost": "^0.4.3",
"apollo-datasource-rest": "^0.5.0",
"apollo-link-context": "^1.0.18",
"apollo-link-error": "^1.1.11",
"apollo-server-express": "^2.6.7",
"babel-polyfill": "^6.26.0",
"classnames": "^2.2.6",
"config": "^3.1.0",
"cookie": "^0.3.1",
"cookie-parser": "^1.4.4",
"css-vars-ponyfill": "^2.0.2",
"dataloader": "^1.4.0",
"date-fns": "^1.30.1",
"express": "^4.16.4",
"final-form": "^4.18.5",
"final-form-arrays": "^3.0.1",
"format-string-by-pattern": "^1.1.1",
"glob": "^7.1.4",
"graphql": "^14.5.6",
"graphql-tools": "^4.0.4",
"http-proxy-middleware": "^0.19.1",
"intl": "^1.2.5",
"intl-pluralrules": "^1.0.3",
"is-valid-date": "^0.0.1",
"isomorphic-unfetch": "^3.0.0",
"lodash.debounce": "^4.0.8",
"lodash.sortby": "^4.7.0",
"merge-graphql-schemas": "^1.5.8",
"next": "^8.1.0",
"next-compose-plugins": "^2.2.0",
"postcss-custom-properties": "^8.0.10",
"postcss-nested": "^4.1.2",
"qs": "^6.7.0",
"react": "^16.8.6",
"react-apollo": "^2.5.8",
"react-components": "",
"react-dom": "^16.8.6",
"react-final-form": "^6.3.0",
"react-final-form-arrays": "^3.1.1",
"react-final-form-listeners": "^1.0.2",
"react-input-mask": "^2.0.4",
"react-intl": "^3.1.13",
"react-markdown": "^4.2.2",
"react-modal": "^3.9.1",
"react-range": "^1.0.6",
"react-redux": "^7.1.1",
"react-select": "^3.0.4",
"react-sidebar": "^3.0.2",
"react-tooltip-lite": "^1.10.0",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0",
"request": "^2.88.0",
"request-promise": "^4.2.4",
"smoothscroll-polyfill": "^0.4.4",
"styled-components": "^4.2.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"#babel/preset-env": "^7.6.3",
"#graphql-codegen/cli": "^1.2.0",
"#graphql-codegen/fragment-matcher": "^1.2.0",
"#graphql-codegen/schema-ast": "^1.2.0",
"#graphql-codegen/typescript": "^1.2.0",
"#graphql-codegen/typescript-operations": "^1.2.0",
"#graphql-codegen/typescript-react-apollo": "^1.2.0",
"#graphql-codegen/typescript-resolvers": "^1.2.0",
"#storybook/addon-actions": "^5.1.9",
"#storybook/addon-backgrounds": "^5.1.9",
"#storybook/addon-info": "^5.1.9",
"#storybook/addon-knobs": "^5.1.9",
"#storybook/addon-links": "^5.1.9",
"#storybook/addon-storysource": "^5.1.9",
"#storybook/addon-viewport": "^5.1.9",
"#storybook/addons": "^5.1.9",
"#storybook/react": "^5.1.9",
"#svgr/cli": "^4.2.0",
"#types/accepts": "^1.3.5",
"#types/config": "^0.0.34",
"#types/cookie": "^0.3.2",
"#types/cookie-parser": "^1.4.1",
"#types/enzyme": "^3.9.1",
"#types/express": "^4.16.1",
"#types/glob": "^7.1.1",
"#types/graphql": "^14.0.7",
"#types/http-proxy-middleware": "^0.19.2",
"#types/intl": "^1.2.0",
"#types/jest": "^24.0.11",
"#types/lodash.debounce": "^4.0.6",
"#types/next": "^8.0.6",
"#types/qs": "^6.5.2",
"#types/react": "^16.8.23",
"#types/react-dom": "^16.8.4",
"#types/react-modal": "^3.8.2",
"#types/request-promise": "^4.1.44",
"#types/smoothscroll-polyfill": "^0.3.1",
"#types/storybook__addon-actions": "^3.4.3",
"#types/storybook__addon-info": "^4.1.2",
"#types/storybook__addon-knobs": "^5.0.1",
"#types/storybook__addon-links": "^3.3.5",
"#types/storybook__react": "^4.0.2",
"#types/styled-components": "^4.1.12",
"#types/uuid": "^3.4.4",
"#typescript-eslint/eslint-plugin": "^1.5.0",
"#typescript-eslint/parser": "^1.5.0",
"apollo": "^2.18.3",
"awesome-typescript-loader": "^5.2.1",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.7.1",
"babel-loader": "^8.0.5",
"babel-plugin-react-intl": "^4.1.14",
"core-js": "^3.2.1",
"cross-env": "^6.0.3",
"csvtojson": "^2.0.8",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"eslint": "5.3.0",
"eslint-config-airbnb": "17.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.11.0",
"eslint-plugin-react-hooks": "^1.6.0",
"file-loader": "^4.2.0",
"graphql-tag": "^2.10.1",
"husky": "^1.3.1",
"jest": "^24.5.0",
"jest-dom": "^3.1.3",
"l10n-node-client": "",
"lint-staged": "^8.1.5",
"nodemon": "^1.18.10",
"prettier": "^1.16.4",
"react-addons-test-utils": "^15.6.2",
"react-docgen-typescript-loader": "^3.1.0",
"react-testing-library": "^6.0.3",
"storybook-addon-intl": "^2.4.1",
"storybook-addon-styled-component-theme": "^1.2.1",
"stylelint": "^10.0.1",
"stylelint-config-recommended": "^2.2.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.6.0",
"ts-node": "^8.0.3",
"typescript": "^3.6.3"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && tsc"
}
}
}
polyfills.js
import 'babel-polyfill';
import cssVarsPonyfill from 'css-vars-ponyfill';
if (!('remove' in Element.prototype)) {
Element.prototype.remove = function() {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
};
}
cssVarsPonyfill({
onlyLegacy: true,
exclude: '[data-styled]',
});
next.config.js
const { PHASE_PRODUCTION_BUILD } = require('next-server/constants');
const withPlugins = require('next-compose-plugins');
const typescript = require('#zeit/next-typescript');
const css = require('#zeit/next-css');
const getLocalIdent = require('css-loader/lib/getLocalIdent');
const config = require('config');
module.exports = withPlugins([typescript, css], {
distDir: '../.next',
assetPrefix: config.get('development') ? '' : '/avia',
cssModules: true,
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[local]___[hash:base64:5]',
camelCase: true,
getLocalIdent: (loaderContext, localIdentName, localName, options) => {
// FIXME Костыль для импорта css-файлов из react-components без преобразования имен классов
if (loaderContext.resourceQuery === '?raw-class-name') {
return localName;
}
return getLocalIdent(loaderContext, localIdentName, localName, options);
},
},
[PHASE_PRODUCTION_BUILD]: {
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[local]___[hash:base64:5]',
camelCase: true,
getLocalIdent: (loaderContext, localIdentName, localName, options) => {
// FIXME Костыль для импорта css-файлов из react-components без преобразования имен классов
if (loaderContext.resourceQuery === '?raw-class-name') {
return localName;
}
return getLocalIdent(loaderContext, localIdentName, localName, options);
},
},
},
webpack(config) {
const originalEntry = config.entry;
config.entry = async () => {
const entries = await originalEntry();
if (
entries['main.js'] &&
!entries['main.js'].includes('./polyfills.js')
) {
entries['main.js'].unshift('./polyfills.js');
}
return entries;
};
config.module.rules.push({
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
});
config.module.rules.push({
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
}
]
});
config.stats = {
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/250
warningsFilter: (warning) => /Conflicting order between/m.test(warning),
};
return config;
},
});

Resources