paths.js 4.35 KB
Newer Older
1
// @remove-on-eject-begin
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
10
// @remove-on-eject-end
11
'use strict';
12

13
14
15
const path = require('path');
const fs = require('fs');
const url = require('url');
16
17
18

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
19
const appDirectory = fs.realpathSync(process.cwd());
20
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
21

22
const envPublicUrl = process.env.PUBLIC_URL;
23
24

function ensureSlash(path, needsSlash) {
25
  const hasSlash = path.endsWith('/');
26
27
28
  if (hasSlash && !needsSlash) {
    return path.substr(path, path.length - 1);
  } else if (!hasSlash && needsSlash) {
29
    return `${path}/`;
30
31
32
33
34
  } else {
    return path;
  }
}

35
36
const getPublicUrl = appPackageJson =>
  envPublicUrl || require(appPackageJson).homepage;
37
38
39
40
41
42
43
44

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
45
  const publicUrl = getPublicUrl(appPackageJson);
46
47
  const servedUrl =
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
48
49
50
  return ensureSlash(servedUrl, true);
}

51
52
// config after eject: we're in ./config/
module.exports = {
53
  dotenv: resolveApp('.env'),
54
  appBuild: resolveApp('build'),
55
56
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
57
  appIndexJs: resolveApp('src/index.js'),
58
59
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
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
68
const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
69

70
71
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
72
  dotenv: resolveApp('.env'),
73
  appPath: resolveApp('.'),
74
  appBuild: resolveApp('build'),
75
76
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
77
  appIndexJs: resolveApp('src/index.js'),
78
79
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
80
  yarnLockFile: resolveApp('yarn.lock'),
81
  testsSetup: resolveApp('src/setupTests.js'),
82
  appNodeModules: resolveApp('node_modules'),
83
  publicUrl: getPublicUrl(resolveApp('package.json')),
84
85
86
87
  servedPath: getServedPath(resolveApp('package.json')),
  // These properties only exist before ejecting:
  ownPath: resolveOwn('.'),
  ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
88
};
89

90
91
const ownPackageJson = require('../package.json');
const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
92
93
const reactScriptsLinked =
  fs.existsSync(reactScriptsPath) &&
94
  fs.lstatSync(reactScriptsPath).isSymbolicLink();
95

96
// config before publish: we're in ./packages/react-scripts/config/
97
98
99
100
if (
  !reactScriptsLinked &&
  __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
) {
101
  module.exports = {
102
    dotenv: resolveOwn('template/.env'),
103
104
105
106
107
108
109
110
111
112
113
    appPath: resolveApp('.'),
    appBuild: resolveOwn('../../build'),
    appPublic: resolveOwn('template/public'),
    appHtml: resolveOwn('template/public/index.html'),
    appIndexJs: resolveOwn('template/src/index.js'),
    appPackageJson: resolveOwn('package.json'),
    appSrc: resolveOwn('template/src'),
    yarnLockFile: resolveOwn('template/yarn.lock'),
    testsSetup: resolveOwn('template/src/setupTests.js'),
    appNodeModules: resolveOwn('node_modules'),
    publicUrl: getPublicUrl(resolveOwn('package.json')),
114
115
116
117
    servedPath: getServedPath(resolveOwn('package.json')),
    // These properties only exist before ejecting:
    ownPath: resolveOwn('.'),
    ownNodeModules: resolveOwn('node_modules'),
118
119
  };
}
120
// @remove-on-eject-end