UNPKG

4.5 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10// @remove-on-eject-end
11'use strict';
12
13const path = require('path');
14const fs = require('fs');
15const url = require('url');
16
17// Make sure any symlinks in the project folder are resolved:
18// https://github.com/facebookincubator/create-react-app/issues/637
19const appDirectory = fs.realpathSync(process.cwd());
20const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
21
22const envPublicUrl = process.env.PUBLIC_URL;
23
24function ensureSlash(path, needsSlash) {
25 const hasSlash = path.endsWith('/');
26 if (hasSlash && !needsSlash) {
27 return path.substr(path, path.length - 1);
28 } else if (!hasSlash && needsSlash) {
29 return `${path}/`;
30 } else {
31 return path;
32 }
33}
34
35const getPublicUrl = appPackageJson =>
36 envPublicUrl || require(appPackageJson).homepage;
37
38// We use `PUBLIC_URL` environment variable or "homepage" field to infer
39// "public path" at which the app is served.
40// Webpack needs to know it to put the right <script> hrefs into HTML even in
41// single-page apps that may serve index.html for nested URLs like /todos/42.
42// We can't use a relative path in HTML because we don't want to load something
43// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
44function getServedPath(appPackageJson) {
45 const publicUrl = getPublicUrl(appPackageJson);
46 const servedUrl =
47 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
48 return ensureSlash(servedUrl, true);
49}
50
51// config after eject: we're in ./config/
52module.exports = {
53 dotenv: resolveApp('.env'),
54 appBuild: resolveApp('build'),
55 appPublic: resolveApp('public'),
56 appHtml: resolveApp('public/index.html'),
57 appIndexJs: resolveApp('src/index.js'),
58 appPackageJson: resolveApp('package.json'),
59 appSrc: resolveApp('src'),
60 yarnLockFile: resolveApp('yarn.lock'),
61 testsSetup: resolveApp('src/setupTests.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.tsx'),
78 appPackageJson: resolveApp('package.json'),
79 appTsConfig: resolveApp('tsconfig.json'),
80 appSrc: resolveApp('src'),
81 yarnLockFile: resolveApp('yarn.lock'),
82 testsSetup: resolveApp('src/setupTests.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 appNodeModules: resolveOwn('node_modules'),
114 publicUrl: getPublicUrl(resolveOwn('package.json')),
115 servedPath: getServedPath(resolveOwn('package.json')),
116 // These properties only exist before ejecting:
117 ownPath: resolveOwn('.'),
118 ownNodeModules: resolveOwn('node_modules'),
119 };
120}
121// @remove-on-eject-end