Skip to content

Commit da64422

Browse files
committed
Fix: Do not expect react-script to be in devDependencies (fix #526)
1 parent 967eb0f commit da64422

File tree

6 files changed

+33
-53
lines changed

6 files changed

+33
-53
lines changed

scripts/__tests__/config.spec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,15 @@ it('should return webpackConfig option as is', () => {
160160
});
161161

162162
it('should return webpackConfig with user webpack config', () => {
163-
process.chdir('test/apps/cra');
163+
process.chdir('test/apps/basic');
164164
const result = getConfig();
165-
expect(result.webpackConfig).toEqual({ cra: true });
165+
expect(result.webpackConfig).toEqual(
166+
expect.objectContaining({
167+
module: {
168+
loaders: expect.any(Array),
169+
},
170+
})
171+
);
166172
});
167173

168174
it('should allow no webpack config', () => {

scripts/__tests__/index.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ describe('makeWebpackConfig', () => {
102102
});
103103

104104
it('should merge Create React App Webpack config', () => {
105-
process.chdir('test/apps/cra');
105+
process.chdir('test/apps/basic');
106106
const api = styleguidist();
107107
const result = api.makeWebpackConfig();
108108

109109
expect(result).toBeTruthy();
110-
expect(result.cra).toBeTruthy();
110+
expect(result.module).toBeTruthy();
111111
});
112112

113113
it('should add json-loader', () => {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import findUserWebpackConfig from '../findUserWebpackConfig';
22

33
const cwd = process.cwd();
4-
afterEach(() => {
5-
process.chdir(cwd);
6-
});
4+
afterEach(() => process.chdir(cwd));
75

86
it('should return path to Create React App Webpack config', () => {
9-
process.chdir('test/apps/cra');
10-
const result = findUserWebpackConfig();
7+
const result = findUserWebpackConfig(a => a);
118
expect(result).toMatch(/^react-scripts\//);
129
});
1310

1411
it('should return an absolute path to user Webpack config located in project root folder', () => {
1512
process.chdir('test/apps/basic');
13+
1614
const result = findUserWebpackConfig();
1715
expect(result).toMatch(/^\//);
1816
expect(result).toMatch(/webpack.config.js$/);
1917
});
18+
19+
it('should return false if there is no webpack config', () => {
20+
process.chdir('test/apps/no-webpack');
21+
22+
const result = findUserWebpackConfig();
23+
expect(result).toBeFalsy();
24+
});

scripts/utils/__tests__/isCreateReactApp.spec.js

-18
This file was deleted.

scripts/utils/findUserWebpackConfig.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const isCreateReactApp = require('./isCreateReactApp');
65

76
const CREATE_REACT_APP_WEBPACK_CONFIG = 'react-scripts/config/webpack.config.dev';
87
const USER_WEBPACK_CONFIG_NAMES = ['webpack.config.js', 'webpackfile.js'];
@@ -14,19 +13,21 @@ const absolutize = filePath => path.resolve(process.cwd(), filePath);
1413
* Fixed location for Create React App or webpack.config.js in the root directory.
1514
* Returns false if config not found.
1615
*
16+
* @param {Function} resolve
1717
* @return {string|boolean}
1818
*/
19-
module.exports = function findUserWebpackConfig() {
20-
// Create React App
21-
if (isCreateReactApp()) {
22-
return CREATE_REACT_APP_WEBPACK_CONFIG;
23-
}
24-
25-
// Check in the root folder
26-
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
27-
const absoluteConfigFile = absolutize(configFile);
28-
if (fs.existsSync(absoluteConfigFile)) {
29-
return absoluteConfigFile;
19+
module.exports = function findUserWebpackConfig(resolve) {
20+
resolve = resolve || require.resolve;
21+
try {
22+
// Create React App
23+
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG);
24+
} catch (err) {
25+
// Check in the root folder
26+
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
27+
const absoluteConfigFile = absolutize(configFile);
28+
if (fs.existsSync(absoluteConfigFile)) {
29+
return absoluteConfigFile;
30+
}
3031
}
3132
}
3233

scripts/utils/isCreateReactApp.js

-14
This file was deleted.

0 commit comments

Comments
 (0)