Skip to content

Commit 7aab18d

Browse files
author
Félix Bélanger-Robillard
committed
Merge branch 'master' into support-cra5
2 parents 8db54e0 + 8ab5cc9 commit 7aab18d

File tree

8 files changed

+35359
-9041
lines changed

8 files changed

+35359
-9041
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
**C**reate **R**eact **A**pp **C**onfiguration **O**verride is an easy and comprehensible configuration layer for create-react-app.
1010

11-
Get all the benefits of create-react-app **and** customization without using 'eject' by adding a single `craco.config.js` file at the root of your application and customize your eslint, babel, postcss configurations and many more.
11+
Get all the benefits of create-react-app **and** customization without using 'eject' by adding a single configuration (e.g. `craco.config.js`) file at the root of your application and customize your eslint, babel, postcss configurations and many more.
1212

13-
All you have to do is create your app using [create-react-app](https://github.com/facebook/create-react-app/) and customize the configuration with a `craco.config.js` file.
13+
All you have to do is create your app using [create-react-app](https://github.com/facebook/create-react-app/) and customize the configuration file.
1414

1515
## Support
1616

@@ -28,7 +28,7 @@ All you have to do is create your app using [create-react-app](https://github.co
2828
- [Configuration File](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#configuration-file)
2929
- [Configuration Helpers](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#configuration-helpers)
3030
- [Exporting your Configuration](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#exporting-your-configuration)
31-
- [Setting a Custom Location for `craco.config.js`](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#setting-a-custom-location-for-cracoconfigjs)
31+
- [Setting a Custom Location for the configuration file](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#setting-a-custom-location-for-cracoconfigjs)
3232
- [CRA Toolchain for Beginners](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#cra-toolchain-for-beginners)
3333
- [Notes on CRA Configurations and Problem Solving](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#notes-on-cra-configurations-and-problem-solving)
3434
- [Ejecting CRA to Learn](https://github.com/sharegate/craco/blob/master/packages/craco/README.md#ejecting-cra-to-learn)

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"packages": ["packages/*"],
3-
"version": "6.3.0"
3+
"version": "6.4.3"
44
}

packages/craco/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
**C**reate **R**eact **A**pp **C**onfiguration **O**verride is an easy and comprehensible configuration layer for create-react-app.
44

5-
Get all the benefits of create-react-app **and** customization without using 'eject' by adding a single `craco.config.js` file at the root of your application and customize your eslint, babel, postcss configurations and many more.
5+
Get all the benefits of create-react-app **and** customization without using 'eject' by adding a single configuration (e.g. `craco.config.js`) file at the root of your application and customize your eslint, babel, postcss configurations and many more.
66

7-
All you have to do is create your app using [create-react-app](https://github.com/facebook/create-react-app/) and customize the configuration with a `craco.config.js` file.
7+
All you have to do is create your app using [create-react-app](https://github.com/facebook/create-react-app/) and customize the configuration file.
88

99
## Support
1010

@@ -22,7 +22,7 @@ All you have to do is create your app using [create-react-app](https://github.co
2222
- [Configuration File](#configuration-file)
2323
- [Configuration Helpers](#configuration-helpers)
2424
- [Exporting your Configuration](#exporting-your-configuration)
25-
- [Setting a Custom Location for `craco.config.js`](#setting-a-custom-location-for-cracoconfigjs)
25+
- [Setting a Custom Location for the configuration file](#setting-a-custom-location-for-cracoconfigjs)
2626
- [CRA toolchain for Beginners](#cra-toolchain-for-beginners)
2727
- [Notes on CRA Configurations and Problem Solving](#notes-on-cra-configurations-and-problem-solving)
2828
- [Ejecting CRA to Learn](#ejecting-cra-to-learn)
@@ -99,13 +99,16 @@ $ npm run build
9999

100100
## Configuration
101101

102-
CRACO is configured with a `craco.config.js`, `.cracorc.js` or `.cracorc` file. This file is divided into sections representing the major parts of what makes up the default create react app.
102+
CRACO is configured with a `craco.config.ts`, `craco.config.js`, `.cracorc.ts`, `.cracorc.js` or `.cracorc` file, or [a file specified in `package.json`](#setting-a-custom-location-for-cracoconfigjs). This file is divided into sections representing the major parts of what makes up the default create react app.
103103

104104
If there are multiple configuration files in the same directory, CRACO will only use one. The priority order is:
105105

106+
1. `package.json`
107+
1. `craco.config.ts`
106108
1. `craco.config.js`
107-
2. `.cracorc.js`
108-
3. `.cracorc`
109+
1. `.cracorc.ts`
110+
1. `.cracorc.js`
111+
1. `.cracorc`
109112

110113
### Configuration File
111114

packages/craco/lib/config.js

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
const { configFilePath } = require("./paths");
2-
const { isFunction, isArray, deepMergeWithArray } = require("./utils");
1+
const { cosmiconfigSync } = require("cosmiconfig");
2+
const { default: tsLoader } = require("cosmiconfig-typescript-loader");
3+
4+
const path = require("path");
5+
6+
const { getArgs } = require("./args");
37
const { log } = require("./logger");
8+
const { projectRoot } = require("./paths");
9+
const { deepMergeWithArray, isArray, isFunction, isString } = require("./utils");
410
const { applyCracoConfigPlugins } = require("./features/plugins");
511
const { POSTCSS_MODES } = require("./features/webpack/style/postcss");
612
const { ESLINT_MODES } = require("./features/webpack/eslint");
@@ -23,6 +29,22 @@ const DEFAULT_CONFIG = {
2329
}
2430
};
2531

32+
const moduleName = "craco";
33+
const explorer = cosmiconfigSync(moduleName, {
34+
searchPlaces: [
35+
"package.json",
36+
`${moduleName}.config.ts`,
37+
`${moduleName}.config.js`,
38+
`${moduleName}.config.cjs`,
39+
`.${moduleName}rc.ts`,
40+
`.${moduleName}rc.js`,
41+
`.${moduleName}rc`
42+
],
43+
loaders: {
44+
".ts": tsLoader()
45+
}
46+
});
47+
2648
function ensureConfigSanity(cracoConfig) {
2749
if (isArray(cracoConfig.plugins)) {
2850
cracoConfig.plugins.forEach((x, index) => {
@@ -40,15 +62,39 @@ function processCracoConfig(cracoConfig, context) {
4062
return applyCracoConfigPlugins(resultingCracoConfig, context);
4163
}
4264

43-
function getConfigAsObject(context) {
44-
if (configFilePath == undefined || configFilePath.length == 0) {
45-
throw new Error("craco: Config file not found. check if file exists at root (craco.config.js, .cracorc.js, .cracorc)");
65+
function getConfigPath() {
66+
const args = getArgs();
67+
68+
if (args.config.isProvided) {
69+
return path.resolve(projectRoot, args.config.value);
70+
} else {
71+
const packageJsonPath = path.join(projectRoot, "package.json");
72+
73+
const package = require(packageJsonPath);
74+
75+
if (package.cracoConfig && isString(package.cracoConfig)) {
76+
// take it as the path to the config file if it's path-like, otherwise assume it contains the config content below
77+
return path.resolve(projectRoot, package.cracoConfig);
78+
} else {
79+
const result = explorer.search(projectRoot);
80+
81+
if (result === null) {
82+
throw new Error(
83+
"craco: Config file not found. check if file exists at root (craco.config.ts, craco.config.js, .cracorc.js, .cracorc.json, .cracorc.yaml, .cracorc)"
84+
);
85+
}
86+
87+
return result.filepath;
88+
}
4689
}
47-
48-
log("Found craco config file at: ", configFilePath);
90+
}
91+
92+
function getConfigAsObject(context) {
93+
const configFilePath = getConfigPath();
94+
log("Config file path resolved to: ", configFilePath);
95+
const result = explorer.load(configFilePath);
4996

50-
const config = require(configFilePath);
51-
const configAsObject = isFunction(config) ? config(context) : config;
97+
const configAsObject = isFunction(result.config) ? result.config(context) : result.config;
5298

5399
if (!configAsObject) {
54100
throw new Error("craco: Config function didn't return a config object.");

packages/craco/lib/features/webpack/style/postcss.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ function extendsPostcss(match, { plugins, env }) {
5858
}
5959

6060
if (plugins) {
61-
postcssPlugins = typeof plugins === "function"
62-
? plugins(postcssPlugins)
63-
: postcssPlugins.concat(plugins);
61+
postcssPlugins = typeof plugins === "function" ? plugins(postcssPlugins) : postcssPlugins.concat(plugins);
6462

6563
log("Added PostCSS plugins.");
6664
}

packages/craco/lib/paths.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
1-
const path = require("path");
21
const fs = require("fs");
3-
const { getArgs } = require("./args");
42
const { log } = require("./logger");
5-
const { isString } = require("./utils");
63

74
const projectRoot = fs.realpathSync(process.cwd());
8-
const packageJsonPath = path.join(projectRoot, "package.json");
95

106
log("Project root path resolved to: ", projectRoot);
117

12-
let configFilePath = "";
13-
14-
const configFilenames = ["craco.config.js", ".cracorc.js", ".cracorc"];
15-
16-
const args = getArgs();
17-
18-
if (args.config.isProvided) {
19-
configFilePath = path.resolve(projectRoot, args.config.value);
20-
} else {
21-
const package = require(packageJsonPath);
22-
23-
if (package.cracoConfig) {
24-
if (!isString(package.cracoConfig)) {
25-
throw new Error("craco: 'cracoConfig' value must be a string.");
26-
}
27-
28-
configFilePath = path.resolve(projectRoot, package.cracoConfig);
29-
} else {
30-
for (const filename of configFilenames) {
31-
const filePath = path.join(projectRoot, filename);
32-
33-
if (fs.existsSync(filePath)) {
34-
configFilePath = filePath;
35-
break;
36-
}
37-
}
38-
}
39-
}
40-
41-
log("Config file path resolved to: ", configFilePath);
42-
438
module.exports = {
44-
projectRoot,
45-
packageJsonPath,
46-
configFilePath
9+
projectRoot
4710
};

0 commit comments

Comments
 (0)