paths.js 4.25 KB
Newer Older
1
// @remove-on-eject-begin
2
3
4
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
5
6
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
7
 */
8
// @remove-on-eject-end
9
'use strict';
10

11
12
13
const path = require('path');
const fs = require('fs');
const url = require('url');
14
15
16

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

20
const envPublicUrl = process.env.PUBLIC_URL;
21
22

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

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

// 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) {
43
  const publicUrl = getPublicUrl(appPackageJson);
44
45
  const servedUrl =
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
46
47
48
  return ensureSlash(servedUrl, true);
}

49
50
// config after eject: we're in ./config/
module.exports = {
51
  dotenv: resolveApp('.env'),
52
  appPath: resolveApp('.'),
53
  appBuild: resolveApp('build'),
54
55
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
56
  appIndexJs: resolveApp('src/index.js'),
57
58
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
59
  yarnLockFile: resolveApp('yarn.lock'),
60
  testsSetup: resolveApp('src/setupTests.js'),
61
  appNodeModules: resolveApp('node_modules'),
62
  publicUrl: getPublicUrl(resolveApp('package.json')),
63
  servedPath: getServedPath(resolveApp('package.json')),
64
};
65

66
// @remove-on-eject-begin
67
const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
68

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

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

95
// config before publish: we're in ./packages/react-scripts/config/
96
97
98
99
if (
  !reactScriptsLinked &&
  __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
) {
100
  module.exports = {
101
    dotenv: resolveOwn('template/.env'),
102
103
104
105
106
107
108
109
110
111
112
    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')),
113
114
115
116
    servedPath: getServedPath(resolveOwn('package.json')),
    // These properties only exist before ejecting:
    ownPath: resolveOwn('.'),
    ownNodeModules: resolveOwn('node_modules'),
117
118
  };
}
119
// @remove-on-eject-end