Skip to content

Commit

Permalink
Fix for CRA 2.1.2 timarney#343, builds on timarney#344
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffon committed Jan 2, 2019
1 parent 2728ab7 commit 77e72d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/react-app-rewired/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"dependencies": {
"cross-spawn": "^5.1.0",
"dotenv": "^4.0.0"
"dotenv": "^4.0.0",
"semver": "^5.6.0"
},
"repository": {
"type": "git",
Expand Down
23 changes: 14 additions & 9 deletions packages/react-app-rewired/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
process.env.NODE_ENV = 'production';

const paths = require('./utils/paths');
const overrides = require('../config-overrides');
const scriptPkg = require(paths.scriptVersion + "/package.json");
const semver = require('semver');

const isOldScript = !(scriptPkg && scriptPkg.version >= '2.1.2');
const { scriptVersion } = require('./utils/paths');
const overrides = require('../config-overrides');
const scriptPkg = require(`${scriptVersion}/package.json`);

const webpackConfigPath = paths.scriptVersion + isOldScript ? "/config/webpack.config.prod" : "/config/webpack.config";
// CRA 2.1.2 switched to using a webpack config factory
// https://github.com/facebook/create-react-app/pull/5722
// https://github.com/facebook/create-react-app/releases/tag/v2.1.2
const isWebpackFactory = semver.gte(scriptPkg && scriptPkg.version, '2.1.2');

// load original config
const webpackConfigPath = `${scriptVersion}/config/webpack.config${!isWebpackFactory ? '.prod' : ''}`;
const webpackConfig = require(webpackConfigPath);

// override config in memory
require.cache[require.resolve(webpackConfigPath)].exports =
isOldScript ? overrides.webpack(webpackConfig, process.env.NODE_ENV) : (env) => overrides.webpack(webpackConfig(env), env);
require.cache[require.resolve(webpackConfigPath)].exports = isWebpackFactory
? (env) => overrides.webpack(webpackConfig(env), env)
: overrides.webpack(webpackConfig, process.env.NODE_ENV);
// run original script
require(paths.scriptVersion + '/scripts/build');
require(`${scriptVersion}/scripts/build`);
27 changes: 16 additions & 11 deletions packages/react-app-rewired/scripts/start.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
process.env.NODE_ENV = process.env.NODE_ENV || "development";
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const paths = require("./utils/paths");
const overrides = require('../config-overrides');
const scriptPkg = require(paths.scriptVersion + "/package.json");
const semver = require('semver');

const isOldScript = !(scriptPkg && scriptPkg.version >= '2.1.2');
const { scriptVersion } = require('./utils/paths');
const overrides = require('../config-overrides');
const scriptPkg = require(`${scriptVersion}/package.json`);

const webpackConfigPath = paths.scriptVersion + isOldScript ? "/config/webpack.config.dev" : "/config/webpack.config";
const devServerConfigPath = paths.scriptVersion + "/config/webpackDevServer.config.js";
// CRA 2.1.2 switched to using a webpack config factory
// https://github.com/facebook/create-react-app/pull/5722
// https://github.com/facebook/create-react-app/releases/tag/v2.1.2
const isWebpackFactory = semver.gte(scriptPkg && scriptPkg.version, '2.1.2');

// load original configs
const webpackConfigPath = `${scriptVersion}/config/webpack.config${!isWebpackFactory ? '.dev' : ''}`;
const devServerConfigPath = `${scriptVersion}/config/webpackDevServer.config.js`;
const webpackConfig = require(webpackConfigPath);
const devServerConfig = require(devServerConfigPath);

// override config in memory
require.cache[require.resolve(webpackConfigPath)].exports =
isOldScript ? overrides.webpack(webpackConfig, process.env.NODE_ENV) : (env) => overrides.webpack(webpackConfig(env), env);
require.cache[require.resolve(webpackConfigPath)].exports = isWebpackFactory
? (env) => overrides.webpack(webpackConfig(env), env)
: overrides.webpack(webpackConfig, process.env.NODE_ENV);

require.cache[require.resolve(devServerConfigPath)].exports =
overrides.devServer(devServerConfig, process.env.NODE_ENV);

// run original script
require(paths.scriptVersion + "/scripts/start");
require(`${scriptVersion}/scripts/start`);

0 comments on commit 77e72d1

Please sign in to comment.