UNPKG

1.88 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const sh = require('shelljs')
3const path = require('path')
4
5const env = process.env.NODE_ENV
6const isEnvDevelopment = env === 'development'
7const isEnvTest = env === 'test'
8const isEnvProduction = env === 'production'
9
10const appDirectory = fs.realpathSync(process.cwd())
11const resolveApp = relativePath => path.resolve(appDirectory, relativePath)
12
13function shellExec(com) {
14 if (sh.exec(com).code !== 0) {
15 sh.exit(1)
16 }
17}
18
19const getLocalConfig = () => {
20 let config = {}
21 try {
22 config = require(appDirectory + '/config/local')
23 } catch (err) {
24 // nothing
25 }
26
27 return config
28}
29
30const getConfig = () => {
31 let config
32 try {
33 config = require(appDirectory + '/config/deploy')
34 } catch (err) {
35 throw new Error('没有找到 /config/deploy.js 文件')
36 }
37
38 // 开发默认 /build/
39 if (isEnvDevelopment) {
40 config.publicPath = '/build/'
41 }
42
43 // local 覆盖 deploy
44 Object.assign(config, getLocalConfig())
45
46 // 开发环境需要提供 publicPath
47 if (!isEnvDevelopment && !config.publicPath) {
48 throw new Error('production 没有提供 publicPath 字段')
49 }
50
51 return config
52}
53
54const PATH = {
55 appDirectory,
56 appBuild: resolveApp('build'),
57 appConfig: resolveApp('config'),
58 appSrc: resolveApp('src'),
59 appIndexTemplate: resolveApp('src/index.html'),
60 appIndexJs: resolveApp('src/index.js'),
61 appPackageJson: resolveApp('package.json'),
62}
63
64// query-string 比较坑爹,里面用了 const
65const commonInclude = [
66 PATH.appSrc,
67 /gm-/,
68 /gmfe/,
69 /gm-touch/,
70 /gm-common/,
71 /gm_static_storage/,
72 /react-mgm/,
73 /query-string/,
74 /split-on-first/,
75 /strict-uri-encode/,
76]
77
78const packageJson = JSON.parse(fs.readFileSync(PATH.appPackageJson))
79
80module.exports = {
81 isEnvDevelopment,
82 isEnvTest,
83 isEnvProduction,
84 appDirectory,
85 PATH,
86 packageJson,
87 commonInclude,
88 shellExec,
89 getConfig,
90}