unpkg/config/paths.js

79 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-11-26 05:19:55 +00:00
"use strict"
2017-11-26 05:19:55 +00:00
var path = require("path")
var fs = require("fs")
var url = require("url")
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
2017-11-26 05:19:55 +00:00
var appDirectory = fs.realpathSync(process.cwd())
function resolveApp(relativePath) {
2017-11-26 05:19:55 +00:00
return path.resolve(appDirectory, relativePath)
}
// We support resolving modules according to `NODE_PATH`.
// This lets you use absolute paths in imports inside large monorepos:
// https://github.com/facebookincubator/create-react-app/issues/253.
// It works similar to `NODE_PATH` in Node itself:
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
// We will export `nodePaths` as an array of absolute paths.
// It will then be used by Webpack configs.
// Jest doesnt need this because it already handles `NODE_PATH` out of the box.
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
2017-11-26 05:19:55 +00:00
var nodePaths = (process.env.NODE_PATH || "")
.split(process.platform === "win32" ? ";" : ":")
.filter(Boolean)
.filter(folder => !path.isAbsolute(folder))
2017-11-26 05:19:55 +00:00
.map(resolveApp)
2017-11-26 05:19:55 +00:00
var envPublicUrl = process.env.PUBLIC_URL
function ensureSlash(path, needsSlash) {
2017-11-26 05:19:55 +00:00
var hasSlash = path.endsWith("/")
if (hasSlash && !needsSlash) {
2017-11-26 05:19:55 +00:00
return path.substr(path, path.length - 1)
} else if (!hasSlash && needsSlash) {
2017-11-26 05:19:55 +00:00
return path + "/"
} else {
2017-11-26 05:19:55 +00:00
return path
}
}
function getPublicUrl(appPackageJson) {
2017-11-26 05:19:55 +00:00
return 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) {
2017-11-26 05:19:55 +00:00
var publicUrl = getPublicUrl(appPackageJson)
var servedUrl = envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : "/")
return ensureSlash(servedUrl, true)
}
// config after eject: we're in ./config/
module.exports = {
2017-11-26 05:19:55 +00:00
appBuild: resolveApp("build"),
appPublic: resolveApp("public"),
appHtml: resolveApp("public/index.html"),
appIndexJs: resolveApp("client/index.js"),
appPackageJson: resolveApp("package.json"),
appSrc: resolveApp("client"),
yarnLockFile: resolveApp("yarn.lock"),
testsSetup: resolveApp("client/setupTests.js"),
appNodeModules: resolveApp("node_modules"),
nodePaths: nodePaths,
2017-11-26 05:19:55 +00:00
publicUrl: getPublicUrl(resolveApp("package.json")),
servedPath: getServedPath(resolveApp("package.json"))
}