UNPKG

5.42 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
13const path = require('path');
14const fs = require('fs');
15const 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
19const 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
39const nodePaths = (process.env.NODE_PATH || '')
40 .split(process.platform === 'win32' ? ';' : ':')
41 .filter(Boolean)
42 .filter(folder => !path.isAbsolute(folder))
43 .map(resolveApp);
44
45const envPublicUrl = process.env.PUBLIC_URL;
46
47function ensureSlash(path, needsSlash) {
48 const 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 const publicUrl = getPublicUrl(appPackageJson);
70 const servedUrl = envPublicUrl ||
71 (publicUrl ? url.parse(publicUrl).pathname : '/');
72 return ensureSlash(servedUrl, true);
73}
74
75// config after eject: we're in ./config/
76module.exports = {
77 appBuild: resolveApp('build'),
78 appPublic: resolveApp('public'),
79 appHtml: resolveApp('public/index.html'),
80 appIndexJs: resolveApp('src/index.js'),
81 appPackageJson: resolveApp('package.json'),
82 appSrc: resolveApp('src'),
83 yarnLockFile: resolveApp('yarn.lock'),
84 testsSetup: resolveApp('src/setupTests.js'),
85 appNodeModules: resolveApp('node_modules'),
86 nodePaths: nodePaths,
87 publicUrl: getPublicUrl(resolveApp('package.json')),
88 servedPath: getServedPath(resolveApp('package.json')),
89};
90
91// @remove-on-eject-begin
92function resolveOwn(relativePath) {
93 return path.resolve(__dirname, '..', relativePath);
94}
95
96// config before eject: we're in ./node_modules/react-scripts/config/
97module.exports = {
98 appPath: resolveApp('.'),
99 appBuild: resolveApp('build'),
100 appPublic: resolveApp('public'),
101 appHtml: resolveApp('public/index.html'),
102 appIndexJs: resolveApp('src/index.js'),
103 appPackageJson: resolveApp('package.json'),
104 appSrc: resolveApp('src'),
105 yarnLockFile: resolveApp('yarn.lock'),
106 testsSetup: resolveApp('src/setupTests.js'),
107 appNodeModules: resolveApp('node_modules'),
108 nodePaths: nodePaths,
109 publicUrl: getPublicUrl(resolveApp('package.json')),
110 servedPath: getServedPath(resolveApp('package.json')),
111 // These properties only exist before ejecting:
112 ownPath: resolveOwn('.'),
113 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
114};
115
116const ownPackageJson = require('../package.json');
117const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
118const reactScriptsLinked = fs.existsSync(reactScriptsPath) &&
119 fs.lstatSync(reactScriptsPath).isSymbolicLink();
120
121// config before publish: we're in ./packages/react-scripts/config/
122if (
123 !reactScriptsLinked &&
124 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
125) {
126 module.exports = {
127 appPath: resolveApp('.'),
128 appBuild: resolveOwn('../../build'),
129 appPublic: resolveOwn('template/public'),
130 appHtml: resolveOwn('template/public/index.html'),
131 appIndexJs: resolveOwn('template/src/index.js'),
132 appPackageJson: resolveOwn('package.json'),
133 appSrc: resolveOwn('template/src'),
134 yarnLockFile: resolveOwn('template/yarn.lock'),
135 testsSetup: resolveOwn('template/src/setupTests.js'),
136 appNodeModules: resolveOwn('node_modules'),
137 nodePaths: nodePaths,
138 publicUrl: getPublicUrl(resolveOwn('package.json')),
139 servedPath: getServedPath(resolveOwn('package.json')),
140 // These properties only exist before ejecting:
141 ownPath: resolveOwn('.'),
142 ownNodeModules: resolveOwn('node_modules'),
143 };
144}
145// @remove-on-eject-end