paths.js 3.52 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
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
38
39
var nodePaths = (process.env.NODE_PATH || '')
  .split(process.platform === 'win32' ? ';' : ':')
  .filter(Boolean)
40
  .filter(folder => !path.isAbsolute(folder))
41
  .map(resolveApp);
42

43
44
45
// config after eject: we're in ./config/
module.exports = {
  appBuild: resolveApp('build'),
46
47
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
48
  appIndexJs: resolveApp('src/index.js'),
49
50
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
51
  yarnLockFile: resolveApp('yarn.lock'),
52
  testsSetup: resolveApp('src/setupTests.js'),
53
  appNodeModules: resolveApp('node_modules'),
54
55
  ownNodeModules: resolveApp('node_modules'),
  nodePaths: nodePaths
56
};
57

58
// @remove-on-eject-begin
59
function resolveOwn(relativePath) {
60
61
  return path.resolve(__dirname, relativePath);
}
62

63
64
65
// config before eject: we're in ./node_modules/react-scripts/config/
module.exports = {
  appBuild: resolveApp('build'),
66
67
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
68
  appIndexJs: resolveApp('src/index.js'),
69
70
  appPackageJson: resolveApp('package.json'),
  appSrc: resolveApp('src'),
Ville Immonen's avatar
Ville Immonen committed
71
  yarnLockFile: resolveApp('yarn.lock'),
72
  testsSetup: resolveApp('src/setupTests.js'),
73
74
  appNodeModules: resolveApp('node_modules'),
  // this is empty with npm3 but node resolution searches higher anyway:
75
76
  ownNodeModules: resolveOwn('../node_modules'),
  nodePaths: nodePaths
77
};
78

79
80
81
82
83
84
85
86
87
// config before publish: we're in ./packages/react-scripts/config/
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
  module.exports = {
    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'),
Ville Immonen's avatar
Ville Immonen committed
88
    yarnLockFile: resolveOwn('../template/yarn.lock'),
89
90
91
92
93
94
    testsSetup: resolveOwn('../template/src/setupTests.js'),
    appNodeModules: resolveOwn('../node_modules'),
    ownNodeModules: resolveOwn('../node_modules'),
    nodePaths: nodePaths
  };
}
95
// @remove-on-eject-end