|
| 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 | + |
| 11 | +const path = require('path'); |
| 12 | +const fs = require('fs'); |
| 13 | +const url = require('url'); |
| 14 | + |
| 15 | +// Make sure any symlinks in the project folder are resolved: |
| 16 | +// https://github.com/facebookincubator/create-react-app/issues/637 |
| 17 | +const appDirectory = fs.realpathSync(process.cwd()); |
| 18 | +const resolveApp = relativePath => path.resolve(appDirectory, relativePath); |
| 19 | + |
| 20 | +const envPublicUrl = process.env.PUBLIC_URL; |
| 21 | + |
| 22 | +function ensureSlash(path, needsSlash) { |
| 23 | + const hasSlash = path.endsWith('/'); |
| 24 | + if (hasSlash && !needsSlash) { |
| 25 | + return path.substr(path, path.length - 1); |
| 26 | + } else if (!hasSlash && needsSlash) { |
| 27 | + return `${path}/`; |
| 28 | + } else { |
| 29 | + return path; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const getPublicUrl = appPackageJson => |
| 34 | + envPublicUrl || require(appPackageJson).homepage; |
| 35 | + |
| 36 | +// We use `PUBLIC_URL` environment variable or "homepage" field to infer |
| 37 | +// "public path" at which the app is served. |
| 38 | +// Webpack needs to know it to put the right <script> hrefs into HTML even in |
| 39 | +// single-page apps that may serve index.html for nested URLs like /todos/42. |
| 40 | +// We can't use a relative path in HTML because we don't want to load something |
| 41 | +// like /todos/42/static/js/bundle.7289d.js. We have to know the root. |
| 42 | +function getServedPath(appPackageJson) { |
| 43 | + const publicUrl = getPublicUrl(appPackageJson); |
| 44 | + const servedUrl = |
| 45 | + envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/'); |
| 46 | + return ensureSlash(servedUrl, true); |
| 47 | +} |
| 48 | + |
| 49 | +// config after eject: we're in ./config/ |
| 50 | +module.exports = { |
| 51 | + dotenv: resolveApp('.env'), |
| 52 | + appBuild: resolveApp('build'), |
| 53 | + appPublic: resolveApp('public'), |
| 54 | + appHtml: resolveApp('public/index.html'), |
| 55 | + appIndexJs: resolveApp('src/index.js'), |
| 56 | + appPackageJson: resolveApp('package.json'), |
| 57 | + appSrc: resolveApp('src'), |
| 58 | + yarnLockFile: resolveApp('yarn.lock'), |
| 59 | + testsSetup: resolveApp('src/setupTests.js'), |
| 60 | + appNodeModules: resolveApp('node_modules'), |
| 61 | + publicUrl: getPublicUrl(resolveApp('package.json')), |
| 62 | + servedPath: getServedPath(resolveApp('package.json')), |
| 63 | +}; |
| 64 | + |
| 65 | +// @remove-on-eject-begin |
| 66 | +const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath); |
| 67 | + |
| 68 | +// config before eject: we're in ./node_modules/react-scripts/config/ |
| 69 | +module.exports = { |
| 70 | + dotenv: resolveApp('.env'), |
| 71 | + appPath: resolveApp('.'), |
| 72 | + appBuild: resolveApp('build'), |
| 73 | + appPublic: resolveApp('public'), |
| 74 | + appHtml: resolveApp('public/index.html'), |
| 75 | + appIndexJs: resolveApp('src/index.js'), |
| 76 | + appPackageJson: resolveApp('package.json'), |
| 77 | + appSrc: resolveApp('src'), |
| 78 | + yarnLockFile: resolveApp('yarn.lock'), |
| 79 | + testsSetup: resolveApp('src/setupTests.js'), |
| 80 | + appNodeModules: resolveApp('node_modules'), |
| 81 | + publicUrl: getPublicUrl(resolveApp('package.json')), |
| 82 | + servedPath: getServedPath(resolveApp('package.json')), |
| 83 | + // These properties only exist before ejecting: |
| 84 | + ownPath: resolveOwn('.'), |
| 85 | + ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3 |
| 86 | +}; |
| 87 | + |
| 88 | +const ownPackageJson = require('../package.json'); |
| 89 | +const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`); |
| 90 | +const reactScriptsLinked = |
| 91 | + fs.existsSync(reactScriptsPath) && |
| 92 | + fs.lstatSync(reactScriptsPath).isSymbolicLink(); |
| 93 | + |
| 94 | +// config before publish: we're in ./packages/react-scripts/config/ |
| 95 | +if ( |
| 96 | + !reactScriptsLinked && |
| 97 | + __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1 |
| 98 | +) { |
| 99 | + module.exports = { |
| 100 | + dotenv: resolveOwn('template/.env'), |
| 101 | + appPath: resolveApp('.'), |
| 102 | + appBuild: resolveOwn('../../build'), |
| 103 | + appPublic: resolveOwn('template/public'), |
| 104 | + appHtml: resolveOwn('template/public/index.html'), |
| 105 | + appIndexJs: resolveOwn('template/src/index.js'), |
| 106 | + appPackageJson: resolveOwn('package.json'), |
| 107 | + appSrc: resolveOwn('template/src'), |
| 108 | + yarnLockFile: resolveOwn('template/yarn.lock'), |
| 109 | + testsSetup: resolveOwn('template/src/setupTests.js'), |
| 110 | + appNodeModules: resolveOwn('node_modules'), |
| 111 | + publicUrl: getPublicUrl(resolveOwn('package.json')), |
| 112 | + servedPath: getServedPath(resolveOwn('package.json')), |
| 113 | + // These properties only exist before ejecting: |
| 114 | + ownPath: resolveOwn('.'), |
| 115 | + ownNodeModules: resolveOwn('node_modules'), |
| 116 | + }; |
| 117 | +} |
| 118 | +// @remove-on-eject-end |
0 commit comments