v3.0.0
New features
This new version of react-magnetic-di
comes with tons of goodies!
- Automatic injection. Until now,
di()
needed to be called in every function to enable injection and, moreover, it needed to contain all the values allowed for injection. It was a safe strategy but it was suboptimal. This version ofmagnetic-di
enables injection withoutdi()
- Support for injecting anywhere. While the pkg name is still referencing React, this version exposes
runWithDi
which enables replacement in any context - Injectable level targeting. With the new option
injectable(A,B, { target: C })
you can replace a dependency only in a particular function scope. - New ESLint rule
no-restricted-injectable
. Allows to warn/error about dangerous injectable patterns (eg on very commonly used dependencies)
Breaking changes
- No more
babel-plugin-macro
support, asdi()
is now implicit - Removed
exhaustive-inject
ESLint rule as no longer needed - Change in Babel configuration
Migration from v2
The most substantial change is the different Babel plugin configuration. To facilitate the migration, you can maintain the previous behaviour by excluding all files from auto injection:
// In your .babelrc / babel.config.js
// ... other stuff like presets
plugins: [
// ...
['react-magnetic-di/babel-plugin', { exclude: [/.*/], enabledEnvs: ['development', 'test'] }],
],
Then, you can start excluding less from auto injection, only leaving test and mock files (we recommend it, to reduce chances of loops due to injectables injecting themselves). In case you hit an issue with auto di, you can opt out in a specific function by adding the magic comment // di-ignore