From d7aae72f8164233379f690010892033479a1e5f4 Mon Sep 17 00:00:00 2001 From: vdfor Date: Tue, 25 Dec 2018 18:24:04 +0800 Subject: [PATCH 1/3] react-app-rewired support react-scripts more than 2.1.2 version --- packages/react-app-rewired/scripts/build.js | 8 ++++++-- packages/react-app-rewired/scripts/start.js | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/react-app-rewired/scripts/build.js b/packages/react-app-rewired/scripts/build.js index b9e43fa..7ee68a0 100644 --- a/packages/react-app-rewired/scripts/build.js +++ b/packages/react-app-rewired/scripts/build.js @@ -2,12 +2,16 @@ process.env.NODE_ENV = 'production'; const paths = require('./utils/paths'); const overrides = require('../config-overrides'); -const webpackConfigPath = paths.scriptVersion + "/config/webpack.config.prod"; +const scriptPkg = require(paths.scriptVersion + "/package.json"); + +const isOldScript = !(scriptPkg && scriptPkg.version >= '2.1.2'); + +const webpackConfigPath = paths.scriptVersion + isOldScript ? "/config/webpack.config.prod" : "/config/webpack.config"; // load original config const webpackConfig = require(webpackConfigPath); // override config in memory require.cache[require.resolve(webpackConfigPath)].exports = - overrides.webpack(webpackConfig, process.env.NODE_ENV); + isOldScript ? overrides.webpack(webpackConfig, process.env.NODE_ENV) : (env) => overrides.webpack(webpackConfig(env), env); // run original script require(paths.scriptVersion + '/scripts/build'); diff --git a/packages/react-app-rewired/scripts/start.js b/packages/react-app-rewired/scripts/start.js index fea8b38..5aae597 100644 --- a/packages/react-app-rewired/scripts/start.js +++ b/packages/react-app-rewired/scripts/start.js @@ -2,7 +2,11 @@ process.env.NODE_ENV = process.env.NODE_ENV || "development"; const paths = require("./utils/paths"); const overrides = require('../config-overrides'); -const webpackConfigPath = paths.scriptVersion + "/config/webpack.config.dev"; +const scriptPkg = require(paths.scriptVersion + "/package.json"); + +const isOldScript = !(scriptPkg && scriptPkg.version >= '2.1.2'); + +const webpackConfigPath = paths.scriptVersion + isOldScript ? "/config/webpack.config.dev" : "/config/webpack.config"; const devServerConfigPath = paths.scriptVersion + "/config/webpackDevServer.config.js"; // load original configs @@ -10,7 +14,7 @@ const webpackConfig = require(webpackConfigPath); const devServerConfig = require(devServerConfigPath); // override config in memory require.cache[require.resolve(webpackConfigPath)].exports = - overrides.webpack(webpackConfig, process.env.NODE_ENV); + isOldScript ? overrides.webpack(webpackConfig, process.env.NODE_ENV) : (env) => overrides.webpack(webpackConfig(env), env); require.cache[require.resolve(devServerConfigPath)].exports = overrides.devServer(devServerConfig, process.env.NODE_ENV); From 77e72d1936af185226bdfaaa742c99174b1ca6e0 Mon Sep 17 00:00:00 2001 From: Joe Farro Date: Wed, 2 Jan 2019 13:57:35 -0500 Subject: [PATCH 2/3] Fix for CRA 2.1.2 #343, builds on #344 --- packages/react-app-rewired/package.json | 3 ++- packages/react-app-rewired/scripts/build.js | 23 +++++++++++------- packages/react-app-rewired/scripts/start.js | 27 ++++++++++++--------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/react-app-rewired/package.json b/packages/react-app-rewired/package.json index 46d11e7..50d2725 100644 --- a/packages/react-app-rewired/package.json +++ b/packages/react-app-rewired/package.json @@ -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", diff --git a/packages/react-app-rewired/scripts/build.js b/packages/react-app-rewired/scripts/build.js index 7ee68a0..723d98f 100644 --- a/packages/react-app-rewired/scripts/build.js +++ b/packages/react-app-rewired/scripts/build.js @@ -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`); diff --git a/packages/react-app-rewired/scripts/start.js b/packages/react-app-rewired/scripts/start.js index 5aae597..ea4ebf1 100644 --- a/packages/react-app-rewired/scripts/start.js +++ b/packages/react-app-rewired/scripts/start.js @@ -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"); \ No newline at end of file +require(`${scriptVersion}/scripts/start`); From 47830a88e9f0ff12c3e6c30f9b8ef52d88838bd1 Mon Sep 17 00:00:00 2001 From: Joe Farro Date: Wed, 2 Jan 2019 14:24:26 -0500 Subject: [PATCH 3/3] See about using node 6 and 8 in CI --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 67f9971..44dec88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: node_js node_js: - - "7" + - "6" + - "8" cache: yarn: true \ No newline at end of file