UNPKG

2.02 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const fs = require('fs');
5const 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
9const appDirectory = fs.realpathSync(process.cwd());
10const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
11
12const envPublicUrl = process.env.PUBLIC_URL;
13
14function ensureSlash(path, needsSlash) {
15 const hasSlash = path.endsWith('/');
16 if (hasSlash && !needsSlash) {
17 return path.substr(path, path.length - 1);
18 } else if (!hasSlash && needsSlash) {
19 return `${path}/`;
20 } else {
21 return path;
22 }
23}
24
25const getPublicUrl = appPackageJson =>
26 envPublicUrl || require(appPackageJson).homepage;
27
28// We use `PUBLIC_URL` environment variable or "homepage" field to infer
29// "public path" at which the app is served.
30// Webpack needs to know it to put the right <script> hrefs into HTML even in
31// single-page apps that may serve index.html for nested URLs like /todos/42.
32// We can't use a relative path in HTML because we don't want to load something
33// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
34function getServedPath(appPackageJson) {
35 const publicUrl = getPublicUrl(appPackageJson);
36 const servedUrl =
37 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
38 return ensureSlash(servedUrl, true);
39}
40
41// config after eject: we're in ./config/
42module.exports = {
43 dotenv: resolveApp('.env'),
44 appBuild: resolveApp('lib'),
45 appPublic: resolveApp('public'),
46 appHtml: resolveApp('public/index.html'),
47 appIndexJs: resolveApp('src/index.js'),
48 appLibJs: resolveApp('src/kara-uc/index.js'),
49 appPackageJson: resolveApp('package.json'),
50 appSrc: resolveApp('src'),
51 yarnLockFile: resolveApp('yarn.lock'),
52 testsSetup: resolveApp('src/setupTests.js'),
53 appNodeModules: resolveApp('node_modules'),
54 publicUrl: getPublicUrl(resolveApp('package.json')),
55 servedPath: getServedPath(resolveApp('package.json')),
56};