paths.js 5.4 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
21
22
function resolveApp(relativePath) {
  return path.resolve(appDirectory, relativePath);
}
23

Dan Abramov's avatar
Dan Abramov committed
24
// We support resolving modules according to `NODE_PATH`.
25
26
// This lets you use absolute paths in imports inside large monorepos:
// https://github.com/facebookincubator/create-react-app/issues/253.
Dan Abramov's avatar
Dan Abramov committed
27
28

// It works similar to `NODE_PATH` in Node itself:
29
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
Dan Abramov's avatar
Dan Abramov committed
30

31
// We will export `nodePaths` as an array of absolute paths.
Dan Abramov's avatar
Dan Abramov committed
32
33
34
// It will then be used by Webpack configs.
// Jest doesn’t need this because it already handles `NODE_PATH` out of the box.

35
36
37
38
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421

39
const nodePaths = (process.env.NODE_PATH || '')
40
41
  .split(process.platform === 'win32' ? ';' : ':')
  .filter(Boolean)
42
  .filter(folder => !path.isAbsolute(folder))
43
  .map(resolveApp);
44

45
const envPublicUrl = process.env.PUBLIC_URL;
46
47

function ensureSlash(path, needsSlash) {
48
  const hasSlash = path.endsWith('/');
49
50
51
  if (hasSlash && !needsSlash) {
    return path.substr(path, path.length - 1);
  } else if (!hasSlash && needsSlash) {
52
    return `${path}/`;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  } else {
    return path;
  }
}

function getPublicUrl(appPackageJson) {
  return envPublicUrl || require(appPackageJson).homepage;
}

// 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) {
69
70
71
  const publicUrl = getPublicUrl(appPackageJson);
  const servedUrl = envPublicUrl ||
    (publicUrl ? url.parse(publicUrl).pathname : '/');
72
73
74
  return ensureSlash(servedUrl, true);
}

75
76
// config after eject: we're in ./config/
module.exports = {
77
  dotenv: resolveApp('.env'),
78
  appBuild: resolveApp('build'),
79
80
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
81
  appIndexJs: resolveApp('src/index.js'),
82
83
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
84
  yarnLockFile: resolveApp('yarn.lock'),
85
  testsSetup: resolveApp('src/setupTests.js'),
86
  appNodeModules: resolveApp('node_modules'),
87
88
  nodePaths: nodePaths,
  publicUrl: getPublicUrl(resolveApp('package.json')),
89
  servedPath: getServedPath(resolveApp('package.json')),
90
};
91

92
// @remove-on-eject-begin
93
function resolveOwn(relativePath) {
94
  return path.resolve(__dirname, '..', relativePath);
95
}
96

97
98
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
99
  dotenv: resolveApp('.env'),
100
  appPath: resolveApp('.'),
101
  appBuild: resolveApp('build'),
102
103
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
104
  appIndexJs: resolveApp('src/index.js'),
105
106
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
107
  yarnLockFile: resolveApp('yarn.lock'),
108
  testsSetup: resolveApp('src/setupTests.js'),
109
  appNodeModules: resolveApp('node_modules'),
110
111
  nodePaths: nodePaths,
  publicUrl: getPublicUrl(resolveApp('package.json')),
112
113
114
115
  servedPath: getServedPath(resolveApp('package.json')),
  // These properties only exist before ejecting:
  ownPath: resolveOwn('.'),
  ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
116
};
117

118
119
120
121
const ownPackageJson = require('../package.json');
const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
const reactScriptsLinked = fs.existsSync(reactScriptsPath) &&
  fs.lstatSync(reactScriptsPath).isSymbolicLink();
122

123
// config before publish: we're in ./packages/react-scripts/config/
124
125
126
127
if (
  !reactScriptsLinked &&
  __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
) {
128
  module.exports = {
129
    dotenv: resolveOwn('template/.env'),
130
131
132
133
134
135
136
137
138
139
    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'),
140
    nodePaths: nodePaths,
141
    publicUrl: getPublicUrl(resolveOwn('package.json')),
142
143
144
145
    servedPath: getServedPath(resolveOwn('package.json')),
    // These properties only exist before ejecting:
    ownPath: resolveOwn('.'),
    ownNodeModules: resolveOwn('node_modules'),
146
147
  };
}
148
// @remove-on-eject-end