-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Mock react-scripts 'config' folder (#593)
* feat: Add `config` folder with `paths.js`, `webpack.config.js` and `webpackDevServer.config.js` * fix: Remove duplicated `paths` override from `overrides/webpack.js` * fix: `paths` overrides also in `config/webpack.config.js` and `config/webpackDevServer.config.js`
- Loading branch information
1 parent
36375e3
commit 6372ca2
Showing
11 changed files
with
97 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../overrides/paths'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// override paths in memory | ||
require('../overrides/paths'); | ||
|
||
module.exports = require('../overrides/webpack'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// override paths in memory | ||
require('../overrides/paths'); | ||
|
||
module.exports = require('../overrides/devServer'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { scriptVersion } = require('../scripts/utils/paths'); | ||
const overrides = require('../config-overrides'); | ||
|
||
const devServerConfigPath = `${scriptVersion}/config/webpackDevServer.config.js`; | ||
const devServerConfig = require(devServerConfigPath); | ||
|
||
// override config in memory | ||
require.cache[require.resolve(devServerConfigPath)].exports = | ||
overrides.devServer(devServerConfig, process.env.NODE_ENV); | ||
|
||
module.exports = require(devServerConfigPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const path = require('path'); | ||
const paths = require('./paths'); | ||
const overrides = require('../config-overrides'); | ||
const rewireJestConfig = require('../scripts/utils/rewireJestConfig'); | ||
const createJestConfigPath = `${paths.scriptVersion}/scripts/utils/createJestConfig`; | ||
|
||
// hide overrides in package.json for CRA's original createJestConfig | ||
const packageJson = require(paths.appPackageJson); | ||
const jestOverrides = packageJson.jest; | ||
delete packageJson.jest; | ||
// load original createJestConfig | ||
const createJestConfig = require(createJestConfigPath); | ||
// run original createJestConfig | ||
const config = createJestConfig( | ||
relativePath => path.resolve(paths.appPath, 'node_modules', paths.scriptVersion, relativePath), | ||
path.resolve(paths.appSrc, '..'), | ||
false | ||
); | ||
// restore overrides for rewireJestConfig | ||
packageJson.jest = jestOverrides; | ||
// override createJestConfig in memory | ||
require.cache[require.resolve(createJestConfigPath)].exports = | ||
() => overrides.jest(rewireJestConfig(config)); | ||
// Passing the --scripts-version on to the original test script can result | ||
// in the test script rejecting it as an invalid option. So strip it out of | ||
// the command line arguments before invoking the test script. | ||
if (paths.customScriptsIndex > -1) { | ||
process.argv.splice(paths.customScriptsIndex, 2); | ||
} | ||
|
||
module.exports = require(createJestConfigPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const paths = require('../scripts/utils/paths'); | ||
const overrides = require('../config-overrides'); | ||
|
||
const pathsConfigPath = `${paths.scriptVersion}/config/paths.js`; | ||
const pathsConfig = require(pathsConfigPath); | ||
|
||
// extend paths with overrides | ||
const extendedPaths = Object.assign({}, paths, overrides.paths(pathsConfig, process.env.NODE_ENV)); | ||
|
||
// override paths in memory | ||
require.cache[require.resolve(pathsConfigPath)].exports = | ||
extendedPaths; | ||
|
||
module.exports = require(pathsConfigPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const semver = require('semver'); | ||
|
||
const { scriptVersion } = require('../scripts/utils/paths'); | ||
const overrides = require('../config-overrides'); | ||
const scriptPkg = require(`${scriptVersion}/package.json`); | ||
|
||
// 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'); | ||
const webpackFactoryEnvSuffix = process.env.NODE_ENV === 'production' ? '.prod' : '.dev'; | ||
|
||
const webpackConfigPath = `${scriptVersion}/config/webpack.config${!isWebpackFactory ? webpackFactoryEnvSuffix : ''}`; | ||
const webpackConfig = require(webpackConfigPath); | ||
|
||
// override config in memory | ||
require.cache[require.resolve(webpackConfigPath)].exports = isWebpackFactory | ||
? (env) => overrides.webpack(webpackConfig(env), env) | ||
: overrides.webpack(webpackConfig, process.env.NODE_ENV); | ||
|
||
module.exports = require(webpackConfigPath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
"/config-overrides.js", | ||
"/assets", | ||
"/bin", | ||
"/overrides", | ||
"/scripts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,12 @@ | ||
process.env.NODE_ENV = 'production'; | ||
|
||
const semver = require('semver'); | ||
|
||
const { scriptVersion } = require('./utils/paths'); | ||
const overrides = require('../config-overrides'); | ||
const scriptPkg = require(`${scriptVersion}/package.json`); | ||
|
||
const pathsConfigPath = `${scriptVersion}/config/paths.js`; | ||
const pathsConfig = require(pathsConfigPath); | ||
|
||
// override paths in memory | ||
require.cache[require.resolve(pathsConfigPath)].exports = | ||
overrides.paths(pathsConfig, process.env.NODE_ENV); | ||
|
||
// 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'); | ||
|
||
const webpackConfigPath = `${scriptVersion}/config/webpack.config${!isWebpackFactory ? '.prod' : ''}`; | ||
const webpackConfig = require(webpackConfigPath); | ||
require('../overrides/paths'); | ||
|
||
// override config in memory | ||
require.cache[require.resolve(webpackConfigPath)].exports = isWebpackFactory | ||
? (env) => overrides.webpack(webpackConfig(env), env) | ||
: overrides.webpack(webpackConfig, process.env.NODE_ENV); | ||
require('../overrides/webpack'); | ||
|
||
// run original script | ||
require(`${scriptVersion}/scripts/build`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,13 @@ | ||
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; | ||
|
||
const semver = require('semver'); | ||
|
||
const { scriptVersion } = require('./utils/paths'); | ||
const overrides = require('../config-overrides'); | ||
const scriptPkg = require(`${scriptVersion}/package.json`); | ||
|
||
const pathsConfigPath = `${scriptVersion}/config/paths.js`; | ||
const pathsConfig = require(pathsConfigPath); | ||
|
||
// override paths in memory | ||
require.cache[require.resolve(pathsConfigPath)].exports = | ||
overrides.paths(pathsConfig, process.env.NODE_ENV); | ||
|
||
// 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'); | ||
|
||
const webpackConfigPath = `${scriptVersion}/config/webpack.config${!isWebpackFactory ? '.dev' : ''}`; | ||
const devServerConfigPath = `${scriptVersion}/config/webpackDevServer.config.js`; | ||
const webpackConfig = require(webpackConfigPath); | ||
const devServerConfig = require(devServerConfigPath); | ||
require('../overrides/paths'); | ||
|
||
// override config in memory | ||
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); | ||
require('../overrides/webpack'); | ||
require('../overrides/devServer'); | ||
|
||
// run original script | ||
require(`${scriptVersion}/scripts/start`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,12 @@ | ||
process.env.NODE_ENV = process.env.NODE_ENV || 'test'; | ||
|
||
const path = require("path"); | ||
let paths = require("./utils/paths"); | ||
const overrides = require('../config-overrides'); | ||
const rewireJestConfig = require("./utils/rewireJestConfig"); | ||
const createJestConfigPath = paths.scriptVersion + "/scripts/utils/createJestConfig"; | ||
|
||
const pathsConfigPath = `${paths.scriptVersion}/config/paths.js`; | ||
const pathsConfig = require(pathsConfigPath); | ||
|
||
// extend paths with overrides | ||
paths = Object.assign({}, paths, overrides.paths(pathsConfig, process.env.NODE_ENV)); | ||
const { scriptVersion } = require('./utils/paths'); | ||
|
||
// override paths in memory | ||
require.cache[require.resolve(pathsConfigPath)].exports = | ||
paths; | ||
require('../overrides/paths'); | ||
|
||
// hide overrides in package.json for CRA's original createJestConfig | ||
const packageJson = require(paths.appPackageJson); | ||
const jestOverrides = packageJson.jest; | ||
delete packageJson.jest; | ||
// load original createJestConfig | ||
const createJestConfig = require(createJestConfigPath); | ||
// run original createJestConfig | ||
const config = createJestConfig( | ||
relativePath => path.resolve(paths.appPath, "node_modules", paths.scriptVersion, relativePath), | ||
path.resolve(paths.appSrc, ".."), | ||
false | ||
); | ||
// restore overrides for rewireJestConfig | ||
packageJson.jest = jestOverrides; | ||
// override createJestConfig in memory | ||
require.cache[require.resolve(createJestConfigPath)].exports = | ||
() => overrides.jest(rewireJestConfig(config)); | ||
// Passing the --scripts-version on to the original test script can result | ||
// in the test script rejecting it as an invalid option. So strip it out of | ||
// the command line arguments before invoking the test script. | ||
if (paths.customScriptsIndex > -1) { | ||
process.argv.splice(paths.customScriptsIndex, 2); | ||
} | ||
require('../overrides/jest'); | ||
|
||
// run original script | ||
require(paths.scriptVersion + '/scripts/test'); | ||
require(`${scriptVersion}/scripts/test`); |