1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | const fs = require('fs');
|
12 | const path = require('path');
|
13 | const paths = require('./paths');
|
14 | const chalk = require('react-dev-utils/chalk');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function getAdditionalModulePaths(options = {}) {
|
22 | const baseUrl = options.baseUrl;
|
23 |
|
24 |
|
25 |
|
26 | if (baseUrl == null) {
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const nodePath = process.env.NODE_PATH || '';
|
32 | return nodePath.split(path.delimiter).filter(Boolean);
|
33 | }
|
34 |
|
35 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
|
36 |
|
37 |
|
38 |
|
39 | if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
|
40 | return null;
|
41 | }
|
42 |
|
43 |
|
44 | if (path.relative(paths.appSrc, baseUrlResolved) === '') {
|
45 | return [paths.appSrc];
|
46 | }
|
47 |
|
48 |
|
49 | throw new Error(
|
50 | chalk.red.bold(
|
51 | "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
|
52 | ' Create React App does not support other values at this time.'
|
53 | )
|
54 | );
|
55 | }
|
56 |
|
57 | function getModules() {
|
58 |
|
59 | const hasTsConfig = fs.existsSync(paths.appTsConfig);
|
60 | const hasJsConfig = fs.existsSync(paths.appJsConfig);
|
61 |
|
62 | if (hasTsConfig && hasJsConfig) {
|
63 | throw new Error(
|
64 | 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
|
65 | );
|
66 | }
|
67 |
|
68 | let config;
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | if (hasTsConfig) {
|
74 | config = require(paths.appTsConfig);
|
75 |
|
76 |
|
77 | } else if (hasJsConfig) {
|
78 | config = require(paths.appJsConfig);
|
79 | }
|
80 |
|
81 | config = config || {};
|
82 | const options = config.compilerOptions || {};
|
83 |
|
84 | const additionalModulePaths = getAdditionalModulePaths(options);
|
85 |
|
86 | return {
|
87 | additionalModulePaths: additionalModulePaths,
|
88 | hasTsConfig,
|
89 | };
|
90 | }
|
91 |
|
92 | module.exports = getModules();
|