UNPKG

1.9 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 swiper dom7 比较坑爹,里面用了 const
65const commonInclude = [
66 PATH.appSrc,
67 /gm-/,
68 /gmfe/,
69 /gm_static_storage/,
70 /react-mgm/,
71 /react-gm/,
72 /query-string/,
73 /split-on-first/,
74 /strict-uri-encode/,
75 /swiper/,
76 /dom7/,
77]
78
79const packageJson = JSON.parse(fs.readFileSync(PATH.appPackageJson))
80
81module.exports = {
82 isEnvDevelopment,
83 isEnvTest,
84 isEnvProduction,
85 appDirectory,
86 PATH,
87 packageJson,
88 commonInclude,
89 shellExec,
90 getConfig,
91}