paths.js 4.51 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
const findMonorepo = require('react-dev-utils/workspaceUtils').findMonorepo;
15
16

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

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

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

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

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

50
51
// config after eject: we're in ./config/
module.exports = {
52
  dotenv: resolveApp('.env'),
53
  appPath: resolveApp('.'),
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'),
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
67
let checkForMonorepo = true;

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

71
72
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
73
  dotenv: resolveApp('.env'),
74
  appPath: resolveApp('.'),
75
  appBuild: resolveApp('build'),
76
77
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
78
  appIndexJs: resolveApp('src/index.js'),
79
80
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
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
92
93
94
95
96
// detect if template should be used, ie. when cwd is react-scripts itself
const useTemplate =
  appDirectory === fs.realpathSync(path.join(__dirname, '..'));

checkForMonorepo = !useTemplate;

if (useTemplate) {
97
  module.exports = {
98
    dotenv: resolveOwn('template/.env'),
99
100
101
102
103
104
105
106
107
108
    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'),
    testsSetup: resolveOwn('template/src/setupTests.js'),
    appNodeModules: resolveOwn('node_modules'),
    publicUrl: getPublicUrl(resolveOwn('package.json')),
109
110
111
112
    servedPath: getServedPath(resolveOwn('package.json')),
    // These properties only exist before ejecting:
    ownPath: resolveOwn('.'),
    ownNodeModules: resolveOwn('node_modules'),
113
114
  };
}
115
// @remove-on-eject-end
116
117
118

module.exports.srcPaths = [module.exports.appSrc];

119
120
121
module.exports.useYarn = fs.existsSync(
  path.join(module.exports.appPath, 'yarn.lock')
);
122
123
124
125

if (checkForMonorepo) {
  // if app is in a monorepo (lerna or yarn workspace), treat other packages in
  // the monorepo as if they are app source
126
127
128
129
130
  const mono = findMonorepo(appDirectory);
  if (mono.isAppIncluded) {
    Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
  }
  module.exports.useYarn = module.exports.useYarn || mono.isYarnWs;
131
}