UNPKG

5.35 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
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
11const path = require('path');
12const fs = require('fs');
13const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
14
15// Make sure any symlinks in the project folder are resolved:
16// https://github.com/facebook/create-react-app/issues/637
17const appDirectory = fs.realpathSync(process.cwd());
18const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
19
20// We use `PUBLIC_URL` environment variable or "homepage" field to infer
21// "public path" at which the app is served.
22// webpack needs to know it to put the right <script> hrefs into HTML even in
23// single-page apps that may serve index.html for nested URLs like /todos/42.
24// We can't use a relative path in HTML because we don't want to load something
25// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
26const publicUrlOrPath = getPublicUrlOrPath(
27 process.env.NODE_ENV === 'development',
28 require(resolveApp('package.json')).homepage,
29 process.env.PUBLIC_URL
30);
31
32const buildPath = process.env.BUILD_PATH || 'build';
33
34const moduleFileExtensions = [
35 'web.mjs',
36 'mjs',
37 'web.js',
38 'js',
39 'web.ts',
40 'ts',
41 'web.tsx',
42 'tsx',
43 'json',
44 'web.jsx',
45 'jsx',
46];
47
48// Resolve file paths in the same order as webpack
49const resolveModule = (resolveFn, filePath) => {
50 const extension = moduleFileExtensions.find(extension =>
51 fs.existsSync(resolveFn(`${filePath}.${extension}`))
52 );
53
54 if (extension) {
55 return resolveFn(`${filePath}.${extension}`);
56 }
57
58 return resolveFn(`${filePath}.js`);
59};
60
61// config after eject: we're in ./config/
62module.exports = {
63 dotenv: resolveApp('.env'),
64 appPath: resolveApp('.'),
65 appBuild: resolveApp(buildPath),
66 appPublic: resolveApp('public'),
67 appHtml: resolveApp('public/index.html'),
68 appIndexJs: resolveModule(resolveApp, 'src/index'),
69 appPackageJson: resolveApp('package.json'),
70 appSrc: resolveApp('src'),
71 appTsConfig: resolveApp('tsconfig.json'),
72 appJsConfig: resolveApp('jsconfig.json'),
73 yarnLockFile: resolveApp('yarn.lock'),
74 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
75 proxySetup: resolveApp('src/setupProxy.js'),
76 appNodeModules: resolveApp('node_modules'),
77 swSrc: resolveModule(resolveApp, 'src/service-worker'),
78 publicUrlOrPath,
79};
80
81// @remove-on-eject-begin
82const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
83
84// config before eject: we're in ./node_modules/react-scripts/config/
85module.exports = {
86 dotenv: resolveApp('.env'),
87 appPath: resolveApp('.'),
88 appBuild: resolveApp(buildPath),
89 appPublic: resolveApp('public'),
90 appHtml: resolveApp('public/index.html'),
91 appIndexJs: resolveModule(resolveApp, 'src/index'),
92 appPackageJson: resolveApp('package.json'),
93 appSrc: resolveApp('src'),
94 appTsConfig: resolveApp('tsconfig.json'),
95 appJsConfig: resolveApp('jsconfig.json'),
96 yarnLockFile: resolveApp('yarn.lock'),
97 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
98 proxySetup: resolveApp('src/setupProxy.js'),
99 appNodeModules: resolveApp('node_modules'),
100 swSrc: resolveModule(resolveApp, 'src/service-worker'),
101 publicUrlOrPath,
102 // These properties only exist before ejecting:
103 ownPath: resolveOwn('.'),
104 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
105 appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
106 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
107};
108
109const ownPackageJson = require('../package.json');
110const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
111const reactScriptsLinked =
112 fs.existsSync(reactScriptsPath) &&
113 fs.lstatSync(reactScriptsPath).isSymbolicLink();
114
115// config before publish: we're in ./packages/react-scripts/config/
116if (
117 !reactScriptsLinked &&
118 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
119) {
120 const templatePath = '../cra-template/template';
121 module.exports = {
122 dotenv: resolveOwn(`${templatePath}/.env`),
123 appPath: resolveApp('.'),
124 appBuild: resolveOwn(path.join('../..', buildPath)),
125 appPublic: resolveOwn(`${templatePath}/public`),
126 appHtml: resolveOwn(`${templatePath}/public/index.html`),
127 appIndexJs: resolveModule(resolveOwn, `${templatePath}/src/index`),
128 appPackageJson: resolveOwn('package.json'),
129 appSrc: resolveOwn(`${templatePath}/src`),
130 appTsConfig: resolveOwn(`${templatePath}/tsconfig.json`),
131 appJsConfig: resolveOwn(`${templatePath}/jsconfig.json`),
132 yarnLockFile: resolveOwn(`${templatePath}/yarn.lock`),
133 testsSetup: resolveModule(resolveOwn, `${templatePath}/src/setupTests`),
134 proxySetup: resolveOwn(`${templatePath}/src/setupProxy.js`),
135 appNodeModules: resolveOwn('node_modules'),
136 swSrc: resolveModule(resolveOwn, `${templatePath}/src/service-worker`),
137 publicUrlOrPath,
138 // These properties only exist before ejecting:
139 ownPath: resolveOwn('.'),
140 ownNodeModules: resolveOwn('node_modules'),
141 appTypeDeclarations: resolveOwn(`${templatePath}/src/react-app-env.d.ts`),
142 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
143 };
144}
145// @remove-on-eject-end
146
147module.exports.moduleFileExtensions = moduleFileExtensions;