Skip to content

Commit 709dc40

Browse files
author
Brad Lemley
committed
Support for multiple source paths via package.json srcPaths entry.
1 parent 4b55193 commit 709dc40

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

packages/react-scripts/config/paths.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function getServedPath(appPackageJson) {
4646
return ensureSlash(servedUrl, true);
4747
}
4848

49+
const appPackageJson = require(resolveApp('package.json'));
50+
const srcPaths = appPackageJson.srcPaths || [];
51+
4952
// config after eject: we're in ./config/
5053
module.exports = {
5154
dotenv: resolveApp('.env'),
@@ -55,6 +58,7 @@ module.exports = {
5558
appIndexJs: resolveApp('src/index.js'),
5659
appPackageJson: resolveApp('package.json'),
5760
appSrc: resolveApp('src'),
61+
srcPaths: srcPaths.map(resolveApp),
5862
yarnLockFile: resolveApp('yarn.lock'),
5963
testsSetup: resolveApp('src/setupTests.js'),
6064
appNodeModules: resolveApp('node_modules'),
@@ -75,6 +79,7 @@ module.exports = {
7579
appIndexJs: resolveApp('src/index.js'),
7680
appPackageJson: resolveApp('package.json'),
7781
appSrc: resolveApp('src'),
82+
srcPaths: srcPaths.map(resolveApp),
7883
yarnLockFile: resolveApp('yarn.lock'),
7984
testsSetup: resolveApp('src/setupTests.js'),
8085
appNodeModules: resolveApp('node_modules'),
@@ -96,6 +101,8 @@ if (
96101
!reactScriptsLinked &&
97102
__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
98103
) {
104+
const appPackageJson = require(resolveOwn('package.json'));
105+
const srcPaths = appPackageJson.srcPaths || [];
99106
module.exports = {
100107
dotenv: resolveOwn('template/.env'),
101108
appPath: resolveApp('.'),
@@ -105,6 +112,7 @@ if (
105112
appIndexJs: resolveOwn('template/src/index.js'),
106113
appPackageJson: resolveOwn('package.json'),
107114
appSrc: resolveOwn('template/src'),
115+
srcPaths: srcPaths.map(resolveOwn),
108116
yarnLockFile: resolveOwn('template/yarn.lock'),
109117
testsSetup: resolveOwn('template/src/setupTests.js'),
110118
appNodeModules: resolveOwn('node_modules'),

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module.exports = {
8383
modules: ['node_modules', paths.appNodeModules].concat(
8484
// It is guaranteed to exist because we tweak it in `env.js`
8585
process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
86-
),
86+
).concat(paths.srcPaths),
8787
// These are the reasonable defaults supported by the Node ecosystem.
8888
// We also include JSX as a common component filename extension to support
8989
// some tools, although we do not recommend using it, see:
@@ -111,7 +111,9 @@ module.exports = {
111111
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
112112
// please link the files into your node_modules/ and let module-resolution kick in.
113113
// Make sure your source files are compiled, as they will not be processed in any way.
114-
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
114+
115+
// TODO: update ModuleScopePlugin to enforce rules with appSrc + srcPaths
116+
// new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
115117
],
116118
},
117119
module: {
@@ -142,7 +144,7 @@ module.exports = {
142144
loader: require.resolve('eslint-loader'),
143145
},
144146
],
145-
include: paths.appSrc,
147+
include: [paths.appSrc].concat(paths.srcPaths),
146148
},
147149
{
148150
// "oneOf" will traverse all following loaders until one will
@@ -163,7 +165,7 @@ module.exports = {
163165
// Process JS with Babel.
164166
{
165167
test: /\.(js|jsx|mjs)$/,
166-
include: paths.appSrc,
168+
include: [paths.appSrc].concat(paths.srcPaths),
167169
loader: require.resolve('babel-loader'),
168170
options: {
169171
// @remove-on-eject-begin

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = {
8989
modules: ['node_modules', paths.appNodeModules].concat(
9090
// It is guaranteed to exist because we tweak it in `env.js`
9191
process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
92-
),
92+
).concat(paths.srcPaths),
9393
// These are the reasonable defaults supported by the Node ecosystem.
9494
// We also include JSX as a common component filename extension to support
9595
// some tools, although we do not recommend using it, see:
@@ -117,7 +117,9 @@ module.exports = {
117117
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
118118
// please link the files into your node_modules/ and let module-resolution kick in.
119119
// Make sure your source files are compiled, as they will not be processed in any way.
120-
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
120+
121+
// TODO: update ModuleScopePlugin to enforce rules with appSrc + srcPaths
122+
// new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
121123
],
122124
},
123125
module: {
@@ -150,7 +152,7 @@ module.exports = {
150152
loader: require.resolve('eslint-loader'),
151153
},
152154
],
153-
include: paths.appSrc,
155+
include: [paths.appSrc].concat(paths.srcPaths),
154156
},
155157
{
156158
// "oneOf" will traverse all following loaders until one will
@@ -170,7 +172,7 @@ module.exports = {
170172
// Process JS with Babel.
171173
{
172174
test: /\.(js|jsx|mjs)$/,
173-
include: paths.appSrc,
175+
include: [paths.appSrc].concat(paths.srcPaths),
174176
loader: require.resolve('babel-loader'),
175177
options: {
176178
// @remove-on-eject-begin
@@ -301,7 +303,7 @@ module.exports = {
301303
},
302304
mangle: {
303305
safari10: true,
304-
},
306+
},
305307
output: {
306308
comments: false,
307309
// Turned on because emoji and regex is not minified properly using default

packages/react-scripts/scripts/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ argv.push(
4242
createJestConfig(
4343
relativePath => path.resolve(__dirname, '..', relativePath),
4444
path.resolve(paths.appSrc, '..'),
45+
paths.srcPaths,
4546
false
4647
)
4748
)

packages/react-scripts/scripts/utils/createJestConfig.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ const fs = require('fs');
1111
const chalk = require('chalk');
1212
const paths = require('../../config/paths');
1313

14-
module.exports = (resolve, rootDir, isEjecting) => {
14+
const srcRootTestMatch = srcRoot => ([
15+
srcRoot + '/**/__tests__/**/*.{js,jsx,mjs}',
16+
srcRoot + '/**/?(*.)(spec|test).{js,jsx,mjs}',
17+
])
18+
19+
const srcRootTestMatches = srcRoots =>
20+
srcRoots.reduce((m, srcRoot) => m.concat(srcRootTestMatch(srcRoot)), [])
21+
22+
module.exports = (resolve, rootDir, srcRoots, isEjecting) => {
1523
// Use this instead of `paths.testsSetup` to avoid putting
1624
// an absolute filename into configuration after ejecting.
1725
const setupTestsFile = fs.existsSync(paths.testsSetup)
@@ -27,7 +35,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
2735
testMatch: [
2836
'<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}',
2937
'<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}',
30-
],
38+
].concat(srcRootTestMatches(srcRoots)),
3139
testEnvironment: 'node',
3240
testURL: 'http://localhost',
3341
transform: {
@@ -56,6 +64,9 @@ module.exports = (resolve, rootDir, isEjecting) => {
5664
if (rootDir) {
5765
config.rootDir = rootDir;
5866
}
67+
if (srcRoots) {
68+
config.roots = [rootDir].concat(srcRoots);
69+
}
5970
const overrides = Object.assign({}, require(paths.appPackageJson).jest);
6071
const supportedKeys = [
6172
'collectCoverageFrom',

0 commit comments

Comments
 (0)