UNPKG

5.54 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 yarnLockFile: resolveApp('yarn.lock'),
88 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
89 proxySetup: resolveApp('src/setupProxy.js'),
90 appNodeModules: resolveApp('node_modules'),
91 publicUrl: getPublicUrl(resolveApp('package.json')),
92 servedPath: getServedPath(resolveApp('package.json')),
93};
94
95// @remove-on-eject-begin
96const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
97
98// config before eject: we're in ./node_modules/react-scripts/config/
99module.exports = {
100 dotenv: resolveApp('.env'),
101 appPath: resolveApp('.'),
102 appBuild: resolveApp('build'),
103 appPublic: resolveApp('public'),
104 appHtml: resolveApp('public/index.html'),
105 appIndexJs: resolveModule(resolveApp, 'src/index'),
106 appPackageJson: resolveApp('package.json'),
107 appSrc: resolveApp('src'),
108 appTsConfig: resolveApp('tsconfig.json'),
109 yarnLockFile: resolveApp('yarn.lock'),
110 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
111 proxySetup: resolveApp('src/setupProxy.js'),
112 appNodeModules: resolveApp('node_modules'),
113 publicUrl: getPublicUrl(resolveApp('package.json')),
114 servedPath: getServedPath(resolveApp('package.json')),
115 // These properties only exist before ejecting:
116 ownPath: resolveOwn('.'),
117 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
118 appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
119 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
120};
121
122const ownPackageJson = require('../package.json');
123const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
124const reactScriptsLinked =
125 fs.existsSync(reactScriptsPath) &&
126 fs.lstatSync(reactScriptsPath).isSymbolicLink();
127
128// config before publish: we're in ./packages/react-scripts/config/
129if (
130 !reactScriptsLinked &&
131 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
132) {
133 module.exports = {
134 dotenv: resolveOwn('template/.env'),
135 appPath: resolveApp('.'),
136 appBuild: resolveOwn('../../build'),
137 appPublic: resolveOwn('template/public'),
138 appHtml: resolveOwn('template/public/index.html'),
139 appIndexJs: resolveModule(resolveOwn, 'template/src/index'),
140 appPackageJson: resolveOwn('package.json'),
141 appSrc: resolveOwn('template/src'),
142 appTsConfig: resolveOwn('template/tsconfig.json'),
143 yarnLockFile: resolveOwn('template/yarn.lock'),
144 testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
145 proxySetup: resolveOwn('template/src/setupProxy.js'),
146 appNodeModules: resolveOwn('node_modules'),
147 publicUrl: getPublicUrl(resolveOwn('package.json')),
148 servedPath: getServedPath(resolveOwn('package.json')),
149 // These properties only exist before ejecting:
150 ownPath: resolveOwn('.'),
151 ownNodeModules: resolveOwn('node_modules'),
152 appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
153 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
154 };
155}
156// @remove-on-eject-end
157
158module.exports.moduleFileExtensions = moduleFileExtensions;