UNPKG

5.39 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10// @remove-on-eject-end
11'use strict';
12
13var path = require('path');
14var fs = require('fs');
15var url = require('url');
16
17// Make sure any symlinks in the project folder are resolved:
18// https://github.com/facebookincubator/create-react-app/issues/637
19var appDirectory = fs.realpathSync(process.cwd());
20function resolveApp(relativePath) {
21 return path.resolve(appDirectory, relativePath);
22}
23
24// We support resolving modules according to `NODE_PATH`.
25// This lets you use absolute paths in imports inside large monorepos:
26// https://github.com/facebookincubator/create-react-app/issues/253.
27
28// It works similar to `NODE_PATH` in Node itself:
29// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
30
31// We will export `nodePaths` as an array of absolute paths.
32// It will then be used by Webpack configs.
33// Jest doesn’t need this because it already handles `NODE_PATH` out of the box.
34
35// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
36// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
37// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
38
39var nodePaths = (process.env.NODE_PATH || '')
40 .split(process.platform === 'win32' ? ';' : ':')
41 .filter(Boolean)
42 .filter(folder => !path.isAbsolute(folder))
43 .map(resolveApp);
44
45var envPublicUrl = process.env.PUBLIC_URL;
46
47function ensureSlash(path, needsSlash) {
48 var hasSlash = path.endsWith('/');
49 if (hasSlash && !needsSlash) {
50 return path.substr(path, path.length - 1);
51 } else if (!hasSlash && needsSlash) {
52 return path + '/';
53 } else {
54 return path;
55 }
56}
57
58function getPublicUrl(appPackageJson) {
59 return envPublicUrl || require(appPackageJson).homepage;
60}
61
62// We use `PUBLIC_URL` environment variable or "homepage" field to infer
63// "public path" at which the app is served.
64// Webpack needs to know it to put the right <script> hrefs into HTML even in
65// single-page apps that may serve index.html for nested URLs like /todos/42.
66// We can't use a relative path in HTML because we don't want to load something
67// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
68function getServedPath(appPackageJson) {
69 var publicUrl = getPublicUrl(appPackageJson);
70 var servedUrl = envPublicUrl || (
71 publicUrl ? url.parse(publicUrl).pathname : '/'
72 );
73 return ensureSlash(servedUrl, true);
74}
75
76// config after eject: we're in ./config/
77module.exports = {
78 appBuild: resolveApp('build'),
79 appPublic: resolveApp('public'),
80 appHtml: resolveApp('public/index.html'),
81 appIndexJs: resolveApp('src/index.js'),
82 appPackageJson: resolveApp('package.json'),
83 appSrc: resolveApp('src'),
84 yarnLockFile: resolveApp('yarn.lock'),
85 testsSetup: resolveApp('src/setupTests.js'),
86 appNodeModules: resolveApp('node_modules'),
87 nodePaths: nodePaths,
88 publicUrl: getPublicUrl(resolveApp('package.json')),
89 servedPath: getServedPath(resolveApp('package.json'))
90};
91
92// @remove-on-eject-begin
93function resolveOwn(relativePath) {
94 return path.resolve(__dirname, '..', relativePath);
95}
96
97// config before eject: we're in ./node_modules/react-scripts/config/
98module.exports = {
99 appPath: resolveApp('.'),
100 appBuild: resolveApp('build'),
101 appPublic: resolveApp('public'),
102 appHtml: resolveApp('public/index.html'),
103 appIndexJs: resolveApp('src/index.js'),
104 appPackageJson: resolveApp('package.json'),
105 appSrc: resolveApp('src'),
106 yarnLockFile: resolveApp('yarn.lock'),
107 testsSetup: resolveApp('src/setupTests.js'),
108 appNodeModules: resolveApp('node_modules'),
109 nodePaths: nodePaths,
110 publicUrl: getPublicUrl(resolveApp('package.json')),
111 servedPath: getServedPath(resolveApp('package.json')),
112 // These properties only exist before ejecting:
113 ownPath: resolveOwn('.'),
114 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
115};
116
117var ownPackageJson = require('../package.json');
118var reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
119var reactScriptsLinked = fs.existsSync(reactScriptsPath) && fs.lstatSync(reactScriptsPath).isSymbolicLink();
120
121// config before publish: we're in ./packages/react-scripts/config/
122if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
123 module.exports = {
124 appPath: resolveApp('.'),
125 appBuild: resolveOwn('../../build'),
126 appPublic: resolveOwn('template/public'),
127 appHtml: resolveOwn('template/public/index.html'),
128 appIndexJs: resolveOwn('template/src/index.js'),
129 appPackageJson: resolveOwn('package.json'),
130 appSrc: resolveOwn('template/src'),
131 yarnLockFile: resolveOwn('template/yarn.lock'),
132 testsSetup: resolveOwn('template/src/setupTests.js'),
133 appNodeModules: resolveOwn('node_modules'),
134 nodePaths: nodePaths,
135 publicUrl: getPublicUrl(resolveOwn('package.json')),
136 servedPath: getServedPath(resolveOwn('package.json')),
137 // These properties only exist before ejecting:
138 ownPath: resolveOwn('.'),
139 ownNodeModules: resolveOwn('node_modules'),
140 };
141}
142// @remove-on-eject-end