UNPKG

4.46 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const merge = require('webpack-merge')
5const validateOptions = require('@mara/schema-utils')
6const maraxOptionsSchema = require('./maraxOptions')
7const paths = require('./paths')
8const argv = require('./argv')
9const { isObject } = require('@mara/devkit')
10const defConf = require('./defaultOptions')
11const { DEPLOY_ENV, TARGET } = require('./const')
12const maraxVersion = require(paths.maraxPackageJson).version
13
14const maraConf = getMaraConf()
15const useTypeScript = fs.existsSync(paths.tsConfig)
16const useYarn = fs.existsSync(paths.yarnLock)
17// deployEnv: dev 开发环境 | test 测试环境 | online 线上环境
18// 默认 online
19const deployEnv = getDeployEnv(argv.env)
20// app | web
21const target = getBuildTarget(maraConf.globalEnv)
22
23function getBuildTarget(globalEnv) {
24 const targetMap = {
25 web: TARGET.WEB,
26 // wap 映射为 web
27 wap: TARGET.WEB,
28 app: TARGET.APP
29 }
30 let target = targetMap[argv.target]
31
32 if (target) {
33 // 覆盖 globalEnv 配置
34 // jsbridgeBuildType 为 hybrid 两端统一依赖变量
35 // jsbridgeBuildType 可选值为 wap | app,当 target 为 web 时强制为 wap
36 globalEnv.jsbridgeBuildType = target === TARGET.WEB ? TARGET.WAP : target
37 } else if (globalEnv.jsbridgeBuildType === TARGET.APP) {
38 target = globalEnv.jsbridgeBuildType
39 } else {
40 // 当 target 缺省且 jsbridgeBuildType 缺省或值为非 app 时,
41 // jsbridgeBuildType 默认回退为 wap
42 globalEnv.jsbridgeBuildType = TARGET.WAP
43 // target 强制指定为 web
44 target = TARGET.WEB
45 }
46
47 return target
48}
49
50function getMaraConf() {
51 let maraConf = defConf
52
53 if (fs.existsSync(paths.marauder)) {
54 const userOptions = require(paths.marauder)
55
56 try {
57 if (validateOptions(maraxOptionsSchema, userOptions, 'mararc', 'Marax')) {
58 // use deep merge
59 maraConf = merge({}, defConf, userOptions)
60 }
61 } catch (e) {
62 console.log(e.message)
63 process.exit(1)
64 }
65 }
66
67 return maraConf
68}
69
70function getDeployEnv(env) {
71 switch (env) {
72 case 'dev':
73 return DEPLOY_ENV.DEV
74 case 'test':
75 return DEPLOY_ENV.TEST
76 case 'online':
77 return DEPLOY_ENV.ONLINE
78 default:
79 return DEPLOY_ENV.ONLINE
80 }
81}
82
83function getCLIBooleanOptions(field, defVal = false) {
84 const val = argv[field] || process.env[`npm_config_${field}`]
85
86 return !!(typeof val === 'undefined' ? defVal : val)
87}
88
89function getHashConf(hash) {
90 let { main, chunk } = defConf.hash
91
92 if (typeof hash === 'boolean') {
93 main = chunk = hash
94 } else if (isObject(hash)) {
95 main = hash.main === undefined ? main : hash.main
96 chunk = hash.chunk === undefined ? chunk : hash.chunk
97 }
98
99 return { main, chunk }
100}
101
102const maraContext = {
103 argv,
104 target,
105 // 为了防止不同文件夹下的同名资源文件冲突
106 // 资源文件不提供 hash 修改权限
107 hash: getHashConf(maraConf.hash),
108 deployEnv,
109 version: maraxVersion,
110 debug: argv.debug,
111 library: maraConf.library,
112 parallel: false,
113 globalEnv: maraConf.globalEnv,
114 tsImportLibs: maraConf.tsImportLibs,
115 webpackPluginsHandler: maraConf.webpackPluginsHandler,
116 // 编译配置
117 compiler: maraConf.compiler,
118 // 通知 babel 以项目级配置编译 node_module 里额外的模块
119 esm: maraConf.esm,
120 // 打包 dll
121 vendor: maraConf.vendor,
122 paths: paths,
123 publicPath: maraConf.publicPath,
124 prerender: maraConf.prerender,
125 build: {
126 sourceMap: maraConf.sourceMap,
127 report: getCLIBooleanOptions('report'),
128 writeStatsJson: getCLIBooleanOptions('stats'),
129 // upload bundle use ftp
130 // `npm run build <view> --ftp [namespace]`
131 // Set to `true` or `false` to always turn it on or off
132 uploadFtp: process.env.npm_config_ftp,
133 testDeploy: process.env.npm_config_test
134 },
135 tinifyKeys: maraConf.tinifyKeys,
136 devServer: maraConf.devServer,
137 useYarn,
138 useTypeScript,
139 ftp: maraConf.ftp,
140 ciConfig: maraConf.ciConfig,
141 // hybrid 项目配置,存在此属性时,将会生成 zip 包
142 hybrid: maraConf.hybrid,
143 babelPlugins: maraConf.babelPlugins,
144 postcss: {
145 stage: 3,
146 // 允许 flexbox 2009 以支持多行超出省略
147 // https://github.com/jonathantneal/postcss-preset-env/blob/master/lib/plugins-by-specification-id.js
148 features: {
149 // image-set polyfill 与雪碧图使用时存在 bug,在此禁用
150 'css-images-image-set-notation': false
151 }
152 },
153 marax: maraConf.marax
154}
155
156module.exports = maraContext