|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('path'); |
| 4 | +var fs = require('fs'); |
| 5 | +var url = require('url'); |
| 6 | + |
| 7 | +// Make sure any symlinks in the project folder are resolved: |
| 8 | +// https://github.com/facebookincubator/create-react-app/issues/637 |
| 9 | +var appDirectory = fs.realpathSync(process.cwd()); |
| 10 | +function resolveApp(relativePath) { |
| 11 | + return path.resolve(appDirectory, relativePath); |
| 12 | +} |
| 13 | + |
| 14 | +// We support resolving modules according to `NODE_PATH`. |
| 15 | +// This lets you use absolute paths in imports inside large monorepos: |
| 16 | +// https://github.com/facebookincubator/create-react-app/issues/253. |
| 17 | + |
| 18 | +// It works similar to `NODE_PATH` in Node itself: |
| 19 | +// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders |
| 20 | + |
| 21 | +// We will export `nodePaths` as an array of absolute paths. |
| 22 | +// It will then be used by Webpack configs. |
| 23 | +// Jest doesn’t need this because it already handles `NODE_PATH` out of the box. |
| 24 | + |
| 25 | +// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored. |
| 26 | +// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims. |
| 27 | +// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421 |
| 28 | + |
| 29 | +var nodePaths = (process.env.NODE_PATH || '') |
| 30 | + .split(process.platform === 'win32' ? ';' : ':') |
| 31 | + .filter(Boolean) |
| 32 | + .filter(folder => !path.isAbsolute(folder)) |
| 33 | + .map(resolveApp); |
| 34 | + |
| 35 | +var envPublicUrl = process.env.PUBLIC_URL; |
| 36 | + |
| 37 | +function ensureSlash(path, needsSlash) { |
| 38 | + var hasSlash = path.endsWith('/'); |
| 39 | + if (hasSlash && !needsSlash) { |
| 40 | + return path.substr(path, path.length - 1); |
| 41 | + } else if (!hasSlash && needsSlash) { |
| 42 | + return path + '/'; |
| 43 | + } else { |
| 44 | + return path; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function getPublicUrl(appPackageJson) { |
| 49 | + return envPublicUrl || require(appPackageJson).homepage; |
| 50 | +} |
| 51 | + |
| 52 | +// We use `PUBLIC_URL` environment variable or "homepage" field to infer |
| 53 | +// "public path" at which the app is served. |
| 54 | +// Webpack needs to know it to put the right <script> hrefs into HTML even in |
| 55 | +// single-page apps that may serve index.html for nested URLs like /todos/42. |
| 56 | +// We can't use a relative path in HTML because we don't want to load something |
| 57 | +// like /todos/42/static/js/bundle.7289d.js. We have to know the root. |
| 58 | +function getServedPath(appPackageJson) { |
| 59 | + var publicUrl = getPublicUrl(appPackageJson); |
| 60 | + var servedUrl = envPublicUrl || ( |
| 61 | + publicUrl ? url.parse(publicUrl).pathname : '/' |
| 62 | + ); |
| 63 | + return ensureSlash(servedUrl, true); |
| 64 | +} |
| 65 | + |
| 66 | +// config after eject: we're in ./config/ |
| 67 | +module.exports = { |
| 68 | + appBuild: resolveApp('build'), |
| 69 | + appPublic: resolveApp('public'), |
| 70 | + appHtml: resolveApp('public/index.html'), |
| 71 | + appIndexJs: resolveApp('src/index.js'), |
| 72 | + appPackageJson: resolveApp('package.json'), |
| 73 | + appSrc: resolveApp('src'), |
| 74 | + yarnLockFile: resolveApp('yarn.lock'), |
| 75 | + testsSetup: resolveApp('src/setupTests.js'), |
| 76 | + appNodeModules: resolveApp('node_modules'), |
| 77 | + nodePaths: nodePaths, |
| 78 | + publicUrl: getPublicUrl(resolveApp('package.json')), |
| 79 | + servedPath: getServedPath(resolveApp('package.json')), |
| 80 | + |
| 81 | + serverIndexJs: resolveApp('src/node_modules/server/server.js'), |
| 82 | +}; |
0 commit comments