1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const merge = require('webpack-merge')
|
5 | const validateOptions = require('@mara/schema-utils')
|
6 | const maraxOptionsSchema = require('./maraxOptions')
|
7 | const paths = require('./paths')
|
8 | const argv = require('./argv')
|
9 | const { isObject } = require('@mara/devkit')
|
10 | const defConf = require('./defaultOptions')
|
11 | const { DEPLOY_ENV, TARGET } = require('./const')
|
12 | const maraxVersion = require(paths.maraxPackageJson).version
|
13 |
|
14 | const maraConf = getMaraConf()
|
15 | const useTypeScript = fs.existsSync(paths.tsConfig)
|
16 | const useYarn = fs.existsSync(paths.yarnLock)
|
17 |
|
18 |
|
19 | const deployEnv = getDeployEnv(argv.env)
|
20 |
|
21 | const target = getBuildTarget(maraConf.globalEnv)
|
22 |
|
23 | function getBuildTarget(globalEnv) {
|
24 | const targetMap = {
|
25 | web: TARGET.WEB,
|
26 |
|
27 | wap: TARGET.WEB,
|
28 | app: TARGET.APP
|
29 | }
|
30 | let target = targetMap[argv.target]
|
31 |
|
32 | if (target) {
|
33 |
|
34 |
|
35 |
|
36 | globalEnv.jsbridgeBuildType = target === TARGET.WEB ? TARGET.WAP : target
|
37 | } else if (globalEnv.jsbridgeBuildType === TARGET.APP) {
|
38 | target = globalEnv.jsbridgeBuildType
|
39 | } else {
|
40 |
|
41 |
|
42 | globalEnv.jsbridgeBuildType = TARGET.WAP
|
43 |
|
44 | target = TARGET.WEB
|
45 | }
|
46 |
|
47 | return target
|
48 | }
|
49 |
|
50 | function 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 |
|
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 |
|
70 | function 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 |
|
83 | function getCLIBooleanOptions(field, defVal = false) {
|
84 | const val = argv[field] || process.env[`npm_config_${field}`]
|
85 |
|
86 | return !!(typeof val === 'undefined' ? defVal : val)
|
87 | }
|
88 |
|
89 | function 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 |
|
102 | const maraContext = {
|
103 | argv,
|
104 | target,
|
105 |
|
106 |
|
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 |
|
119 | esm: maraConf.esm,
|
120 |
|
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 |
|
130 |
|
131 |
|
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 |
|
142 | hybrid: maraConf.hybrid,
|
143 | babelPlugins: maraConf.babelPlugins,
|
144 | postcss: {
|
145 | stage: 3,
|
146 |
|
147 |
|
148 | features: {
|
149 |
|
150 | 'css-images-image-set-notation': false
|
151 | }
|
152 | },
|
153 | marax: maraConf.marax
|
154 | }
|
155 |
|
156 | module.exports = maraContext
|