craco-alias
A craco plugin for automatic aliases generation for Webpack and Jest.
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")
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: "tsconfig",
tsConfigPath: "tsconfig.paths.json"
}
}
]
};
{
compilerOptions: {
baseUrl: "src",
paths: {
"@file": ["file.js"],
"@dir/*": ["dir/*", "dir"]
}
}
}