UNPKG

1.75 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const fs = require('fs');
5const url = require('url');
6
7//应用目录
8const appDirectory = fs.realpathSync(process.cwd());
9
10const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
11const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
12
13const envPublicUrl = process.env.PUBLIC_URL;
14
15
16
17//路径中末尾反斜杆的处理
18function ensureSlash(path, needsSlash) {
19 const hasSlash = path.endsWith('/');
20 if (hasSlash && !needsSlash) {
21 return path.substr(path, path.length - 1);
22 } else if (!hasSlash && needsSlash) {
23 return `${path}/`;
24 } else {
25 return path;
26 }
27}
28
29const getPublicUrl = appPackageJson =>
30 envPublicUrl || require(appPackageJson).homepage;
31
32
33function getServedPath(appPackageJson) {
34 const publicUrl = getPublicUrl(appPackageJson);
35 const servedUrl =
36 envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
37 return ensureSlash(servedUrl, true);
38}
39
40module.exports = {
41 dotenv: resolveApp('.env'),
42 appPath: resolveApp('.'),
43 appBuild: resolveApp('build'),
44 appDevBuild: resolveApp('build/dev'),
45 appProdBuild: resolveApp('build/prod'),
46 appPackage: resolveApp('build/pkg'),
47 appPackageDev: resolveApp('build/pkg-dev'),
48 appPublic: resolveApp('dist'),
49 appIndexJs: resolveApp('index.js'),
50 appPackageJson: resolveApp('package.json'),
51 appSrc: resolveApp('.'),
52 yarnLockFile: resolveApp('yarn.lock'),
53 appNodeModules: resolveApp('node_modules'),
54 publicUrl: getPublicUrl(resolveApp('package.json')),
55 servedPath: getServedPath(resolveApp('package.json')),
56 ownPath: resolveOwn('.'),
57 ownNodeModules: resolveOwn('node_modules')
58};