UNPKG

5.68 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 url = require('url');
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
20const envPublicUrl = process.env.PUBLIC_URL;
21
22function ensureSlash(inputPath, needsSlash) {
23 const hasSlash = inputPath.endsWith('/');
24 if (hasSlash && !needsSlash) {
25 return inputPath.substr(0, inputPath.length - 1);
26 } else if (!hasSlash && needsSlash) {
27 return `${inputPath}/`;
28 } else {
29 return inputPath;
30 }
31}
32
33const getPublicUrl = appPackageJson =>
34 envPublicUrl || require(appPackageJson).homepage;
35
36// We use `PUBLIC_URL` environment variable or "homepage" field to infer
37// "public path" at which the app is served.
38// Webpack needs to know it to put the right <script> hrefs into HTML even in
39// single-page apps that may serve index.html for nested URLs like /todos/42.
40// We can't use a relative path in HTML because we don't want to load something
41// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
42function getServedPath(appPackageJson) {
43 const publicUrl = getPublicUrl(appPackageJson);
44 const servedUrl =
45 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
46 return ensureSlash(servedUrl, true);
47}
48
49const moduleFileExtensions = [
50 'web.mjs',
51 'mjs',
52 'web.js',
53 'js',
54 'web.ts',
55 'ts',
56 'web.tsx',
57 'tsx',
58 'json',
59 'web.jsx',
60 'jsx',
61];
62
63// Resolve file paths in the same order as webpack
64const resolveModule = (resolveFn, filePath) => {
65 const extension = moduleFileExtensions.find(extension =>
66 fs.existsSync(resolveFn(`${filePath}.${extension}`))
67 );
68
69 if (extension) {
70 return resolveFn(`${filePath}.${extension}`);
71 }
72
73 return resolveFn(`${filePath}.js`);
74};
75
76// config after eject: we're in ./config/
77module.exports = {
78 dotenv: resolveApp('.env'),
79 appPath: resolveApp('.'),
80 appBuild: resolveApp('build'),
81 appPublic: resolveApp('public'),
82 appHtml: resolveApp('public/index.html'),
83 appIndexJs: resolveModule(resolveApp, 'src/index'),
84 appPackageJson: resolveApp('package.json'),
85 appSrc: resolveApp('src'),
86 appTsConfig: resolveApp('tsconfig.json'),
87 appJsConfig: resolveApp('jsconfig.json'),
88 yarnLockFile: resolveApp('yarn.lock'),
89 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
90 proxySetup: resolveApp('src/setupProxy.js'),
91 appNodeModules: resolveApp('node_modules'),
92 publicUrl: getPublicUrl(resolveApp('package.json')),
93 servedPath: getServedPath(resolveApp('package.json')),
94};
95
96// @remove-on-eject-begin
97const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
98
99// config before eject: we're in ./node_modules/react-scripts/config/
100module.exports = {
101 dotenv: resolveApp('.env'),
102 appPath: resolveApp('.'),
103 appBuild: resolveApp('build'),
104 appPublic: resolveApp('public'),
105 appHtml: resolveApp('public/index.html'),
106 appIndexJs: resolveModule(resolveApp, 'src/index'),
107 appPackageJson: resolveApp('package.json'),
108 appSrc: resolveApp('src'),
109 appTsConfig: resolveApp('tsconfig.json'),
110 appJsConfig: resolveApp('jsconfig.json'),
111 yarnLockFile: resolveApp('yarn.lock'),
112 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
113 proxySetup: resolveApp('src/setupProxy.js'),
114 appNodeModules: resolveApp('node_modules'),
115 publicUrl: getPublicUrl(resolveApp('package.json')),
116 servedPath: getServedPath(resolveApp('package.json')),
117 // These properties only exist before ejecting:
118 ownPath: resolveOwn('.'),
119 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
120 appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
121 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
122};
123
124const ownPackageJson = require('../package.json');
125const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
126const reactScriptsLinked =
127 fs.existsSync(reactScriptsPath) &&
128 fs.lstatSync(reactScriptsPath).isSymbolicLink();
129
130// config before publish: we're in ./packages/react-scripts/config/
131if (
132 !reactScriptsLinked &&
133 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
134) {
135 module.exports = {
136 dotenv: resolveOwn('template/.env'),
137 appPath: resolveApp('.'),
138 appBuild: resolveOwn('../../build'),
139 appPublic: resolveOwn('template/public'),
140 appHtml: resolveOwn('template/public/index.html'),
141 appIndexJs: resolveModule(resolveOwn, 'template/src/index'),
142 appPackageJson: resolveOwn('package.json'),
143 appSrc: resolveOwn('template/src'),
144 appTsConfig: resolveOwn('template/tsconfig.json'),
145 appJsConfig: resolveOwn('template/jsconfig.json'),
146 yarnLockFile: resolveOwn('template/yarn.lock'),
147 testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
148 proxySetup: resolveOwn('template/src/setupProxy.js'),
149 appNodeModules: resolveOwn('node_modules'),
150 publicUrl: getPublicUrl(resolveOwn('package.json')),
151 servedPath: getServedPath(resolveOwn('package.json')),
152 // These properties only exist before ejecting:
153 ownPath: resolveOwn('.'),
154 ownNodeModules: resolveOwn('node_modules'),
155 appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
156 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
157 };
158}
159// @remove-on-eject-end
160
161module.exports.moduleFileExtensions = moduleFileExtensions;