paths.js 2.14 KB
Newer Older
zhanghaozhe committed
1
"use strict"
2

zhanghaozhe committed
3 4 5
const path = require("path")
const fs = require("fs")
const getPublicUrlOrPath = require("react-dev-utils/getPublicUrlOrPath")
6 7 8

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
zhanghaozhe committed
9 10
const appDirectory = fs.realpathSync(process.cwd())
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath)
11 12 13

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
zhanghaozhe committed
14
// webpack needs to know it to put the right <script> hrefs into HTML even in
15 16 17
// 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.
zhanghaozhe committed
18
const publicUrlOrPath = getPublicUrlOrPath(
zhanghaozhe committed
19 20
  process.env.NODE_ENV === "development",
  require(resolveApp("package.json")).homepage,
zhanghaozhe committed
21
  process.env.PUBLIC_URL
zhanghaozhe committed
22
)
23 24

const moduleFileExtensions = [
zhanghaozhe committed
25 26 27 28 29 30 31 32 33 34 35 36
  "web.mjs",
  "mjs",
  "web.js",
  "js",
  "web.ts",
  "ts",
  "web.tsx",
  "tsx",
  "json",
  "web.jsx",
  "jsx",
]
37 38 39

// Resolve file paths in the same order as webpack
const resolveModule = (resolveFn, filePath) => {
zhanghaozhe committed
40
  const extension = moduleFileExtensions.find((extension) =>
41
    fs.existsSync(resolveFn(`${filePath}.${extension}`))
zhanghaozhe committed
42
  )
43 44

  if (extension) {
zhanghaozhe committed
45
    return resolveFn(`${filePath}.${extension}`)
46 47
  }

zhanghaozhe committed
48 49
  return resolveFn(`${filePath}.js`)
}
50 51 52

// config after eject: we're in ./config/
module.exports = {
zhanghaozhe committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
  dotenv: resolveApp(".env"),
  appPath: resolveApp("."),
  appBuild: resolveApp("build"),
  appPublic: resolveApp("public"),
  appHtml: resolveApp("public/index.html"),
  appIndexJs: resolveModule(resolveApp, "src/index"),
  appPackageJson: resolveApp("package.json"),
  appSrc: resolveApp("src"),
  appTsConfig: resolveApp("tsconfig.json"),
  appJsConfig: resolveApp("jsconfig.json"),
  yarnLockFile: resolveApp("yarn.lock"),
  testsSetup: resolveModule(resolveApp, "src/setupTests"),
  proxySetup: resolveApp("src/setupProxy.js"),
  appNodeModules: resolveApp("node_modules"),
zhanghaozhe committed
67
  publicUrlOrPath,
zhanghaozhe committed
68
}
69

zhanghaozhe committed
70
module.exports.moduleFileExtensions = moduleFileExtensions