UNPKG

4.52 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
49// config after eject: we're in ./config/
50module.exports = {
51 dotenv: resolveApp('.env'),
52 appPath: resolveApp('.'),
53 appBuild: resolveApp('build'),
54 appPublic: resolveApp('public'),
55 appHtml: resolveApp('public/index.html'),
56 appIndexJs: resolveApp('src/index.js'),
57 appPackageJson: resolveApp('package.json'),
58 appSrc: resolveApp('src'),
59 yarnLockFile: resolveApp('yarn.lock'),
60 testsSetup: resolveApp('src/setupTests.js'),
61 proxySetup: resolveApp('src/setupProxy.js'),
62 appNodeModules: resolveApp('node_modules'),
63 publicUrl: getPublicUrl(resolveApp('package.json')),
64 servedPath: getServedPath(resolveApp('package.json')),
65};
66
67// @remove-on-eject-begin
68const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
69
70// config before eject: we're in ./node_modules/react-scripts/config/
71module.exports = {
72 dotenv: resolveApp('.env'),
73 appPath: resolveApp('.'),
74 appBuild: resolveApp('build'),
75 appPublic: resolveApp('public'),
76 appHtml: resolveApp('public/index.html'),
77 appIndexJs: resolveApp('src/index.js'),
78 appPackageJson: resolveApp('package.json'),
79 appSrc: resolveApp('src'),
80 yarnLockFile: resolveApp('yarn.lock'),
81 testsSetup: resolveApp('src/setupTests.js'),
82 proxySetup: resolveApp('src/setupProxy.js'),
83 appNodeModules: resolveApp('node_modules'),
84 publicUrl: getPublicUrl(resolveApp('package.json')),
85 servedPath: getServedPath(resolveApp('package.json')),
86 // These properties only exist before ejecting:
87 ownPath: resolveOwn('.'),
88 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
89};
90
91const ownPackageJson = require('../package.json');
92const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
93const reactScriptsLinked =
94 fs.existsSync(reactScriptsPath) &&
95 fs.lstatSync(reactScriptsPath).isSymbolicLink();
96
97// config before publish: we're in ./packages/react-scripts/config/
98if (
99 !reactScriptsLinked &&
100 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
101) {
102 module.exports = {
103 dotenv: resolveOwn('template/.env'),
104 appPath: resolveApp('.'),
105 appBuild: resolveOwn('../../build'),
106 appPublic: resolveOwn('template/public'),
107 appHtml: resolveOwn('template/public/index.html'),
108 appIndexJs: resolveOwn('template/src/index.js'),
109 appPackageJson: resolveOwn('package.json'),
110 appSrc: resolveOwn('template/src'),
111 yarnLockFile: resolveOwn('template/yarn.lock'),
112 testsSetup: resolveOwn('template/src/setupTests.js'),
113 proxySetup: resolveOwn('template/src/setupProxy.js'),
114 appNodeModules: resolveOwn('node_modules'),
115 publicUrl: getPublicUrl(resolveOwn('package.json')),
116 servedPath: getServedPath(resolveOwn('package.json')),
117 // These properties only exist before ejecting:
118 ownPath: resolveOwn('.'),
119 ownNodeModules: resolveOwn('node_modules'),
120 };
121}
122// @remove-on-eject-end