Skip to content

Commit ff40f68

Browse files
committed
3.2.0 modules changes and copyright updates
1 parent 65f0048 commit ff40f68

File tree

8 files changed

+210
-7
lines changed

8 files changed

+210
-7
lines changed

packages/react-scripts/LICENSE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
MIT License
22

3-
Copyright (c) 2013-present, Facebook, Inc.
3+
Some files: Copyright (c) 2013-present, Facebook, Inc.
4+
Some files: Copyright (c) 2019-present, https://github.com/devloco
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

packages/react-scripts/bin/wptheme-scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
/**
3-
* Copyright (c) 2018-present, https://github.com/devloco
3+
* Copyright (c) 2019-present, https://github.com/devloco
44
*
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2019-present, https://github.com/devloco
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
const paths = require('./paths-wptheme');
14+
const chalk = require('react-dev-utils/chalk');
15+
const resolve = require('resolve');
16+
17+
/**
18+
* Get additional module paths based on the baseUrl of a compilerOptions object.
19+
*
20+
* @param {Object} options
21+
*/
22+
function getAdditionalModulePaths(options = {}) {
23+
const baseUrl = options.baseUrl;
24+
25+
// We need to explicitly check for null and undefined (and not a falsy value) because
26+
// TypeScript treats an empty string as `.`.
27+
if (baseUrl == null) {
28+
// If there's no baseUrl set we respect NODE_PATH
29+
// Note that NODE_PATH is deprecated and will be removed
30+
// in the next major release of create-react-app.
31+
32+
const nodePath = process.env.NODE_PATH || '';
33+
return nodePath.split(path.delimiter).filter(Boolean);
34+
}
35+
36+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
37+
38+
// We don't need to do anything if `baseUrl` is set to `node_modules`. This is
39+
// the default behavior.
40+
if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
41+
return null;
42+
}
43+
44+
// Allow the user set the `baseUrl` to `appSrc`.
45+
if (path.relative(paths.appSrc, baseUrlResolved) === '') {
46+
return [paths.appSrc];
47+
}
48+
49+
// If the path is equal to the root directory we ignore it here.
50+
// We don't want to allow importing from the root directly as source files are
51+
// not transpiled outside of `src`. We do allow importing them with the
52+
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
53+
// an alias.
54+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
55+
return null;
56+
}
57+
58+
// Otherwise, throw an error.
59+
throw new Error(
60+
chalk.red.bold(
61+
"Your project's `baseUrl` can only be set to `src` or `node_modules`." +
62+
' Create React App does not support other values at this time.'
63+
)
64+
);
65+
}
66+
67+
/**
68+
* Get webpack aliases based on the baseUrl of a compilerOptions object.
69+
*
70+
* @param {*} options
71+
*/
72+
function getWebpackAliases(options = {}) {
73+
const baseUrl = options.baseUrl;
74+
75+
if (!baseUrl) {
76+
return {};
77+
}
78+
79+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
80+
81+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
82+
return {
83+
src: paths.appSrc,
84+
};
85+
}
86+
}
87+
88+
/**
89+
* Get jest aliases based on the baseUrl of a compilerOptions object.
90+
*
91+
* @param {*} options
92+
*/
93+
function getJestAliases(options = {}) {
94+
const baseUrl = options.baseUrl;
95+
96+
if (!baseUrl) {
97+
return {};
98+
}
99+
100+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
101+
102+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
103+
return {
104+
'src/(.*)$': '<rootDir>/src/$1',
105+
};
106+
}
107+
}
108+
109+
function getModules() {
110+
// Check if TypeScript is setup
111+
const hasTsConfig = fs.existsSync(paths.appTsConfig);
112+
const hasJsConfig = fs.existsSync(paths.appJsConfig);
113+
114+
if (hasTsConfig && hasJsConfig) {
115+
throw new Error(
116+
'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
117+
);
118+
}
119+
120+
let config;
121+
122+
// If there's a tsconfig.json we assume it's a
123+
// TypeScript project and set up the config
124+
// based on tsconfig.json
125+
if (hasTsConfig) {
126+
const ts = require(resolve.sync('typescript', {
127+
basedir: paths.appNodeModules,
128+
}));
129+
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
130+
// Otherwise we'll check if there is jsconfig.json
131+
// for non TS projects.
132+
} else if (hasJsConfig) {
133+
config = require(paths.appJsConfig);
134+
}
135+
136+
config = config || {};
137+
const options = config.compilerOptions || {};
138+
139+
const additionalModulePaths = getAdditionalModulePaths(options);
140+
141+
return {
142+
additionalModulePaths: additionalModulePaths,
143+
webpackAliases: getWebpackAliases(options),
144+
jestAliases: getJestAliases(options),
145+
hasTsConfig,
146+
};
147+
}
148+
149+
module.exports = getModules();

packages/react-scripts/config/modules.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const chalk = require('react-dev-utils/chalk');
1515
const resolve = require('resolve');
1616

1717
/**
18-
* Get the baseUrl of a compilerOptions object.
18+
* Get additional module paths based on the baseUrl of a compilerOptions object.
1919
*
2020
* @param {Object} options
2121
*/
@@ -46,6 +46,15 @@ function getAdditionalModulePaths(options = {}) {
4646
return [paths.appSrc];
4747
}
4848

49+
// If the path is equal to the root directory we ignore it here.
50+
// We don't want to allow importing from the root directly as source files are
51+
// not transpiled outside of `src`. We do allow importing them with the
52+
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
53+
// an alias.
54+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
55+
return null;
56+
}
57+
4958
// Otherwise, throw an error.
5059
throw new Error(
5160
chalk.red.bold(
@@ -55,6 +64,48 @@ function getAdditionalModulePaths(options = {}) {
5564
);
5665
}
5766

67+
/**
68+
* Get webpack aliases based on the baseUrl of a compilerOptions object.
69+
*
70+
* @param {*} options
71+
*/
72+
function getWebpackAliases(options = {}) {
73+
const baseUrl = options.baseUrl;
74+
75+
if (!baseUrl) {
76+
return {};
77+
}
78+
79+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
80+
81+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
82+
return {
83+
src: paths.appSrc,
84+
};
85+
}
86+
}
87+
88+
/**
89+
* Get jest aliases based on the baseUrl of a compilerOptions object.
90+
*
91+
* @param {*} options
92+
*/
93+
function getJestAliases(options = {}) {
94+
const baseUrl = options.baseUrl;
95+
96+
if (!baseUrl) {
97+
return {};
98+
}
99+
100+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
101+
102+
if (path.relative(paths.appPath, baseUrlResolved) === '') {
103+
return {
104+
'src/(.*)$': '<rootDir>/src/$1',
105+
};
106+
}
107+
}
108+
58109
function getModules() {
59110
// Check if TypeScript is setup
60111
const hasTsConfig = fs.existsSync(paths.appTsConfig);
@@ -89,6 +140,8 @@ function getModules() {
89140

90141
return {
91142
additionalModulePaths: additionalModulePaths,
143+
webpackAliases: getWebpackAliases(options),
144+
jestAliases: getJestAliases(options),
92145
hasTsConfig,
93146
};
94147
}

packages/react-scripts/config/paths-wptheme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @remove-on-eject-begin
22
/**
3-
* Copyright (c) 2015-present, Facebook, Inc.
3+
* Copyright (c) 2019-present, https://github.com/devloco
44
*
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.

packages/react-scripts/scripts/init-wptheme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @remove-file-on-eject
22
/**
3-
* Copyright (c) 2015-present, Facebook, Inc.
3+
* Copyright (c) 2019-present, https://github.com/devloco
44
*
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.

packages/react-scripts/scripts/wpbuild.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @remove-on-eject-begin
22
/**
3-
* Copyright (c) 2015-present, Facebook, Inc.
3+
* Copyright (c) 2019-present, https://github.com/devloco
44
*
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.

packages/react-scripts/scripts/wpstart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @remove-on-eject-begin
22
/**
3-
* Copyright (c) 2015-present, Facebook, Inc.
3+
* Copyright (c) 2019-present, https://github.com/devloco
44
*
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.

0 commit comments

Comments
 (0)