paths.js 5.37 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());
Danil Shashkov's avatar
Danil Shashkov committed
20
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
21

Dan Abramov's avatar
Dan Abramov committed
22
// We support resolving modules according to `NODE_PATH`.
23
24
// 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
25
26

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

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

33
34
35
36
// 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

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

43
const envPublicUrl = process.env.PUBLIC_URL;
44
45

function ensureSlash(path, needsSlash) {
46
  const hasSlash = path.endsWith('/');
47
48
49
  if (hasSlash && !needsSlash) {
    return path.substr(path, path.length - 1);
  } else if (!hasSlash && needsSlash) {
50
    return `${path}/`;
51
52
53
54
55
  } else {
    return path;
  }
}

Danil Shashkov's avatar
Danil Shashkov committed
56
const getPublicUrl = (appPackageJson) => envPublicUrl || require(appPackageJson).homepage;
57
58
59
60
61
62
63
64

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

71
72
// config after eject: we're in ./config/
module.exports = {
73
  dotenv: resolveApp('.env'),
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
84
  nodePaths: nodePaths,
  publicUrl: getPublicUrl(resolveApp('package.json')),
85
  servedPath: getServedPath(resolveApp('package.json')),
86
};
87

88
// @remove-on-eject-begin
Danil Shashkov's avatar
Danil Shashkov committed
89
const resolveOwn = (relativePath) => path.resolve(__dirname, '..', relativePath);
90

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

112
113
114
115
const ownPackageJson = require('../package.json');
const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
const reactScriptsLinked = fs.existsSync(reactScriptsPath) &&
  fs.lstatSync(reactScriptsPath).isSymbolicLink();
116

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