Comments (2)
Just to add, for those who faced the same issue and are looking for a solution.
If you can use esm
instead of cjs
- just use it. It's better, believe me 😄
If you cannot avoid cjs
- as a workaround for this issue use cjs
everywhere (assuming you cannot tune your bundler to workaround it in other way).
I know it's painful, but this is what currently available.
const { toast } = require('react-toastify');
Depending on your project configuration, you may also need to:
- Disable linting error:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { toast } = require('react-toastify');
- Provide a new linting rule in your
eslintrc.js
to enforce importingreact-toastify
usingcjs
style:
{
'no-restricted-imports': [
'error',
{
name: 'react-toastify',
message: 'Please use CommonJS importing syntax for this lib - require',
},
]
}
- Also, you may need to change other settings of your
tsconfig
,package.json
or bundler configuration
from react-toastify.
Also, there is tricky workaround for webpack
.
The idea is to remove exports
section before compilation using a custom plugin:
const { validate } = require('schema-utils');
const path = require('path');
const fs = require('fs');
const schema = {
type: 'object',
properties: {
path: {
type: 'string',
},
},
};
class DualCjsEsmWorkaroundPlugin {
static name = 'DualCjsEsmWorkaroundPlugin';
constructor(options) {
validate(schema, options);
const defaultOptions = {
path: null,
};
this.options = { ...defaultOptions, ...options };
}
apply(compiler) {
if (!this.options.path) {
return;
}
compiler.hooks.initialize.tap(DualCjsEsmWorkaroundPlugin.name, () =>
this._removeExportsWhenPossible(),
);
}
_removeExportsWhenPossible() {
try {
const packageJsonPath = path.join(this.options.path, 'package.json');
const packageJson = require(packageJsonPath);
const canRemoveExports = packageJson.module && packageJson.main && packageJson.exports;
if (!canRemoveExports) {
return;
}
const packageJsonWithoutExports = JSON.stringify(
{ ...packageJson, exports: undefined },
null,
2,
);
fs.writeFileSync(packageJsonPath, packageJsonWithoutExports);
} catch (error) {
console.error(
`[${DualCjsEsmWorkaroundPlugin.name}]: error while trying to remove externals: ${error}`,
);
}
}
}
// webpack.config.js
module.exports = {
// other configs
plugins: [
new DualCjsEsmWorkaroundPlugin({ path: path.join(__dirname, 'node_modules/react-toastify') }),
// other plugins
],
};
from react-toastify.
Related Issues (20)
- Does ToastContainer listen to theme props and auto rerender? HOT 1
- Toast Conflicting or loading infinite in promise at concurrent requests HOT 1
- Timer end function()
- The easiest way to use react-toastify with Tailwind CSS HOT 2
- Is there a way to style the toasts on mobile so that they look like on desktop?
- Is it possible to override $rt-mobile: 'only screen and (max-width : 480px)' !default;? If so, how?
- Add a data attribute to ToastContainer
- dismiss() default behaviour when no arguments are passed
- feature: closeOnClickAway
- bug: Promise success clears when 2nd promise has undefined success HOT 2
- A minor issue with the jsdoc for closeOnClick
- The timer stops HOT 1
- `autoClose` is not respected when manually setting `progress` to 1 HOT 2
- Feature: prop for passing function when close button is clicked HOT 1
- Issue in container HOT 2
- Screen readers do not reliably announce toasts with role="alert"
- Update Readme for addons > useNotificationCenter HOT 1
- [Bug] Transitions don't work without ReactToastify.css
- toastClassName and className (on the toast) are not respected
- closeOnClick still requires disabling HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-toastify.