paths.js 2.69 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
12

var path = require('path');
13
14
15
16
17
18
19
20
var fs = require('fs');

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
var appDirectory = fs.realpathSync(process.cwd());
function resolveApp(relativePath) {
  return 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
var nodePaths = (process.env.NODE_PATH || '')
  .split(process.platform === 'win32' ? ';' : ':')
  .filter(Boolean)
36
  .map(resolveApp);
37

38
39
40
41
42
43
// config after eject: we're in ./config/
module.exports = {
  appBuild: resolveApp('build'),
  appHtml: resolveApp('index.html'),
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
44
  testsSetup: resolveApp('src/setupTests.js'),
45
  appNodeModules: resolveApp('node_modules'),
46
47
  ownNodeModules: resolveApp('node_modules'),
  nodePaths: nodePaths
48
};
49

50
// @remove-on-eject-begin
51
function resolveOwn(relativePath) {
52
53
  return path.resolve(__dirname, relativePath);
}
54

55
56
57
58
59
60
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
  appBuild: resolveApp('build'),
  appHtml: resolveApp('index.html'),
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
61
  testsSetup: resolveApp('src/setupTests.js'),
62
63
  appNodeModules: resolveApp('node_modules'),
  // this is empty with npm3 but node resolution searches higher anyway:
64
65
  ownNodeModules: resolveOwn('../node_modules'),
  nodePaths: nodePaths
66
67
};
// @remove-on-eject-end
68

69
70
71
72
73
74
// @remove-on-publish-begin
module.exports = {
  appBuild: resolveOwn('../build'),
  appHtml: resolveOwn('../template/index.html'),
  appPackageJson: resolveOwn('../package.json'),
  appSrc: resolveOwn('../template/src'),
75
  testsSetup: resolveOwn('../template/src/setupTests.js'),
76
  appNodeModules: resolveOwn('../node_modules'),
77
78
  ownNodeModules: resolveOwn('../node_modules'),
  nodePaths: nodePaths
79
80
};
// @remove-on-publish-end