Skip to content

Commit 76e1801

Browse files
committed
Update CRA and fix breaking stuff
1 parent f21e4cb commit 76e1801

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5844
-6865
lines changed

.eslintrc.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

config/env.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
'use strict';
22

33
const fs = require('fs');
44
const path = require('path');
@@ -10,12 +10,12 @@ delete require.cache[require.resolve('./paths')];
1010
const NODE_ENV = process.env.NODE_ENV;
1111
if (!NODE_ENV) {
1212
throw new Error(
13-
'The NODE_ENV environment variable is required but was not specified.',
13+
'The NODE_ENV environment variable is required but was not specified.'
1414
);
1515
}
1616

1717
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
18-
var dotenvFiles = [
18+
const dotenvFiles = [
1919
`${paths.dotenv}.${NODE_ENV}.local`,
2020
`${paths.dotenv}.${NODE_ENV}`,
2121
// Don't include `.env.local` for `test` environment
@@ -35,7 +35,7 @@ dotenvFiles.forEach(dotenvFile => {
3535
require('dotenv-expand')(
3636
require('dotenv').config({
3737
path: dotenvFile,
38-
}),
38+
})
3939
);
4040
}
4141
});
@@ -77,8 +77,7 @@ function getClientEnvironment(publicUrl) {
7777
// This should only be used as an escape hatch. Normally you would put
7878
// images into the `src` and `import` them in code to get their paths.
7979
PUBLIC_URL: publicUrl,
80-
APP_ENV: process.env.APP_ENV || 'browser',
81-
},
80+
}
8281
);
8382
// Stringify all values so we can feed into Webpack DefinePlugin
8483
const stringified = {

config/jest/fileTransform.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const camelcase = require('camelcase');
45

56
// This is a custom Jest transformer turning file imports into filenames.
67
// http://facebook.github.io/jest/docs/en/webpack.html
@@ -10,17 +11,26 @@ module.exports = {
1011
const assetFilename = JSON.stringify(path.basename(filename));
1112

1213
if (filename.match(/\.svg$/)) {
13-
return `module.exports = {
14+
// Based on how SVGR generates a component name:
15+
// https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
16+
const pascalCaseFilename = camelcase(path.parse(filename).name, {
17+
pascalCase: true,
18+
});
19+
const componentName = `Svg${pascalCaseFilename}`;
20+
return `const React = require('react');
21+
module.exports = {
1422
__esModule: true,
1523
default: ${assetFilename},
16-
ReactComponent: (props) => ({
17-
$$typeof: Symbol.for('react.element'),
18-
type: 'svg',
19-
ref: null,
20-
key: null,
21-
props: Object.assign({}, props, {
22-
children: ${assetFilename}
23-
})
24+
ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
25+
return {
26+
$$typeof: Symbol.for('react.element'),
27+
type: 'svg',
28+
ref: ref,
29+
key: null,
30+
props: Object.assign({}, props, {
31+
children: ${assetFilename}
32+
})
33+
};
2434
}),
2535
};`;
2636
}

config/modules.js

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

config/paths.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const path = require('path');
24
const fs = require('fs');
35
const url = require('url');
@@ -53,7 +55,7 @@ const moduleFileExtensions = [
5355
// Resolve file paths in the same order as webpack
5456
const resolveModule = (resolveFn, filePath) => {
5557
const extension = moduleFileExtensions.find(extension =>
56-
fs.existsSync(resolveFn(`${filePath}.${extension}`)),
58+
fs.existsSync(resolveFn(`${filePath}.${extension}`))
5759
);
5860

5961
if (extension) {
@@ -74,14 +76,15 @@ module.exports = {
7476
appPackageJson: resolveApp('package.json'),
7577
appSrc: resolveApp('src'),
7678
appTsConfig: resolveApp('tsconfig.json'),
79+
appJsConfig: resolveApp('jsconfig.json'),
7780
yarnLockFile: resolveApp('yarn.lock'),
7881
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
7982
proxySetup: resolveApp('src/setupProxy.js'),
8083
appNodeModules: resolveApp('node_modules'),
8184
publicUrl: getPublicUrl(resolveApp('package.json')),
8285
servedPath: getServedPath(resolveApp('package.json')),
83-
ssrIndexJs: resolveApp('src/server'),
84-
ssrBuild: resolveApp('dist'),
8586
};
8687

88+
89+
8790
module.exports.moduleFileExtensions = moduleFileExtensions;

config/pnpTs.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const { resolveModuleName } = require('ts-pnp');
4+
5+
exports.resolveModuleName = (
6+
typescript,
7+
moduleName,
8+
containingFile,
9+
compilerOptions,
10+
resolutionHost
11+
) => {
12+
return resolveModuleName(
13+
moduleName,
14+
containingFile,
15+
compilerOptions,
16+
resolutionHost,
17+
typescript.resolveModuleName
18+
);
19+
};
20+
21+
exports.resolveTypeReferenceDirective = (
22+
typescript,
23+
moduleName,
24+
containingFile,
25+
compilerOptions,
26+
resolutionHost
27+
) => {
28+
return resolveModuleName(
29+
moduleName,
30+
containingFile,
31+
compilerOptions,
32+
resolutionHost,
33+
typescript.resolveTypeReferenceDirective
34+
);
35+
};

0 commit comments

Comments
 (0)