paths.js 5.28 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
const findPkg = require('find-pkg');
const globby = require('globby');
16
17

// Make sure any symlinks in the project folder are resolved:
18
// https://github.com/facebook/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
25
function ensureSlash(inputPath, needsSlash) {
  const hasSlash = inputPath.endsWith('/');
26
  if (hasSlash && !needsSlash) {
27
    return inputPath.substr(0, inputPath.length - 1);
28
  } else if (!hasSlash && needsSlash) {
29
    return `${inputPath}/`;
30
  } else {
31
    return inputPath;
32
33
34
  }
}

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
  appPath: resolveApp('.'),
55
  appBuild: resolveApp('build'),
56
57
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
58
  appIndexJs: resolveApp('src/index.js'),
59
60
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
61
  yarnLockFile: resolveApp('yarn.lock'),
62
  testsSetup: resolveApp('src/setupTests.js'),
63
  appNodeModules: resolveApp('node_modules'),
64
  publicUrl: getPublicUrl(resolveApp('package.json')),
65
  servedPath: getServedPath(resolveApp('package.json')),
66
};
67

68
69
let checkForMonorepo = true;

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

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

93
94
95
96
97
98
99
// 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) {
100
  module.exports = {
101
    dotenv: resolveOwn('template/.env'),
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'),
109
    yarnLockFile: resolveOwn('template/yarn.lock'),
110
111
112
    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
120
121
122

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

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const findPkgs = (rootPath, globPatterns) => {
  const globOpts = {
    cwd: rootPath,
    strict: true,
    absolute: true,
  };
  return globPatterns
    .reduce(
      (pkgs, pattern) =>
        pkgs.concat(globby.sync(path.join(pattern, 'package.json'), globOpts)),
      []
    )
    .map(f => path.dirname(path.normalize(f)));
};

const getMonorepoPkgPaths = () => {
  const monoPkgPath = findPkg.sync(path.resolve(appDirectory, '..'));
  if (monoPkgPath) {
    // get monorepo config from yarn workspace
    const pkgPatterns = require(monoPkgPath).workspaces;
    const pkgPaths = findPkgs(path.dirname(monoPkgPath), pkgPatterns);
    // only include monorepo pkgs if app itself is included in monorepo
    if (pkgPaths.indexOf(appDirectory) !== -1) {
      return pkgPaths.filter(f => fs.realpathSync(f) !== appDirectory);
    }
  }
  return [];
};
151
152
153
154

if (checkForMonorepo) {
  // if app is in a monorepo (lerna or yarn workspace), treat other packages in
  // the monorepo as if they are app source
155
  Array.prototype.push.apply(module.exports.srcPaths, getMonorepoPkgPaths());
156
}