/
paths.js
73 lines (62 loc) · 2.52 KB
/
paths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path')
const fs = require('fs')
const url = require('url')
const findMonorepo = require('react-dev-utils/workspaceUtils').findMonorepo
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd())
const resolveApp = relativePath => path.resolve(appDirectory, relativePath)
const envPublicUrl = process.env.PUBLIC_URL
function ensureSlash (inputPath, needsSlash) {
const hasSlash = inputPath.endsWith('/')
if (hasSlash && !needsSlash) {
return inputPath.substr(0, inputPath.length - 1)
} else if (!hasSlash && needsSlash) {
return `${inputPath}/`
} else {
return inputPath
}
}
const getPublicUrl = appPackageJson =>
envPublicUrl || require(appPackageJson).homepage
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath (appPackageJson) {
const publicUrl = getPublicUrl(appPackageJson)
const servedUrl =
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/')
return ensureSlash(servedUrl, true)
}
// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
appPath: resolveApp('.'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveApp('../common/index.js'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('../.'),
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json'))
}
let checkForMonorepo = true
module.exports.srcPaths = [module.exports.appSrc + '/common']
module.exports.useYarn = fs.existsSync(
path.join(module.exports.appPath, 'yarn.lock')
)
if (checkForMonorepo) {
// if app is in a monorepo (lerna or yarn workspace), treat other packages in
// the monorepo as if they are app source
const mono = findMonorepo(appDirectory)
if (mono.isAppIncluded) {
Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs)
}
module.exports.useYarn = module.exports.useYarn || mono.isYarnWs
}