Skip to content

Commit 8554a3d

Browse files
oterralsapegin
authored andcommitted
Fix: Support webpack configs from react-scripts > 2.1.1 (styleguidist#1241)
Closes styleguidist#1243
1 parent 4818ffe commit 8554a3d

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/scripts/utils/__tests__/findUserWebpackConfig.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ import findUserWebpackConfig from '../findUserWebpackConfig';
33
const cwd = process.cwd();
44
afterEach(() => process.chdir(cwd));
55

6-
it('should return path to Create React App Webpack config', () => {
6+
it('should return path to Create React App Webpack old config (react-scripts <= 2.1.1)', () => {
77
const result = findUserWebpackConfig(a => a);
8-
expect(result).toMatch(/^react-scripts\//);
8+
expect(result).toMatchInlineSnapshot(`"react-scripts/config/webpack.config.dev"`);
9+
});
10+
11+
it('should return path to Create React App Webpack config (react-scripts > 2.1.1)', () => {
12+
const result = findUserWebpackConfig(a => {
13+
if (/webpack\.config\.dev/.test(a)) {
14+
// Simulate an error. For example, if the file doesn't exist.
15+
throw new Error();
16+
}
17+
return a;
18+
});
19+
expect(result).toMatchInlineSnapshot(`"react-scripts/config/webpack.config"`);
920
});
1021

1122
it('should return an absolute path to user Webpack config located in project root folder', () => {

src/scripts/utils/findUserWebpackConfig.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const fs = require('fs');
22
const path = require('path');
33

4-
const CREATE_REACT_APP_WEBPACK_CONFIG = 'react-scripts/config/webpack.config.dev';
4+
// react-scripts <= 2.1.1
5+
const CREATE_REACT_APP_WEBPACK_CONFIG_OLD = 'react-scripts/config/webpack.config.dev';
6+
// react-scripts > 2.1.1
7+
const CREATE_REACT_APP_WEBPACK_CONFIG = 'react-scripts/config/webpack.config';
58
const USER_WEBPACK_CONFIG_NAMES = ['webpack.config.js', 'webpackfile.js'];
69

710
const absolutize = filePath => path.resolve(process.cwd(), filePath);
@@ -17,14 +20,19 @@ const absolutize = filePath => path.resolve(process.cwd(), filePath);
1720
module.exports = function findUserWebpackConfig(resolve) {
1821
resolve = resolve || require.resolve;
1922
try {
20-
// Create React App
21-
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG);
23+
// Create React App <= 2.1.1
24+
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG_OLD);
2225
} catch (err) {
23-
// Check in the root folder
24-
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
25-
const absoluteConfigFile = absolutize(configFile);
26-
if (fs.existsSync(absoluteConfigFile)) {
27-
return absoluteConfigFile;
26+
try {
27+
// Create React App > 2.1.1
28+
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG);
29+
} catch (err) {
30+
// Check in the root folder
31+
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
32+
const absoluteConfigFile = absolutize(configFile);
33+
if (fs.existsSync(absoluteConfigFile)) {
34+
return absoluteConfigFile;
35+
}
2836
}
2937
}
3038
}

0 commit comments

Comments
 (0)