craco-alias
A craco plugin for automatic aliases generation for Webpack and Jest.
:warning: The plugin does not fully support a module alises
List of Contents
Installation
-
Install craco
-
Install craco-alias
:
npm i -D craco-alias
-
Edit craco.config.js
:
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
}
}
]
};
-
Go to Examples section
Options
-
source
:
One of "options"
, "jsconfig"
, "tsconfig"
Defaults to "options"
-
aliases
:
An object with aliases names and paths
Defaults to {}
-
tsConfigPath
:
A path to tsconfig file
Only required when source
is set to "tsconfig"
Examples
Specify aliases manually (source: "options")
Note: you don't need to add /*
part for directories in this case
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: "options",
aliases: {
"@file": "src/file.js",
"@dir": "src/some/dir"
}
}
}
]
};
Use aliases from jsconfig.json (source: "jsconfig")
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: "jsconfig"
}
}
]
};
Note: your jsconfig should always have baseUrl and paths properties
{
compilerOptions: {
baseUrl: "src",
paths: {
"@file": ["file.js"],
"@dir/*": ["dir/*", "dir"]
}
}
}
Use aliases from tsconfig.json (source: "tsconfig")
-
Go to project's root directory.
-
Create tsconfig.extend.json
.
-
Edit it as follows:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@file-alias": ["./your/file.tsx"],
"@folder-alias/*": ["./very/long/path/*", "./very/long/path/"]
}
}
}
-
Go to tsconfig.json
.
-
Extend tsconfig.json
from tsconfig.extend.json
:
{
+ "extends": "./tsconfig.extend.json",
"compilerOptions": {
...
},
...
}
-
Edit craco.config.js
:
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: "tsconfig",
tsConfigPath: "./tsconfig.extend.json"
}
}
]
};