UNPKG

6.05 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 oem = require('./oem');
15
16// Make sure any symlinks in the project folder are resolved:
17// https://github.com/facebook/create-react-app/issues/637
18const appDirectory = fs.realpathSync(process.cwd());
19const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
20
21const envPublicUrl = process.env.PUBLIC_URL;
22
23function ensureSlash(inputPath, needsSlash) {
24 const hasSlash = inputPath.endsWith('/');
25 if (hasSlash && !needsSlash) {
26 return inputPath.substr(0, inputPath.length - 1);
27 } else if (!hasSlash && needsSlash) {
28 return `${inputPath}/`;
29 } else {
30 return inputPath;
31 }
32}
33
34const getPublicUrl = appPackageJson =>
35 envPublicUrl || require(appPackageJson).homepage;
36
37// We use `PUBLIC_URL` environment variable or "homepage" field to infer
38// "public path" at which the app is served.
39// Webpack needs to know it to put the right <script> hrefs into HTML even in
40// single-page apps that may serve index.html for nested URLs like /todos/42.
41// We can't use a relative path in HTML because we don't want to load something
42// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
43function getServedPath(appPackageJson) {
44 const publicUrl = getPublicUrl(appPackageJson);
45 const servedUrl =
46 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
47 return ensureSlash(servedUrl, true);
48}
49
50const moduleFileExtensions = [
51 'web.mjs',
52 'mjs',
53 'web.js',
54 'js',
55 'web.ts',
56 'ts',
57 'web.tsx',
58 'tsx',
59 'json',
60 'web.jsx',
61 'jsx',
62];
63
64// Resolve file paths in the same order as webpack
65const resolveModule = (resolveFn, filePath) => {
66 const extension = moduleFileExtensions.find(extension =>
67 fs.existsSync(resolveFn(`${filePath}.${extension}`))
68 );
69
70 if (extension) {
71 return resolveFn(`${filePath}.${extension}`);
72 }
73
74 return resolveFn(`${filePath}.js`);
75};
76
77// config after eject: we're in ./config/
78module.exports = {
79 dotenv: resolveApp('.env'),
80 appPath: resolveApp('.'),
81 appBuild: resolveApp(path.join('dist', oem.reactAppOem)),
82 appPublic: resolveApp('public'),
83 appAsset: resolveApp('asset'),
84 appHtml: resolveApp('public/index.html'),
85 errorHtml: resolveApp('asset/error.html'),
86 appIndexJs: resolveModule(resolveApp, 'src/index'),
87 appPackageJson: resolveApp('package.json'),
88 appSrc: resolveApp('src'),
89 appTsConfig: resolveApp('tsconfig.json'),
90 appJsConfig: resolveApp('jsconfig.json'),
91 yarnLockFile: resolveApp('yarn.lock'),
92 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
93 proxySetup: resolveApp('src/setupProxy.js'),
94 appNodeModules: resolveApp('node_modules'),
95 publicUrl: getPublicUrl(resolveApp('package.json')),
96 servedPath: getServedPath(resolveApp('package.json')),
97};
98
99// @remove-on-eject-begin
100const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
101
102// config before eject: we're in ./node_modules/react-scripts/config/
103module.exports = {
104 dotenv: resolveApp('.env'),
105 appPath: resolveApp('.'),
106 appBuild: resolveApp(path.join('dist', oem.reactAppOem)),
107 appPublic: resolveApp('public'),
108 appAsset: resolveApp('asset'),
109 appHtml: resolveApp('public/index.html'),
110 errorHtml: resolveApp('asset/error.html'),
111 appIndexJs: resolveModule(resolveApp, 'src/index'),
112 appPackageJson: resolveApp('package.json'),
113 appSrc: resolveApp('src'),
114 appTsConfig: resolveApp('tsconfig.json'),
115 appJsConfig: resolveApp('jsconfig.json'),
116 yarnLockFile: resolveApp('yarn.lock'),
117 testsSetup: resolveModule(resolveApp, 'src/setupTests'),
118 proxySetup: resolveApp('src/setupProxy.js'),
119 appNodeModules: resolveApp('node_modules'),
120 publicUrl: getPublicUrl(resolveApp('package.json')),
121 servedPath: getServedPath(resolveApp('package.json')),
122 // These properties only exist before ejecting:
123 ownPath: resolveOwn('.'),
124 ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
125 appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
126 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
127};
128
129const ownPackageJson = require('../package.json');
130const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
131const reactScriptsLinked =
132 fs.existsSync(reactScriptsPath) &&
133 fs.lstatSync(reactScriptsPath).isSymbolicLink();
134
135// config before publish: we're in ./packages/react-scripts/config/
136if (
137 !reactScriptsLinked &&
138 __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
139) {
140 module.exports = {
141 dotenv: resolveOwn('template/.env'),
142 appPath: resolveApp('.'),
143 appBuild: resolveApp(path.join('../../dist', oem.reactAppOem)),
144 appPublic: resolveOwn('template/public'),
145 appAsset: resolveOwn('template/asset'),
146 appHtml: resolveOwn('template/public/index.html'),
147 errorHtml: resolveOwn('template/asset/error.html'),
148 appIndexJs: resolveModule(resolveOwn, 'template/src/index'),
149 appPackageJson: resolveOwn('package.json'),
150 appSrc: resolveOwn('template/src'),
151 appTsConfig: resolveOwn('template/tsconfig.json'),
152 appJsConfig: resolveOwn('template/jsconfig.json'),
153 yarnLockFile: resolveOwn('template/yarn.lock'),
154 testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
155 proxySetup: resolveOwn('template/src/setupProxy.js'),
156 appNodeModules: resolveOwn('node_modules'),
157 publicUrl: getPublicUrl(resolveOwn('package.json')),
158 servedPath: getServedPath(resolveOwn('package.json')),
159 // These properties only exist before ejecting:
160 ownPath: resolveOwn('.'),
161 ownNodeModules: resolveOwn('node_modules'),
162 appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
163 ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
164 };
165}
166// @remove-on-eject-end
167
168module.exports.moduleFileExtensions = moduleFileExtensions;