1 | const _ = require('lodash');
|
2 | const path = require('path');
|
3 | const chalk = require('chalk');
|
4 | const fs = require('fs');
|
5 | const resolveApp = require('../tools').resolveApp;
|
6 |
|
7 |
|
8 |
|
9 | const ENV = process.env.NODE_ENV === 'development' ? 'dev' : 'prod';
|
10 | const webpackConfig = (function getCustomConfig() {
|
11 | const customConfigPath = path.resolve(`./config/webpack.config.${ENV}.js`);
|
12 | return fs.existsSync(customConfigPath) ? require(customConfigPath) : {}
|
13 | })();
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function getFilenames(webpackConfig) {
|
38 |
|
39 | let filenames = _.get(webpackConfig, ['output', 'filenames']);
|
40 |
|
41 | if (filenames && !_.isEmpty(filenames)) {
|
42 | return filenames
|
43 | }
|
44 |
|
45 |
|
46 | log(chalk.yellow('\n warning: config/filenames.js is deprecated, please use webpackconfig.output.filenames instead \n'));
|
47 |
|
48 | try {
|
49 | const env = process.env.NODE_ENV === 'development' ? 'dev' : 'prod';
|
50 | return require(path.resolve('config/filenames'))[env]
|
51 | } catch (e){
|
52 |
|
53 | }
|
54 |
|
55 |
|
56 | if (_.isEmpty(filenames)) {
|
57 | console.log(chalk.red('\n warning: webpackconfig.output.filenames is missing!'));
|
58 | process.exit(1);
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function getCdnPath(webpackConfig) {
|
88 |
|
89 | const defaultPublicPath = '';
|
90 | const publicPath = _.get(webpackConfig, ['output', 'publicPath']);
|
91 |
|
92 | let cdnConfig = {};
|
93 |
|
94 |
|
95 | if (_.isString(publicPath)) {
|
96 | return null
|
97 | }
|
98 |
|
99 |
|
100 | if (ENV === 'prod') {
|
101 |
|
102 | if (_.isPlainObject(publicPath)) {
|
103 |
|
104 | cdnConfig = publicPath;
|
105 |
|
106 | return {
|
107 | js: cdnConfig.js || defaultPublicPath,
|
108 | css: cdnConfig.css || defaultPublicPath,
|
109 | img: cdnConfig.img || defaultPublicPath,
|
110 | media: cdnConfig.media || defaultPublicPath
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 | try{
|
116 | cdnConfig = require(path.resolve('config/cdnPath'));
|
117 | cdnConfig && log(chalk.yellow('\n warning: config/cdnPath.js is deprecated, please use webpackconfig.output.publicPath instead \n'));
|
118 | return {
|
119 | js: cdnConfig.prodJsCDN || defaultPublicPath,
|
120 | css: cdnConfig.prodCssCDN || defaultPublicPath,
|
121 | img: cdnConfig.prodImgCDN || defaultPublicPath,
|
122 | media: cdnConfig.prodMediaCDN || defaultPublicPath
|
123 | }
|
124 | }catch(e){
|
125 |
|
126 |
|
127 | if (!_.isEmpty(webpackConfig) && _.isEmpty(publicPath)) {
|
128 | log(chalk.yellow('\n warning: webpackconfig.output.publicPath is missing!'));
|
129 | return null
|
130 | }
|
131 |
|
132 | }
|
133 |
|
134 |
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function getAliasConfig(webpackConfig) {
|
160 | let defaultConfig = {
|
161 | commons: path.resolve('src/components_common/'),
|
162 | tools: path.resolve('src/tools/'),
|
163 | api: path.resolve('src/api/'),
|
164 | config: path.resolve('src/config'),
|
165 | public: path.resolve('public/'),
|
166 | scss: path.resolve('src/scss_mixin/scss/'),
|
167 | scss_mixin: path.resolve('src/scss_mixin/'),
|
168 | };
|
169 |
|
170 | let alias = _.get(webpackConfig, ['resolve', 'alias']);
|
171 |
|
172 | if (_.isEmpty(alias)) {
|
173 | alias = defaultConfig;
|
174 |
|
175 | log(chalk.yellow('\n warning: ' + "config/alias.js is deprecated. You can use alias in webpackconfig.resolve.alias." + '\n'));
|
176 |
|
177 | try{
|
178 | alias = require(path.resolve('config/alias'));
|
179 | }catch(e){
|
180 | }
|
181 | }
|
182 |
|
183 |
|
184 | return alias
|
185 |
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function getCssModuleConfig(webpackConfig) {
|
209 | let cssModule = _.get(webpackConfig, 'cssModule');
|
210 |
|
211 | if (_.isEmpty(cssModule)) {
|
212 | return {
|
213 | exclude: '',
|
214 | name: '',
|
215 | enable: false
|
216 | }
|
217 | }
|
218 |
|
219 |
|
220 | let excludePath;
|
221 |
|
222 | if (_.isArray(cssModule.exclude)) {
|
223 | excludePath = cssModule.exclude.map((path) => resolveApp(path))
|
224 |
|
225 | } else {
|
226 | excludePath = resolveApp(cssModule.exclude)
|
227 | }
|
228 |
|
229 | const getLocalIdent = cssModule.getLocalIdent ? {getLocalIdent: cssModule.getLocalIdent} : {};
|
230 |
|
231 |
|
232 | return {
|
233 | exclude: excludePath,
|
234 | enable: true,
|
235 | config: Object.assign(
|
236 | getLocalIdent,
|
237 | {
|
238 | localIdentName: cssModule.localIdentName || cssModule.name || '[folder]__[local]-[hash:base64:5]',
|
239 | }
|
240 | )
|
241 | }
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function log(msg) {
|
248 | console.log(msg)
|
249 | }
|
250 |
|
251 | module.exports = {
|
252 | webpackConfig: webpackConfig,
|
253 |
|
254 | filenames: getFilenames(webpackConfig),
|
255 | cdnPath: getCdnPath(webpackConfig),
|
256 | alias: getAliasConfig(webpackConfig),
|
257 | cssModule: getCssModuleConfig(webpackConfig),
|
258 | externals: webpackConfig.externals
|
259 | }; |
\ | No newline at end of file |