Gathering detailed insights and metrics for react-app-rewire-alias
Gathering detailed insights and metrics for react-app-rewire-alias
Gathering detailed insights and metrics for react-app-rewire-alias
Gathering detailed insights and metrics for react-app-rewire-alias
npm install react-app-rewire-alias
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
9 Stars
105 Commits
1 Watching
1 Branches
10 Contributors
Updated on 26 May 2024
Minified
Minified + Gzipped
JavaScript (50.13%)
TypeScript (22.78%)
HTML (17.59%)
CSS (9.5%)
Cumulative downloads
Total Downloads
Last day
-21.3%
2,741
Compared to previous day
Last week
-8.7%
15,037
Compared to previous week
Last month
3.5%
70,332
Compared to previous month
Last year
-24.9%
1,001,984
Compared to previous year
1
For easy migration to major release with renaming follow this migration guide
This is more than simple alias. This is also a multi-project src
directory. Currently, create-react-app
(CRA) does not support more than one
src
directory in the project. Monorepo, multi-repo and library projects with
examples require more than one directory like src
.
This is merely an alias and multi-source solution for CRA and this is not a replacement for multi-package management tools like Lerna.
This requires to modify the CRA webpack configuration in runtime (without ejecting) and works with one of:
src
./directory
at root outside of src
with Babel and CRA featuresprovided fully functional aliases and allows the use of Babel, JSX, etc.
outside of src
(outside of project root
may be enbled with special way
see the section below)
provided fully secure aliases and uses the same module scope plugin from the original create-react-app package for modules (instead of removing it), to minimize the probability of including unwanted code
1yarn add --dev react-app-rewire-alias
or
1npm install --save-dev react-app-rewire-alias
By default folders for alias may be near to src folder or in it.
Outside of project root
is enabled with special way, see below.
Usage steps:
Create a separate file jsconfig.paths.json
or tsconfig.paths.json
, like this:
1// jsconfig.paths.json or tsconfig.paths.json 2{ 3 "compilerOptions": { 4 "baseUrl": ".", 5 "paths": { 6 "example/*": ["example/src/*"], 7 "@library/*": ["library/src/*"] 8 } 9 } 10}
The paths section must not be configured directly in jsconfig.json
or tsconfig.json
, but in a separate extends file mentioned above.
Now include this file in extends section, like this:
1// jsconfig.json or tsconfig.json 2{ 3 "extends": "./jsconfig.paths.json", // or "./tsconfig.paths.json" 4 "compilerOptions": { 5 // ... 6 } 7}
1// config-overrides.js 2const {alias, configPaths} = require('react-app-rewire-alias') 3 4const aliasMap = configPaths('./tsconfig.paths.json') // or jsconfig.paths.json 5 6module.exports = alias(aliasMap) 7module.exports.jest = aliasJest(aliasMap)
aliasMap may be filled manually, for non-typescript only, see api
1// craco.config.js 2 3const {CracoAliasPlugin, configPaths} = require('react-app-rewire-alias') 4 5const aliasMap = configPaths('./tsconfig.paths.json') // or jsconfig.paths.json 6 7module.exports = { 8 plugins: [ 9 { 10 plugin: CracoAliasPlugin, 11 options: {alias: aliasMap} 12 } 13 ] 14}
aliasMap may be filled manually, for non-typescript only, see api
Integrating react-app-rewired
into your project is simple
(see its documentation):
Create config-overrides.js
mentioned above, in the project's root directory
(the same including the package.json
and src
directory).
Install react-app-rewired
1yarn add --dev react-app-rewired 2- or - 3npm install --save-dev react-app-rewired
and rewrite the package.json
like this:
1 "scripts": { 2- "start": "react-scripts start", 3+ "start": "react-app-rewired start", 4+ ... // same way 5 }
According to craco docs install craco:
1yarn add --dev craco 2- or - 3npm install --save-dev craco
and replace react-scripts
in package.json
:
1 "scripts": { 2- "start": "react-scripts start", 3+ "start": "craco start", 4+ ... // same way 5 }
The function alias()
accepts aliases declared in form:
1const aliasMap = { 2 example: 'example/src', 3 '@library': 'library/src', 4} 5 6module.exports = alias(aliasMap)
To make all things worked, aliases must be declared in jsconfig.json
or tsconfig.json
.
However, it must be declared in a separate extends file (see section Workaround for "aliased imports are not supported"
below)
The result is a function which will modify Wepack config
The function configPaths()
loads paths from file compatible with jsconfig.json
or tsconfig.json
and returns path in form acceptable for alias()
function.
The tsconfig.json
is prioritized over the jsconfig.json
in the loading sequence.
1const aliasMap = configPaths('./tsconfig.paths.json') 2 3module.exports = alias(aliasMap)
As any react-app-rewire
or customize-cra
rewire extension this can be integrated
with another:
1module.exports = function override(config) { 2 const modifiedConfig = alias(...)(config) 3 ... 4 return someElse(modifiedConfig) 5} 6module.exports.jest = function override(config) { 7 const modifiedConfig = aliasJest(...)(config) 8 ... 9 return modifiedConfig 10}
CRA overwrites
your tsconfig.json
at runtime and removes paths
from the tsconfig.json
,
which is not officially supported, with this message:
The following changes are being made to your tsconfig.json file: - compilerOptions.paths must not be set (aliased imports are not supported)
The suggested workaround
is to move the paths to a different .json
file, e.g. tsconfig.paths.json
, like this:
1/* tsconfig.paths.json */ 2{ 3 "compilerOptions": { 4 "baseUrl": ".", 5 "paths": { 6 "example/*": ["example/src/*"], 7 "@library/*": ["library/src/*"] 8 } 9 } 10}
with that file's subsequent inclusion in the tsconfig.json
using extends
:
1/* tsconfig.json */ 2{ 3 "extends": "./tsconfig.paths.json" 4}
Alias folders outside of the root of the project currently fully functional and works fine but are not recommended. It has more complicated implementation which currently named dangerous and exported from package separately. Due to complicity it has a higher probability of being incompatible with the next release of create-react-app until an update is released, since these are different systems. However same is for the base implementation but with less probability of being incompatibe with next cra release.
It provides aliases with the same feature set as the original create-react-app
.
create-react-app
does not support aliases and additional src
-like directories as
it does not supports aliases outside of the root
project directory.
Aliases outside or project root
directory may be implemented with some
limitation
of feature set. That is solved by disabling ESLint checking.
This implementation is moved to separated code set named aliasDangerous
to be not confused
with alias
. To use it just replace import like this:
1- const {alias, configPaths, CracoAliasPlugin} = require('react-app-rewire-alias') 2+ const {aliasDangerous, configPaths, CracoAliasPlugin} = require('react-app-rewire-alias/lib/aliasDangerous')
And replace alias
with aliasDangerous
:
1module.exports = function override(config) { 2 aliasDangerous({ 3 ...configPaths('tsconfig.paths.json') 4 })(config) 5 6 return config 7}
node_modules
directoryConfusions in deps versions may bring unclear errors or problems. For example, an application
is not working without any error. Or another example is error in react-router
- <Route>
component do not see <Router>
when actually code is correct and it falls with:
should not use Route or withRouter() outside a Router
This may be a result of some confusion in node_modules
folders for multi-repo projects.
Same take place in plain create-react-app
if somehow one or more additional
node_modulest
directories appear in src
.
To avoid this problem use only one main project node_modules
directory.
Default bundler configuration doesn't assume your configuration and may mix deps from
node_modules
from different projects (top project and nested project) so this may
bring mentioned above confusions with deps versions. To avoid problems:
do not install and run within nested project directly when it is nested or integrated
in another one - but only independent top level configuration Or consider to eject
or configure Webpack manually.
Some libraries use instanceof
and other type comparisions. For example , two objects
created with the same params in the same code of the same library version but installed in
different node_modules
and bundled separately - will mostly have the same data and same
behaviour but different instance type. Such libraries will be unable to recognize their own
objects and will lead to unpredictable behaviour. So use only one main project
node_modules
directory.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
65 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Morereact-app-rewired
Tweak the create-react-app webpack config(s) without using 'eject' and without creating a fork of the react-scripts
eslint-import-resolver-jsconfig
Resolver for eslint-plugin-import to import alias from jsconfig paths
rewire
Easy dependency injection for node.js unit testing
react-app-rewire-multiple-entry
Multiple Entry Support for Create-React-App