Skip to content

Commit

Permalink
Fix: Support webpack configs from react-scripts > 2.1.1 (#1241)
Browse files Browse the repository at this point in the history
Closes #1243
  • Loading branch information
oterral authored and sapegin committed Jan 17, 2019
1 parent 4818ffe commit 8554a3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/scripts/utils/__tests__/findUserWebpackConfig.spec.js
Expand Up @@ -3,9 +3,20 @@ import findUserWebpackConfig from '../findUserWebpackConfig';
const cwd = process.cwd();
afterEach(() => process.chdir(cwd));

it('should return path to Create React App Webpack config', () => {
it('should return path to Create React App Webpack old config (react-scripts <= 2.1.1)', () => {
const result = findUserWebpackConfig(a => a);
expect(result).toMatch(/^react-scripts\//);
expect(result).toMatchInlineSnapshot(`"react-scripts/config/webpack.config.dev"`);
});

it('should return path to Create React App Webpack config (react-scripts > 2.1.1)', () => {
const result = findUserWebpackConfig(a => {
if (/webpack\.config\.dev/.test(a)) {
// Simulate an error. For example, if the file doesn't exist.
throw new Error();
}
return a;
});
expect(result).toMatchInlineSnapshot(`"react-scripts/config/webpack.config"`);
});

it('should return an absolute path to user Webpack config located in project root folder', () => {
Expand Down
24 changes: 16 additions & 8 deletions src/scripts/utils/findUserWebpackConfig.js
@@ -1,7 +1,10 @@
const fs = require('fs');
const path = require('path');

const CREATE_REACT_APP_WEBPACK_CONFIG = 'react-scripts/config/webpack.config.dev';
// react-scripts <= 2.1.1
const CREATE_REACT_APP_WEBPACK_CONFIG_OLD = 'react-scripts/config/webpack.config.dev';
// react-scripts > 2.1.1
const CREATE_REACT_APP_WEBPACK_CONFIG = 'react-scripts/config/webpack.config';
const USER_WEBPACK_CONFIG_NAMES = ['webpack.config.js', 'webpackfile.js'];

const absolutize = filePath => path.resolve(process.cwd(), filePath);
Expand All @@ -17,14 +20,19 @@ const absolutize = filePath => path.resolve(process.cwd(), filePath);
module.exports = function findUserWebpackConfig(resolve) {
resolve = resolve || require.resolve;
try {
// Create React App
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG);
// Create React App <= 2.1.1
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG_OLD);
} catch (err) {
// Check in the root folder
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
const absoluteConfigFile = absolutize(configFile);
if (fs.existsSync(absoluteConfigFile)) {
return absoluteConfigFile;
try {
// Create React App > 2.1.1
return resolve(CREATE_REACT_APP_WEBPACK_CONFIG);
} catch (err) {
// Check in the root folder
for (const configFile of USER_WEBPACK_CONFIG_NAMES) {
const absoluteConfigFile = absolutize(configFile);
if (fs.existsSync(absoluteConfigFile)) {
return absoluteConfigFile;
}
}
}
}
Expand Down

0 comments on commit 8554a3d

Please sign in to comment.