Skip to content

Commit

Permalink
feat: Mock react-scripts 'config' folder (#593)
Browse files Browse the repository at this point in the history
* 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
bencergazda authored Feb 14, 2022
1 parent 36375e3 commit 6372ca2
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 81 deletions.
1 change: 1 addition & 0 deletions config/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../overrides/paths');
4 changes: 4 additions & 0 deletions config/webpack.config.js
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');
4 changes: 4 additions & 0 deletions config/webpackDevServer.config.js
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');
11 changes: 11 additions & 0 deletions overrides/devServer.js
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);
31 changes: 31 additions & 0 deletions overrides/jest.js
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);
14 changes: 14 additions & 0 deletions overrides/paths.js
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);
21 changes: 21 additions & 0 deletions overrides/webpack.js
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);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"/config-overrides.js",
"/assets",
"/bin",
"/overrides",
"/scripts"
]
}
22 changes: 2 additions & 20 deletions scripts/build.js
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`);
28 changes: 3 additions & 25 deletions scripts/start.js
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`);
41 changes: 5 additions & 36 deletions scripts/test.js
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`);

0 comments on commit 6372ca2

Please sign in to comment.