1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | const path = require('path');
|
12 | const fs = require('fs');
|
13 | const url = require('url');
|
14 | const oem = require('./oem');
|
15 |
|
16 |
|
17 |
|
18 | const appDirectory = fs.realpathSync(process.cwd());
|
19 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
|
20 |
|
21 | const envPublicUrl = process.env.PUBLIC_URL;
|
22 |
|
23 | function ensureSlash(inputPath, needsSlash) {
|
24 | const hasSlash = inputPath.endsWith('/');
|
25 | if (hasSlash && !needsSlash) {
|
26 | return inputPath.substr(0, inputPath.length - 1);
|
27 | } else if (!hasSlash && needsSlash) {
|
28 | return `${inputPath}/`;
|
29 | } else {
|
30 | return inputPath;
|
31 | }
|
32 | }
|
33 |
|
34 | const getPublicUrl = appPackageJson =>
|
35 | envPublicUrl || require(appPackageJson).homepage;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function getServedPath(appPackageJson) {
|
44 | const publicUrl = getPublicUrl(appPackageJson);
|
45 | const servedUrl =
|
46 | envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
|
47 | return ensureSlash(servedUrl, true);
|
48 | }
|
49 |
|
50 | const moduleFileExtensions = [
|
51 | 'web.mjs',
|
52 | 'mjs',
|
53 | 'web.js',
|
54 | 'js',
|
55 | 'web.ts',
|
56 | 'ts',
|
57 | 'web.tsx',
|
58 | 'tsx',
|
59 | 'json',
|
60 | 'web.jsx',
|
61 | 'jsx',
|
62 | ];
|
63 |
|
64 |
|
65 | const resolveModule = (resolveFn, filePath) => {
|
66 | const extension = moduleFileExtensions.find(extension =>
|
67 | fs.existsSync(resolveFn(`${filePath}.${extension}`))
|
68 | );
|
69 |
|
70 | if (extension) {
|
71 | return resolveFn(`${filePath}.${extension}`);
|
72 | }
|
73 |
|
74 | return resolveFn(`${filePath}.js`);
|
75 | };
|
76 |
|
77 |
|
78 | module.exports = {
|
79 | dotenv: resolveApp('.env'),
|
80 | appPath: resolveApp('.'),
|
81 | appBuild: resolveApp(path.join('dist', oem.reactAppOem)),
|
82 | appPublic: resolveApp('public'),
|
83 | appAsset: resolveApp('asset'),
|
84 | appHtml: resolveApp('public/index.html'),
|
85 | errorHtml: resolveApp('asset/error.html'),
|
86 | appIndexJs: resolveModule(resolveApp, 'src/index'),
|
87 | appPackageJson: resolveApp('package.json'),
|
88 | appSrc: resolveApp('src'),
|
89 | appTsConfig: resolveApp('tsconfig.json'),
|
90 | appJsConfig: resolveApp('jsconfig.json'),
|
91 | yarnLockFile: resolveApp('yarn.lock'),
|
92 | testsSetup: resolveModule(resolveApp, 'src/setupTests'),
|
93 | proxySetup: resolveApp('src/setupProxy.js'),
|
94 | appNodeModules: resolveApp('node_modules'),
|
95 | publicUrl: getPublicUrl(resolveApp('package.json')),
|
96 | servedPath: getServedPath(resolveApp('package.json')),
|
97 | };
|
98 |
|
99 |
|
100 | const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
|
101 |
|
102 |
|
103 | module.exports = {
|
104 | dotenv: resolveApp('.env'),
|
105 | appPath: resolveApp('.'),
|
106 | appBuild: resolveApp(path.join('dist', oem.reactAppOem)),
|
107 | appPublic: resolveApp('public'),
|
108 | appAsset: resolveApp('asset'),
|
109 | appHtml: resolveApp('public/index.html'),
|
110 | errorHtml: resolveApp('asset/error.html'),
|
111 | appIndexJs: resolveModule(resolveApp, 'src/index'),
|
112 | appPackageJson: resolveApp('package.json'),
|
113 | appSrc: resolveApp('src'),
|
114 | appTsConfig: resolveApp('tsconfig.json'),
|
115 | appJsConfig: resolveApp('jsconfig.json'),
|
116 | yarnLockFile: resolveApp('yarn.lock'),
|
117 | testsSetup: resolveModule(resolveApp, 'src/setupTests'),
|
118 | proxySetup: resolveApp('src/setupProxy.js'),
|
119 | appNodeModules: resolveApp('node_modules'),
|
120 | publicUrl: getPublicUrl(resolveApp('package.json')),
|
121 | servedPath: getServedPath(resolveApp('package.json')),
|
122 |
|
123 | ownPath: resolveOwn('.'),
|
124 | ownNodeModules: resolveOwn('node_modules'),
|
125 | appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
|
126 | ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
|
127 | };
|
128 |
|
129 | const ownPackageJson = require('../package.json');
|
130 | const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
|
131 | const reactScriptsLinked =
|
132 | fs.existsSync(reactScriptsPath) &&
|
133 | fs.lstatSync(reactScriptsPath).isSymbolicLink();
|
134 |
|
135 |
|
136 | if (
|
137 | !reactScriptsLinked &&
|
138 | __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
|
139 | ) {
|
140 | module.exports = {
|
141 | dotenv: resolveOwn('template/.env'),
|
142 | appPath: resolveApp('.'),
|
143 | appBuild: resolveApp(path.join('../../dist', oem.reactAppOem)),
|
144 | appPublic: resolveOwn('template/public'),
|
145 | appAsset: resolveOwn('template/asset'),
|
146 | appHtml: resolveOwn('template/public/index.html'),
|
147 | errorHtml: resolveOwn('template/asset/error.html'),
|
148 | appIndexJs: resolveModule(resolveOwn, 'template/src/index'),
|
149 | appPackageJson: resolveOwn('package.json'),
|
150 | appSrc: resolveOwn('template/src'),
|
151 | appTsConfig: resolveOwn('template/tsconfig.json'),
|
152 | appJsConfig: resolveOwn('template/jsconfig.json'),
|
153 | yarnLockFile: resolveOwn('template/yarn.lock'),
|
154 | testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
|
155 | proxySetup: resolveOwn('template/src/setupProxy.js'),
|
156 | appNodeModules: resolveOwn('node_modules'),
|
157 | publicUrl: getPublicUrl(resolveOwn('package.json')),
|
158 | servedPath: getServedPath(resolveOwn('package.json')),
|
159 |
|
160 | ownPath: resolveOwn('.'),
|
161 | ownNodeModules: resolveOwn('node_modules'),
|
162 | appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
|
163 | ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
|
164 | };
|
165 | }
|
166 |
|
167 |
|
168 | module.exports.moduleFileExtensions = moduleFileExtensions;
|