UNPKG

4.75 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8// @remove-on-eject-end
9'use strict';
10
11const path = require('path');
12const fs = require('fs');
13const url = require('url');
14const findMonorepo = require('react-dev-utils-fresh/workspaceUtils')
15 .findMonorepo;
16
17// Make sure any symlinks in the project folder are resolved:
18// https://github.com/facebook/create-react-app/issues/637
19const appDirectory = fs.realpathSync(process.cwd());
20const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
21
22const envPublicUrl = process.env.PUBLIC_URL;
23
24function ensureSlash(path, needsSlash) {
25 const hasSlash = path.endsWith('/');
26 if (hasSlash && !needsSlash) {
27 return path.substr(path, path.length - 1);
28 } else if (!hasSlash && needsSlash) {
29 return `${path}/`;
30 } else {
31 return path;
32 }
33}
34
35const getPublicUrl = appPackageJson =>
36 envPublicUrl || require(appPackageJson).homepage;
37
38// We use `PUBLIC_URL` environment variable or "homepage" field to infer
39// "public path" at which the app is served.
40// Webpack needs to know it to put the right <script> hrefs into HTML even in
41// single-page apps that may serve index.html for nested URLs like /todos/42.
42// We can't use a relative path in HTML because we don't want to load something
43// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
44function getServedPath(appPackageJson) {
45 const publicUrl = getPublicUrl(appPackageJson);
46 const servedUrl =
47 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
48 return ensureSlash(servedUrl, true);
49}
50
51// config after eject: we're in ./config/
52module.exports = {
53 dotenv: resolveApp('.env'),
54 appPath: resolveApp('.'),
55 appBuild: resolveApp('build'),
56 appPublic: resolveApp('public'),
57 appHtml: resolveApp('public/index.html'),
58 appIndexJs: resolveApp('src/index.js'),
59 appPackageJson: resolveApp('package.json'),
60 appSrc: resolveApp('src'),
61 testsSetup: resolveApp('src/setupTests.js'),
62 appNodeModules: resolveApp('node_modules'),
63 publicUrl: getPublicUrl(resolveApp('package.json')),
64 servedPath: getServedPath(resolveApp('package.json')),
65};
66
67let checkForMonorepo = true;
68
69// @remove-on-eject-begin
70const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
71
72// config before eject: we're in ./node_modules/react-scripts/config/
73module.exports = {
74 dotenv: resolveApp('.env'),
75 appPath: resolveApp('.'),
76 appBuild: resolveApp('build'),
77 appPublic: resolveApp('public'),
78 appHtml: resolveApp('public/index.html'),
79 appIndexJs: resolveApp('src/index.js'),
80 appPackageJson: resolveApp('package.json'),
81 appSrc: resolveApp('src'),
82 testsSetup: resolveApp('src/setupTests.js'),
83 appNodeModules: resolveApp('node_modules'),
84 publicUrl: getPublicUrl(resolveApp('package.json')),
85 servedPath: getServedPath(resolveApp('package.json')),
86 // These properties only exist before ejecting:
87 ownPath: resolveOwn('.'),
88 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
89};
90
91// detect if template should be used, ie. when cwd is react-scripts itself
92const useTemplate =
93 appDirectory === fs.realpathSync(path.join(__dirname, '..'));
94
95checkForMonorepo = !useTemplate;
96
97if (useTemplate) {
98 module.exports = {
99 dotenv: resolveOwn('template/.env'),
100 appPath: resolveApp('.'),
101 appBuild: resolveOwn('../../build'),
102 appPublic: resolveOwn('template/public'),
103 appHtml: resolveOwn('template/public/index.html'),
104 appIndexJs: resolveOwn('template/src/index.js'),
105 appPackageJson: resolveOwn('package.json'),
106 appSrc: resolveOwn('template/src'),
107 testsSetup: resolveOwn('template/src/setupTests.js'),
108 appNodeModules: resolveOwn('node_modules'),
109 publicUrl: getPublicUrl(resolveOwn('package.json')),
110 servedPath: getServedPath(resolveOwn('package.json')),
111 // These properties only exist before ejecting:
112 ownPath: resolveOwn('.'),
113 ownNodeModules: resolveOwn('node_modules'),
114 };
115}
116// @remove-on-eject-end
117
118module.exports.srcPaths = [module.exports.appSrc];
119
120module.exports.useYarn = fs.existsSync(
121 path.join(module.exports.appPath, 'yarn.lock')
122);
123
124if (checkForMonorepo) {
125 // if app is in a monorepo (lerna or yarn workspace), treat other packages in
126 // the monorepo as if they are app source
127 const mono = findMonorepo(appDirectory);
128 if (mono.isAppIncluded) {
129 Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
130 }
131 module.exports.useYarn = module.exports.useYarn || mono.isYarnWs;
132}