Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webpack config file name for react-scripts > 2.1.1 #1241

Merged
merged 7 commits into from Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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).toMatch(/^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).toMatch(/^react-scripts\/config\/webpack\.config/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also match webpack.config.dev. Probably should be something like /^react-scripts\/config\/webpack\.config.js$/.

});

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